Missing collisions?

Official forum for the Chipmunk2D Physics Library.
Post Reply
juanii
Posts: 2
Joined: Tue Mar 31, 2009 10:06 am
Contact:

Missing collisions?

Post by juanii »

Hello. I'm new to Chipmunk and full of doubts about how to use it :)
Though this is a simple simulation, it seems to be failing. It's supposed to be viewed from top (this is why gravity is zero). The vehicle goes upward and should collide with the sloped wall above it.
Sorry for the lenght of the code, but I didn't want to miss anything.

// Create a space and set its properties
cpSpace *space = cpSpaceNew();
space->iterations = space->elasticIterations = 10;
space->gravity = cpv(0, 0);
space->damping = 0.2f;

// A sloped wall "above" the vehicle
cpBody *wall = cpBodyNew(INFINITY, INFINITY);
cpShape *wallShape = cpSegmentShapeNew(wall, cpv(170, 1081), cpv(278, 1137), 0);
wallShape->e = wallShape->u = 1.0f;
cpSpaceAddStaticShape(space, wallShape);

// The vehicle has a somehow "sharp" tip but it fails even if it's rectangular
cpVect vehicleVerts[6] = { cpv(-15, 5), cpv(-15, -5), cpv(8, -5), cpv(15, -3), cpv(15, 3), cpv(8, 5) };
cpBody *vehicle = cpBodyNew(10.0f, cpMomentForPoly(10.0f, 6, vehicleVerts, cpvzero));
vehicle->p = cpv(196, 499);
cpBodySetAngle(vehicle, M_PI_2);
cpSpaceAddBody(space, vehicle);

cpShape *vehicleShape = cpPolyShapeNew(vehicle, 6, vehicleVerts, cpvzero);
vehicleShape->e = 1.0f;
vehicleShape->u = 1.5f;
cpSpaceAddShape(space, vehicleShape);


Now we start moving.This event fires 40 times per second (t = 1/40.0f)

// Give the vehicle a push upwards
cpVect impulse = cpv(200, 0);
float tSim = t / 4.0;

// Rotation here doesn't play anything but to give the impulse the upward direction
cpBodyApplyImpulse(vehicle, cpvrotate(impulse, vehicle->a), cpvrotate(cpv(8, 0), vehicle->a));
for (int i = 0; i < 4; i++)
cpSpaceStep(space, tSim);

// update screen


The problem is the vehicle passes _through_ the sloped wall... no matter how little I make tSim, the most I'm getting is a slight change in the vehicle angle, but it still goes through the wall. I tried giving the vehicle a circular shape and it started to work fine, but well... the fact is it _isn't_ a circle :)

Any help on this will be very appreciated.

Regards
maximile
Posts: 157
Joined: Mon Aug 20, 2007 12:53 pm
Location: London, UK
Contact:

Re: Missing collisions?

Post by maximile »

Have you tried reversing the order of the vertices on the vehicle?
juanii
Posts: 2
Joined: Tue Mar 31, 2009 10:06 am
Contact:

Re: Missing collisions?

Post by juanii »

Do you mean defining it clockwise instead of counter clockwise? No, I didn't... but I've just done it and the first run was successful! :o
Is there a way to know how to define the polygons? is it _always_ clockwise?

Thank you very much!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Missing collisions?

Post by slembcke »

So this has come up sooooo many times. Not that I blame people for missing it, but I think maybe I should add an assertion to check for concavity and reversed winding. At least then it would fail hard and tell you why.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
maximile
Posts: 157
Joined: Mon Aug 20, 2007 12:53 pm
Location: London, UK
Contact:

Re: Missing collisions?

Post by maximile »

Sounds like a very good idea. I've been playing with Chipmunk for ages and it's still usually trial and error for me whether I get the winding right.

juanii: It's always the same. Counter-clockwise or clockwise depends on how your axes are set up.
Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests