Page 1 of 1

Kinematic Collisions?

PostPosted: Sat May 28, 2016 9:56 pm
by Jason
Edit: somehow missed the general chipmunk section above this one. Feel free to move!

Hi all,
Not sure where to post support requests, doesn't seem to be a section for it. Not sure if people use this forum, either... but I'll try.

My question: does this library support kinematic collisions with static objects? I've just tried to implement something very simple and I can't get it to work at all - the entity falls right through the segment without colliding.

I create a space. I make a platform segment using the static body of the space. I set it to start at -20, 1 and end at 20, 1. I've tried changing the value of the radius to other values as well. And after that, I add the shape to the space.

Code: Select all

cpSpace* space = cpSpaceNew();
cpShape* platform = cpSegmentShapeNew(cpSpaceGetStaticBody(space), cpv(-20, 1), cpv(20, 1), 0);
cpSpaceAddShape(space, platform);

Then for my entity:

I used cpSpaceAddbody to create a kinematic body for the entity. I set its position above the platform. I create a box shape for it and add it to the space. Not sure what the radius does here for polygonal shapes.

Code: Select all

entity->rigidbody = cpSpaceAddBody(space, cpBodyNewKinematic());
cpBodySetPosition(entity->rigidbody, cpv(4.5f, 5.5f));
entity->shape = cpSpaceAddShape(space, cpBoxShapeNew(entity->rigidbody, 1.2f, 0.8f, 0));

I use some movement code, which works fine. Then I move it by setting the rigidbody's velocity. This all seems to work fine in terms of movement, but no collision occurs - entity falls right through it.

Code: Select all

float speed = 50.0f;
accel = accel * speed;
accel = accel + entity.velocity * -8.0f;
accel.y = -30.0f;
entity.velocity = (accel * deltaTime) + entity.velocity;

cpBodySetVelocity(entity.rigidbody, cpv(entity.velocity.x, entity.velocity.y));

I do also call the step function 60 times per second.

Code: Select all
cpSpaceStep(state->space, input->deltaTime);

Did I miss something important? I'm not sure why it isn't working.

Thanks in advance for your help.

Re: Kinematic Collisions?

PostPosted: Mon May 30, 2016 1:05 am
by slembcke
My question: does this library support kinematic collisions with static objects?

Not in the way you want. Static and kinematic bodies are very similar in one very important way; they both have infinite mass and moment of inertia. That makes static bodies immovable, and kinematic bodies unstoppable. The key idea with kinematic bodies is that nothing should be able to stop them from moving in exactly the way you ask it to. So what happens when an unstoppable object hits an immovable object? That brain teaser is why Chipmunk chooses to simply ignore collisions between objects with infinite mass.

You need to make a regular body with a finite mass, perhaps a very large mass if you don't want regular sized objects to noticeably affect it. Just keep in mind that setting the velocity every frame on an object is effectively the same as applying a huge force to it if it causes a large change. Imagine a car smashing into a wall, then you ask it to accelerate back to full speed 1/60th of a second later. It's not necessarily a problem as long as you know that's what you are asking the physics to do.

Re: Kinematic Collisions?

PostPosted: Mon May 30, 2016 12:56 pm
by Jason
Thanks for the response.

That makes sense. So, I made my entity dynamic and gave it a mass of 1000 (not sure how big of a mass is sufficient). The problem I have now is that the entity is slowed down when initially hitting the segment, but then passes straight through it and speeds up rapidly after. (Can't even notice a slow down when the segment's radius is set to 0).

I can't use Chipmunk's gravity handling (through cpSpaceSetGravity) because the entity falls like a ton of bricks due to its huge mass. So I set the velocity downward as I did before. But this seems to break collisions.

Is there anything I can do about this?

I essentially want what I believe is called a character controller. The ability to kinematically control entities, let the physics engine handle the collision handling and proper sliding along the walls, but let me handle the motion code.

Given the complexity of collision handling on the level I'll need to for my game, I didn't want to have to write it myself.

Edit: Setting the mass to 1 and using Chipmunk's gravity handling solves the collision problem, but then I can't use setVelocity (because it seems to override whatever chipmunk is doing with gravity and keeps me stuck standing in the air). That would bring me to using a fully dynamic simulation, which I was trying to avoid.

Re: Kinematic Collisions?

PostPosted: Tue May 31, 2016 11:16 am
by slembcke
This needs a more detailed answer than I'm able to give for the next couple days until I get home from a conference. Ping the thread again on Thursday if I forget.