Velocity drift?

Official forum for the Chipmunk2D Physics Library.

Velocity drift?

Postby alf » Wed Sep 17, 2014 5:14 pm

I'm experiencing an odd behavior and was unsuccessful attributing it to my own code, so thought I'd post here to see if it's a known issue or if perhaps I'm doing something incorrectly.

The demo I'm developing to convince my team to use Chipmunk is a simple "carousel" of icons. The user swipes left or right to scroll through them and I apply an impulse (rather than a force) as they swipe. I'm not yet trying to vary that impulse based on speed and distance of the swipe, I'm just doing a small impulse as they drag their finger so the icons basically follow the finger. Damping is being applied to each icon so they will always come to rest and the impulse isn't that large so they come to rest fairly quickly.

Anyhow, the impulse I'm applying only has an 'X' direction component -- the 'Y' is always 0.0f. But what happens is the icons seem to all scatter in random directions of both X & Y. The 'X' change is one thing but I'm surprised that the 'Y' is ever anything but 0.0f since I never set it or apply any force/impulse along 'Y'.

Am I using something incorrectly? For example, one shouldn't apply repeated impulses to an object? It seems to mostly work as expected except for this 'y" business. The bodies for the icons show zero for Y correctly in the beginning, then drift fairly quickly to another 'Y' value and stay at that value as I swipe left & right.

Sorry for the long post, but I wanted to give you as much context as possible.

Posts: 7
Joined: Thu Sep 11, 2014 3:16 am

Re: Velocity drift?

Postby slembcke » Wed Sep 17, 2014 6:20 pm

That's definitely weird. The x-component of an impulse only directly affects the x-velocity of a body.

Things I would check for first:
  • Are you causing a rotation to happen by applying an off center impulse? If the objects are moving in arcs, that's a pretty good clue.
  • Are you making any assumptions about rotations? A body's position isn't the same as it's CoG, which might throw you off.
  • Are the icons colliding with anything or each other?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! ->
User avatar
Site Admin
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Velocity drift?

Postby alf » Wed Sep 17, 2014 6:53 pm

Thanks for those suggestions! Gives me some ideas on what to check.

Since I'm making a super-simple call, I don't see how I'd be causing any unwanted rotation:

Code: Select all
cpBodyApplyImpulse(simBody, cpv(10.0f, 0.0f), cpvzero);

But since I do see the icons moving in an ever-so-slight vertical arc as they scroll across screen, I guess it's possible.

It's possible they may be colliding too, but since they're all in a row you wouldn't think it would cause such huge vertical movement. Could I just put every shape in the same group to test that theory?

Not sure how to check the rotation thing though. If I'm just applying a simple force as above, my assumption is it's being applied to the center of gravity. But I'm also assuming center of mass is the same point.
Posts: 7
Joined: Thu Sep 11, 2014 3:16 am

Re: Velocity drift?

Postby alf » Wed Sep 17, 2014 9:06 pm

Debugging, I noticed that all 'a' and 'w' values start out at zero then change to other values. I'm pretty sure it's collisions causing the random-ish movement because I tested a single body in isolation and it moved from left to right as expected.

I tried adding all my shapes to the same group in order to effectively turn off collision detection, but that didn't change the behavior. Is there some other way I should use to turn off collisions?

As for the center of mass not matching the center of gravity, here's how I declare all my bodies & shapes:

Code: Select all
int numVerts = 4;
cpVect verts[] =
   cpv(x, y - height + 1),         // Lower left
   cpv(x, y),                  // Upper left
   cpv(x + width - 1, y),            // Upper right
   cpv(x + width - 1, y - height + 1)   // Lower right

cpBody *newBody = cpBodyNew(bodyMass, cpMomentForPoly(1.0f, numVerts, verts, cpvzero));
cpBodySetPos(newBody, cpv(x + width/2.0f - 1.0f, y - height/2.0f + 1.0f));

cpShape *newShape = cpPolyShapeNew(newBody, numVerts, verts, cpvzero);
newShape->e = 1.0f;
newShape->u = 1.0f;

cpSpaceAddShape(simSpace, newShape);

Does that look reasonably correct?

Posts: 7
Joined: Thu Sep 11, 2014 3:16 am

Return to Chipmunk2D Physics

Who is online

Users browsing this forum: No registered users and 1 guest