Math for setLinearVelocity(x,y) for angles

Yea, I know this is more math than necessarily corona related, but, I need some help. I have a cannon barrel that rotates and I want to fire cannonballs out of it based on the angle of the cannon.

When the cannon is at a rotation of 0(pointing straight up) I can simply call cannoball:setLinearVelocity(0,-200) and the cannon ball fires upward at a decent pace. However, what if my cannon is rotated 45 degrees? I want the spawn point to be the same place, but I obviously need the proper X and Y values for setLinearVelocity(x,y) that still maintains the same 200 move speed but proportioned properly. What are the trig functions I need(and maybe a little sample)?

Thanks guys! [import]uid: 75335 topic_id: 12577 reply_id: 312577[/import]

Well, I had a friend help me out and I got to an answer cannonball:setLinearVelocity(math.sin(math.rad(turretbarrel.rotation))*200,math.cos(math.rad(turretbarrel.rotation))*-200) gets the functionality I wanted, if anyone is doing something similar.

turretbarrel.rotation = 0 when pointing straight up, increasing in degrees to the right, or decreasing to the left, so pointing straight right is 90, straight left is -90.

Also, if you know a way to make this call *faster* let me know :slight_smile: [import]uid: 75335 topic_id: 12577 reply_id: 45987[/import]

Very cool! With this and some extra tinkering we can make great stuff! Cannons are cool physics things, aren’t they? Anyways, thanks a lot! I will try to put this to great use. In the meantime, keep on coding! :wink: [import]uid: 82408 topic_id: 12577 reply_id: 56970[/import]