Implementing a Bullet

Official forum for the Chipmunk2D Physics Library.
Post Reply
Kipt
Posts: 8
Joined: Tue Jun 07, 2016 6:48 am
Contact:

Implementing a Bullet

Post by Kipt »

As I understand Chipmunk2D does not support any kind of CCD (continuous collision detection) but it does have ray-casting.

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?
    }
}
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 15 guests