Advertisement

2D Trig/physics (beginner)

Started by August 16, 2001 03:29 AM
1 comment, last by JimmyBGood 23 years, 6 months ago
I am playing around trying to learn basic stuff. I made a 2D game that has an airplane flying across the screen. What i need to do is add in gravity (like if you point down, it will accelerate, if you point the nose up, it will slow down) What i have so far doesnt do this. it keeps the same speed no matter what is happening: x = x + cos(pos) * 2 y = y + sin(pos) * 2 // now i use x and y to draw the position of the airplane pos is the amount of degrees the nose is pointed in 180 = left, 90 = down, 0 = right, 270 = up now how do i figure gravity, speed and velocity into all of this? thanks ahead of time
Adding plane speed and/or gravity is not difficult.

x=x+cos(pos)*plane_speed
y=y+sin(pos)*plane_speed-gravity

If you want to add gravity only for some values of Pos, then add if statements eg.

x=x+cos(pos)*plane_speed
if (Pos=90) or (pos=270) then
y=y+sin(pos)*plane_speed-gravity
else
y=y+sin(pos)*plane_speed

Remember to check if your cos/sin/other trig function accepts
degrees or radians. Standard C functions use radians, so if you use degrees, you have to convert
radians = (PI/180.0)*degrees.

Hope it will help you.

K.
Advertisement
The most realistic way of doing gravity (using physics laws and other schtuff) is like this:

x_pos, y_pos = locations of plane
x_vel, y_vel = velocity of plane along the axis
x_acc, y_acc = acceleration of plane along the axis

Since gravity is constant (for all practical purposes -- if you''re doing an earth-to-space thing, it''s not...), x_acc and y_acc will be constants. Of course, since there''s no gravity horizontally (though it''d be interesting -- you can do that for tractor beams ), x_acc = 0. y_acc will be some constant less than 0. Personally, I use -9.8 since that is the true gravity in meters per second.

You''re equations are going to look like this:

x_pos += x_vel * t + x_acc * t * t / 2
y_pos += y_vel * t + y_acc * t * t / 2
x_vel += x_acc * t
y_vel += y_acc * t

Notice that there''s a t in all of these. T stands for time in between successive calls. You can assume, however, that this set of 4 eq''s will run once every fraction of a second. So declare t to be some constant, and mess with it the results you want (i.e. the plane doesn''t spiral to the ground so incredibly slowly or incredibly quickly -- you die at the "perfect" time ).

Hope that helps.

~ Dragonus

This topic is closed to new replies.

Advertisement