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.
// 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/
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]
@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/