Speculative CCD without rigidbody
I am making bullet physics through script without rigidbody using raycast and I wanna know how to implement Speculative CCD for it because bullets keep passing through at high speeds.
this is my bullet script
public class C_BaseKinematicEntity : MonoBehaviour
{
public C_BaseKinematicEntity(GameObject entity, EntityProperties_t entityProperties)
{
this.entity = entity;
this.mass = entityProperties.mass;
this.drag = entityProperties.drag;
this.angularDrag = entityProperties.angularDrag;
}
public GameObject entity;
public float drag, angularDrag, mass;
public Vector3 position, velocity, angularVelocity;
public Quaternion rotation;
public void Setup(EntityData_t entityData)
{
this.position = entityData.position;
this.rotation = entityData.rotation;
this.velocity = entityData.velocity;
this.angularVelocity = entityData.angularVelocity;
this.UpdateGameObject();
}
public void UpdateGameObject()
{
this.entity.transform.position = this.position;
this.entity.transform.rotation = this.rotation;
}
public void UpdateVelocity()
{
this.velocity += Physics.gravity * Time.fixedDeltaTime;
this.velocity *= (1 - Time.fixedDeltaTime * this.drag);
Util.Render.DebugCube(this.position + this.velocity, 0.1f, Color.grey);
}
public void UpdatePosition()
{
this.position += this.velocity * Time.fixedDeltaTime;
}
public void UpdateRotation()
{
this.rotation = Quaternion.FromToRotation(Vector3.forward, this.velocity);
}
public void UpdateCollision()
{
float maxDistance = this.velocity.magnitude * Time.fixedDeltaTime;
RaycastHit hit;
if (Physics.BoxCast(this.entity.transform.position, this.entity.transform.lossyScale / 2, this.entity.transform.forward, out hit, this.entity.transform.rotation, maxDistance))
{
ProcessCollision(hit);
}
}
public virtual void ProcessCollision(RaycastHit hit) { }
}
Answer by Edy · Sep 18, 2021 at 01:02 PM
You can take the current bullet position, the previous bullet position, and call Physics.RaycastAll() or Physics.SphereCastAll() between both positions. That would give you all objects the bullet has passed through in that timestep.
Sorry, that doesn't sound like right solution. I need something like interpolation, I need to smooth out the calls of bullet physics at a fixed frame rate.
Interpolation is a different thing. It won't give you CCD. You have to deal with discrete time steps of arbitrary duration, and interpolation doesn't guarantee your bullet won't keep passing through objects.
Casting the objects between previous and current position guarantees to detect all the affected objects, at any speed, under any time step. Then you could sort the hits by distance to the initial position and use the first hit only, or iterate over the hits to create the effect of the bullet trespassing objects (i.e. breaking a window, then hitting something behind it, everything in a single fixed step.
Okay, but I can't imagine the code, can you show me an example please and I dont plan to use RaycastAll because purpose of doing bullets like this with object pooling is high performance, but doing raycastAll on every single bullet will cause performance drop
Your answer
Follow this Question
Related Questions
Gun RayCast Origin lags with fast moving object. Help? 0 Answers
Any ideas how I could slightly improve the Standard Assets car side collisions. 1 Answer
Why do my physics objects rotate randomly sometimes when reparented 1 Answer
Building Behaving Badly: Rigidbody Blocks on a Kinematic Ground Object 0 Answers
Ball going through and sticking to flippers in a pinball game 1 Answer