Working out angle to apply force

I’m currently tinkering with a little game where the players ship is moved about in much the same way as in Asteroids - rotate left/right and thrust.  The problem I’m having is how to apply the thrust to the players ship.

I found an old post about a very similar thing which had the following answer from Brent

  1. At  any  time when the boost/thrust button is pressed down, gather the angle (rotation) of the ship. Using basic vector math, convert this angle to X/Y vector values, and then apply that force to the ship (from behind it actually), focusing the force on the body’s center point. (http://docs.coronalabs.com/api/type/Body/applyForce.html)

Being a complete loser when it comes to anything maths related,  could some kind soul please show me how I would go about working out the vectors to apply the thrust.

Many thanks

Pull this gist down and run it. There are demos of my maths library:

https://gist.github.com/HoraceBury/9431861

The actual library function you want is math.rotateTo()

Basically, the way I do it, is to assume a point at {x=0,y=1} and pass that to the rotateTo function. It returns that point rotated to the location you want and you can then multiply it to increase force or whatever.

If you have further questions post some code and I’ll see if I can fill in the blanks.

Hey Appletreeman,

using a math library is perfectly fine, but if you just want to have this single functionality use this function:

local function angle2VecDeg(angle) angle = angle\*math.pi/180 return math.cos(angle), math.sin(angle) end local vecX, vecY = angle2VecDeg(90) --vector of length one at 90 degrees

Greetings

Torben

Many thanks to you both.

@horacebury - That maths lib is brilliant.  Many thanks.

@Torben - Perfect for what I need right now.  Awesome!

Pull this gist down and run it. There are demos of my maths library:

https://gist.github.com/HoraceBury/9431861

The actual library function you want is math.rotateTo()

Basically, the way I do it, is to assume a point at {x=0,y=1} and pass that to the rotateTo function. It returns that point rotated to the location you want and you can then multiply it to increase force or whatever.

If you have further questions post some code and I’ll see if I can fill in the blanks.

Hey Appletreeman,

using a math library is perfectly fine, but if you just want to have this single functionality use this function:

local function angle2VecDeg(angle) angle = angle\*math.pi/180 return math.cos(angle), math.sin(angle) end local vecX, vecY = angle2VecDeg(90) --vector of length one at 90 degrees

Greetings

Torben

Many thanks to you both.

@horacebury - That maths lib is brilliant.  Many thanks.

@Torben - Perfect for what I need right now.  Awesome!