Facing a point using forces

Official forum for the Chipmunk2D Physics Library.
Post Reply
smilesome
Posts: 2
Joined: Sun Feb 10, 2008 10:41 pm
Contact:

Facing a point using forces

Post by smilesome »

Hello,
I need a cpBody to face a certain point. However, I do not want to set the angle directly (using atan2) because it appears to interfere with chipmunk's inbuilt physics - the collision detection goes crazy and the cpBody starts to stick inside of other objects. (and other insanities...)

I figure I need to calculate a force and an offset in order to rotate my body towards the target point. I need to use forces in order to respect chipmunk's constructs. (I think...?)

Any ideas? :)
Android_X
Posts: 18
Joined: Wed Oct 31, 2007 7:55 pm
Contact:

Re: Facing a point using forces

Post by Android_X »

Rather than setting the position directly with body->a you clould try setting the angular velocity(body->w) to a value that after one cpSpaceStep() would result in the body facing the way you want.

I havn't done this before and I don't know how calculate the angular velocity value, but that's what I would try.
supertommy
Posts: 56
Joined: Tue Sep 11, 2007 2:30 pm
Contact:

Re: Facing a point using forces

Post by supertommy »

Have you tried using the cpBodySetAngle() method to set your angle?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Facing a point using forces

Post by slembcke »

You don't want to set the position or screw with the velocity I don't think. I would use forces.

A good start would be calculating the difference in orientation. (I'm on lunch break, so this is just off the top of my head)

Code: Select all

delta_angle = asin(cpvcross(body->rot, desired_rotation_vector))
From that you can apply a damped torque, maybe something like:

Code: Select all

torque = -delta_angle*torque_multiplier - body->w*damping_multiplier
Adjust the multipliers until it feels right. Also, it might be more physically correct to square body->w, I'd have to look it up though. You want to add a check that you aren't reversing the direction of the rotation as well. Clamp the force at (+ or -) body->w*body->i_inv/dt. Depending on your situation, the oscillation might be small enough that you won't need it though.

Makes me think though. Angular springs would be pretty easy to implement like the damped springs I already have. I'll add it to the list.

edit: Forget what I said about the torque earlier if you saw it. It was quite wrong. (In my defense, I typed that while hungry :() It should be better now
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
smilesome
Posts: 2
Joined: Sun Feb 10, 2008 10:41 pm
Contact:

Re: Facing a point using forces

Post by smilesome »

Hello again,
Your solution worked very well! For anyone who is curious here is the basic jist of what worked

Code: Select all

  float idealAngle = atan2(physicsBody->p.y-target-y,physicsBody->p.x-target->x);
  float delta_angle = asin(cpvcross(physicsBody->rot, cpvforangle(idealAngle)));
  physicsBody->w = -delta_angle*10.0 - physicsBody->w * .5;
  //again, play with the 10.0 and the .5 to find a good feel for your particular application
Thank you! Your work is fantastic!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Facing a point using forces

Post by slembcke »

Cool. I'll have to add that to my list of todos then.

Also, this is a slightly shorter more concise (probably more efficient) way to calculate your delta_angle:

Code: Select all

    
      cpVect direction = cpvnormalize(cpvsub(physicsBody->p, target));
      float delta_angle = asin(cpvcross(physicsBody->rot, direction));
That way you aren't converting to and angle and back. Whatever you find more readable I guess.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
scorp
Posts: 29
Joined: Sat May 10, 2008 1:41 pm
Contact:

Re: Facing a point using forces

Post by scorp »

Works very nice, thx.
I'l also try using forces to generate torque instead of ->w.

What are asin and atan2? Didn't find them in the docs.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Facing a point using forces

Post by slembcke »

asin() and atan2() are part of the C math library. (math.h)
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
scorp
Posts: 29
Joined: Sat May 10, 2008 1:41 pm
Contact:

Re: Facing a point using forces

Post by scorp »

Oh.. :oops:
Yep, i don't know C++ at all. :lol:
LaChance
Posts: 1
Joined: Wed Apr 02, 2008 3:39 pm
Contact:

Re: Facing a point using forces

Post by LaChance »

I hate to resurrect this but I'm having some trouble with oscillation using this technique. You say to clamp the force at "(+ or -) body->w*body->i_inv/dt" but I'm not quite sure if I'm doing this right. This seems to reduce the amount of oscillation even when using high torque multipliers but if my framerate drops very much then I start seeing oscillation again. I'm not sure if I'm doing something wrong or if this is expected.

Right now I'm basically doing this after I get the torque using the method you posted here (I'm using Python):

Code: Select all

clamp = body.angular_velocity * body.i_inv / dt
if torque > 0 and clamp < 0:
    torque = abs(clamp)
elif torque < 0 and clamp > 0:
    torque = -abs(clamp)

body.angular_velocity = torque
This is the only way I could find to achieve any reduction in oscillation without strange effects and other jumpiness.
Post Reply

Who is online

Users browsing this forum: Heise IT-Markt [Crawler] and 31 guests