Getting force from an angle? [Solved]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Freonce
Prole
Posts: 8
Joined: Tue Jul 27, 2010 6:26 pm

Getting force from an angle? [Solved]

Post 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:
Last edited by Freonce on Thu Aug 19, 2010 8:19 pm, edited 1 time in total.
User avatar
Tesselode
Party member
Posts: 555
Joined: Fri Jul 23, 2010 7:55 pm

Re: Getting force from an angle?

Post 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.
User avatar
knorke
Party member
Posts: 238
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

Re: Getting force from an angle?

Post 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)
....
....
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Getting force from an angle?

Post 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 :(
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
knorke
Party member
Posts: 238
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

Re: Getting force from an angle?

Post by knorke »

vrld wrote:Although this too fails to explain why the rotation works :(
what do you mean?
Freonce
Prole
Posts: 8
Joined: Tue Jul 27, 2010 6:26 pm

Re: Getting force from an angle?

Post 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.
Freonce
Prole
Posts: 8
Joined: Tue Jul 27, 2010 6:26 pm

Re: Getting force from an angle? [Solved]

Post by Freonce »

Everything is working alright now. Just have some velocity bugs I need to work out. Thanks again everyone!
:awesome:
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 29 guests