Page 1 of 1

Understanding applyImpulse

Posted: Sun Mar 22, 2009 1:59 pm
by kendric
I am trying to understand applyImpulse. I have circle shapes and I want to push them without adding any angular velocity. I thought if I just applied an impluse directly at their center, with the force vector being the direction I want them to move , they would do so. But they start spinning. On reading the boards, I see people talking about the force vector being in world co-ordinates vs object co-ordinates. My question is, how is the force vector in a co-ordinate system? Isn't it just X=horizontal component of the force and y=vertical, ie a force vector of 5,5 would be a northeast(in opengl) pushing force of strength 5?
Clearly I don't have this down quite right.
I also tried applying the impulse at a position which is shifted from the center by a inverse of the force vector * my radius but that didnt do it either.

Thoughts?

Updated, Ok I figured out what I was misunderstanding here I think. If somebody can correct me, the 2 vectors you pass are the start and end point of a line that represents the push on the object. The longer the line the bigger, and the point of impact of the line with the shape determines what happens as far as rotation vs velocity etc.

So the only question I have left now is whats the correct force to apply to a circle to make it rotate without making it move. I was trying vertical forces along the circles edge, kinda like a hand hitting a wheel to spin it, but it still gets bumped forward. Should I just hard set the angular momentum variable instead or is that not a good way to use this engine?

Great engine btw, thanks !

Re: Understanding applyImpulse

Posted: Sun Mar 22, 2009 3:46 pm
by slembcke
Hmm. Sounds like you are still very confused.

I apologize for using the same shorthand that the underlying physics equations use. That was a markedly bad idea on my part. People often use physics libraries so they don't have to learn that sort of stuff... <.<

cpBodyApplyImpulse(cpBody *body, cpVect j, cpVect r)

j is the impulse vector. In world space coordinates (see below) and r is the offset from the center of gravity (also in world space coordinates). If you don't want the body to rotate, use cpv(0,0) for the r (offset) parameter.

Now what are world space and body space coordinates? World space coordinates are the absolute coordinates that you probably normally just think of as "coordinates" in general. Body space coordinates are relative to a body (rotation and position. This means that (0,0) will always be at the center of gravity of the body and everything else rotates around the center of gravity as the body rotates. You can convert between the two using the cpBodyLocal2World() and cpBodyWorld2Local() functions.

Put in another way, it depends on what you want the force to be relative to. World space coordinates make more sense in a platformer game where "left" means to make the character move to the left side of the screen. Body relative coordinates make more sense in space games were you have thrusters attached to your ship. The position of the thruster and the direction of it's force is relative to the body. You would have to convert both values in order to use them with the cpBodyApplyImpulse() function.

Re: Understanding applyImpulse

Posted: Sun Mar 22, 2009 6:46 pm
by kendric
Hey. I think your using world space co-ordinates wording in a way thats confusing. If you want to give offset from center of gravity, and you say you want it in world space co-ordinates, then the value that would mean right in the center would be body->p. Anything in worldspace co-ordinates in other gui framworks means relative to 0,0 (ie absolute). Relative co-ordinates would be relative to body. So a 0,0 center of gravity would be relative to the body if its at its center. Does that help? So for the force vector, this is where my confusion came in. If you say the force vector is in world space co-ordinates, then if my body is at 150, 150 and you give a 5,5 vector in world space co-ordinates it means that the vector represents the offset of 5,5 from the axis, which is 145 units away from the body.

I got the no rotation push working, how would i do a rotation only push? Ie spin a circle without moving it?
Thanks again.

Re: Understanding applyImpulse

Posted: Mon Mar 23, 2009 6:44 am
by ker
probably by applying two forces that point in opposite directions and are applied on opposite sides of the body:

Code: Select all

cpBodyApplyImpulse(body, cpv(-1.0, 0.0), (0.0, -1.0));
cpBodyApplyImpulse(body, cpv(1.0, 0.0), (0.0, 1.0));
maybe cpBodyApplyForce would make more sense here... I'm not sure

Also if you want to directly control the rotation directly modifying angular velocity and torque might make a lot more sense.

Re: Understanding applyImpulse

Posted: Tue Mar 24, 2009 1:04 pm
by maximile
Chipmunk's use of world-space and object-space is standard with other frameworks. But IIRC some versions of the documentation had them the wrong way round on that function. Perhaps that's the problem here.