Page 1 of 1
Rotating a body, 360 degree flipping issue.
Posted: Fri Nov 12, 2010 8:16 am
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?
Thanks David
Re: Rotating a body, 360 degree flipping issue.
Posted: Fri Nov 12, 2010 9:58 am
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));
Re: Rotating a body, 360 degree flipping issue.
Posted: Fri Nov 12, 2010 11:25 am
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
Re: Rotating a body, 360 degree flipping issue.
Posted: Fri Nov 12, 2010 11:29 am
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

.
Re: Rotating a body, 360 degree flipping issue.
Posted: Fri Nov 12, 2010 12:14 pm
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.
Re: Rotating a body, 360 degree flipping issue.
Posted: Fri Nov 12, 2010 12:23 pm
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.