Rotation to velocity?

Given a physics body – think a space ship – which I rotate based on player left/ right stick input, how can I set the velocity? I.e. how do I get the needed x/ y speed values for the setLinearVelocity(x, y) call, and make sure it’s always at a constant defined speed?

For what it’s worth, I found this bit in relation to another language:

ball.velocity = new Vector2( (float)Math.Cos(cannon.rotation), (float)Math.Sin(cannon.rotation)) \* 5.0f;

Thanks! [import]uid: 10284 topic_id: 4500 reply_id: 304500[/import]

speed = distance / time

I think that’s right, anyway. I’m afraid I’ve only used gravity, applyForce() and transition.to() to get motion, so far…

m [import]uid: 8271 topic_id: 4500 reply_id: 14113[/import]

Not sure if this would help answer the original question, but the following snippet is from an enterFrame event handler where the object’s velocity is capped to a certain maximum (mspeed):

[lua]–…
local vx, vy = ob:getLinearVelocity()
local v = math.sqrt(vx*vx + vy*vy)
if v > mspeed then
vx = vx * mspeed/v
vy = vy * mspeed/v
ob:setLinearVelocity(vx,vy)
end
–…[/lua]

You could use this to set a constant speed for an object in whatever direction the object goes.
Hope that helps.
-Frank. [import]uid: 8093 topic_id: 4500 reply_id: 14143[/import]

I don’t have my code at the moment, but if I remember correctly, I use something like this:

local angle = ship.rotation;  
local x = math.sin(angle\*math.pi/180);  
local y = math.cos(angle\*math.pi/180);  
ship:setLinearVelocity(x\*desiredSpeed, y\*desiredSpeed);  

Can’t remember if sin is the x and cos the y or viceversa. [import]uid: 8486 topic_id: 4500 reply_id: 14149[/import]

Thanks H4ch1! When I subtract 90 from the angle and switch sin and cos, it works perfectly! [import]uid: 10284 topic_id: 4500 reply_id: 14219[/import]

Just FYI, that’s because, mathmatically speaking, 0 degrees and radians is at ‘east’ rather than ‘north’ as we tend to assume. M [import]uid: 8271 topic_id: 4500 reply_id: 14341[/import]

Ah thanks! Yeah I normalized all my sprite images to be north-facing… [import]uid: 10284 topic_id: 4500 reply_id: 14345[/import]