Rotating a body, 360 degree flipping issue.

Official forum for the Chipmunk2D Physics Library.
Post Reply
ddeaco
Posts: 13
Joined: Fri Nov 12, 2010 7:46 am
Contact:

Rotating a body, 360 degree flipping issue.

Post by ddeaco »

Hi guys,
I'm having an issue when setting a body's angle, I am having a cannon point at the players finger(working on iPhone) as they move it around the screen. Which is simple enough but I don't want it to snap so I'm averaging the last position and the target position, so the cannon swings into position.
This works great apart from when the player moves the target over the top of the cannon, at the point which the cannon goes over 359 degrees to 0 degrees it spins all the way around. I can't seem to figure out a way to fix this problem.

Code: Select all

float angleCurrent =  cannonActive->a;
float angleTarget = -(atan2((cannonActive->p.x-targetPointer.x),(cannonActive->p.y-targetPointer.y))+1.57079633);
cpBodySetAngle(cannonActive, (angleCurrent+angleTarget)/2);
Any ideas? :cry:

Thanks David
[b][color=#FF8000]Follow me -
[url]http://www.IceFlame.com[/url][/color][/b]
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Rotating a body, 360 degree flipping issue.

Post by slembcke »

You either have to handle the cyclic nature (annoying), or use spherical linear interpolation (more expensive but simpler).

Something like this (completely untested) code should do what you want:

Code: Select all

// targetAngle is the angle you want to rotate to
// dt is the timestep
// amount is the percent to converge to each second. 0.25 means that the angle will be 75% of the way to the target after a second

cpVect rot = cpvslerp(body->rot, cpvforangle(targetAngle), cpfpow(1.0f/amount, -dt));
cpBodySetAngle(body, cpvtoangle(rot));

// or if you want a constant angular speed...

cpVect rot = cpvslerpconst(body->rot, cpvforangle(targetAngle), radiansPerSecond*dt);
cpBodySetAngle(body, cpvtoangle(rot));
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
ddeaco
Posts: 13
Joined: Fri Nov 12, 2010 7:46 am
Contact:

Re: Rotating a body, 360 degree flipping issue.

Post by ddeaco »

thanks slembcke for such a quick reply,

I've tried both methods. Changing the 'amount' in the first method doesn't seem to be making much difference at all, The second constant angular speed method works. But both have some strange results where the body will disappear completely...

Thanks David
[b][color=#FF8000]Follow me -
[url]http://www.IceFlame.com[/url][/color][/b]
bollu
Posts: 37
Joined: Tue Sep 14, 2010 4:33 am
Contact:

Re: Rotating a body, 360 degree flipping issue.

Post by bollu »

just wondering..

what does atan2 do? and why add that integer at the end?

P.S I don't have an answer, sorry :) .
ddeaco
Posts: 13
Joined: Fri Nov 12, 2010 7:46 am
Contact:

Re: Rotating a body, 360 degree flipping issue.

Post by ddeaco »

bollu wrote:just wondering..

what does atan2 do? and why add that integer at the end?

P.S I don't have an answer, sorry :) .
atan2 is a math function, which can be used to find the radian angle between to points.

1.57079633 is 180 degrees in radians.
[b][color=#FF8000]Follow me -
[url]http://www.IceFlame.com[/url][/color][/b]
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Rotating a body, 360 degree flipping issue.

Post by slembcke »

@ddeaco: Hrm, that's strange. Is it the case when you are telling it to do a complete 180? I may need to add some special cases to my slerp function. Also, if you want your cannon body to interact with other objects, you should probably take a look at the Tank demo that comes with Chipmunk. It uses a rogue control body and a specially configured gear joint to control the turning of the tank towards the mouse. (You can ignore the pivot joint as that controls it's position.)

@bollu: atan2() gives you the angle from the origin to a point in (x,y). If by integer you mean 1.57079633, the value is Pi/2 which is 90 degrees in radians.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests