Facing a point using forces
-
- Posts: 2
- Joined: Sun Feb 10, 2008 10:41 pm
- Contact:
Facing a point using forces
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?
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?
-
- Posts: 18
- Joined: Wed Oct 31, 2007 7:55 pm
- Contact:
Re: Facing a point using forces
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.
I havn't done this before and I don't know how calculate the angular velocity value, but that's what I would try.
-
- Posts: 56
- Joined: Tue Sep 11, 2007 2:30 pm
- Contact:
Re: Facing a point using forces
Have you tried using the cpBodySetAngle() method to set your angle?
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Facing a point using forces
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)
From that you can apply a damped torque, maybe something like:
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
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))
Code: Select all
torque = -delta_angle*torque_multiplier - body->w*damping_multiplier
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/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 2
- Joined: Sun Feb 10, 2008 10:41 pm
- Contact:
Re: Facing a point using forces
Hello again,
Your solution worked very well! For anyone who is curious here is the basic jist of what worked
Thank you! Your work is fantastic!
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
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Facing a point using forces
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:
That way you aren't converting to and angle and back. Whatever you find more readable I guess.
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));
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 29
- Joined: Sat May 10, 2008 1:41 pm
- Contact:
Re: Facing a point using forces
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.
I'l also try using forces to generate torque instead of ->w.
What are asin and atan2? Didn't find them in the docs.
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Facing a point using forces
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/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 29
- Joined: Sat May 10, 2008 1:41 pm
- Contact:
Re: Facing a point using forces
Oh..
Yep, i don't know C++ at all.
Yep, i don't know C++ at all.
-
- Posts: 1
- Joined: Wed Apr 02, 2008 3:39 pm
- Contact:
Re: Facing a point using forces
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):
This is the only way I could find to achieve any reduction in oscillation without strange effects and other jumpiness.
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
Who is online
Users browsing this forum: No registered users and 5 guests