With ray-casting you can cast a line-segment from the bullets position to the position where it would be next time step (with cpSpaceSegmentQuery). This way you can implement a lot of "bullet" logic, like a gun shooting or basically anything that doesn't require a collision to be resolved. However, I would need the collision to be resolved.
Given I know the bullet's mass and material properties, how do I simulate a collision between the bullet and a body it hits based on the result from cpSpaceSegmentQueryFirst?
Consider the following code:
Code: Select all
struct Bullet
{
cpVect pos;
cpVect velocity;
float radius;
cpShapeFilter filter;
Material material; // density, friction, elasticity, ...
};
void updateBullet(cpSpace* space, Bullet* bullet, float timeStep)
{
cpVect oldPos = bullet->pos;
bullet->pos = ... // updates to new position.
if (cpShape* hit = cpSpaceSegmentQueryFirst(space, oldPos, bullet->pos, bullet->radius, bullet->filter, NULL))
{
// What code should be performed here to simulate a physics collision?
}
}