Page 1 of 1

Getting force from an angle? [Solved]

Posted: Tue Aug 17, 2010 9:11 pm
by Freonce
Hey there. I'm trying to start my second game using Lua and I decided to make a lunar lander game. It's going alright so far, but the main area I'm having trouble with is getting a correct force applied to my ship depending on its angle. Can anyone point me in the right direction as to what I could (or should) be using.

I'm using body:getAngle to get the angle of the ship and I'm just multiplying that by 70 to get a decent x value for the force I want to apply (probably not the best way to go about it ). I can't seem to find any way to turn the angle into a proper y value though.

Bring on the math, and thanks in advance for any help.
:awesome:

Re: Getting force from an angle?

Posted: Tue Aug 17, 2010 9:36 pm
by Tesselode
Well I think you can use tangent to get the slope. Don't ask me; I'm not a trig expert. I'm still not sure how you would change a slope into proper x and y values though.

Re: Getting force from an angle?

Posted: Tue Aug 17, 2010 10:06 pm
by knorke
you should play Astropatrolonium. Its a great game.
http://love2d.org/forums/viewtopic.php?f=5&t=1716
Then look at its code where it says
----ship movement physik----
in function love.update(dt)
then a few lines down you find:

Code: Select all

	
....
...
	if (p.want_up) then 
			local thrust_fx = -sin(p.b:getAngle()) *  ship_acc
			local thrust_fy = -cos(p.b:getAngle()) *  ship_acc
			p.b:applyForce (thrust_fx, thrust_fy)
....
....

Re: Getting force from an angle?

Posted: Tue Aug 17, 2010 10:53 pm
by vrld
If you want to know why, I recommend reading the "Linear algebra for game developers" by wolfire.
Here is Although this too fails to explain why the rotation works :(

Re: Getting force from an angle?

Posted: Tue Aug 17, 2010 11:17 pm
by knorke
vrld wrote:Although this too fails to explain why the rotation works :(
what do you mean?

Re: Getting force from an angle?

Posted: Wed Aug 18, 2010 6:59 pm
by Freonce
Awesome!
Thanks for all the replies. I think that little snippet of code is just want I need. I was trying to use math.atan2 for some reason... :P I'll be sure to read those links as well. Understanding the why is a key part to learning all this, and having it stick.

I'll post an update when I get my ship working.

Re: Getting force from an angle? [Solved]

Posted: Thu Aug 19, 2010 8:20 pm
by Freonce
Everything is working alright now. Just have some velocity bugs I need to work out. Thanks again everyone!
:awesome: