# Making gravity, vspeed, hspeed, speed from scratch

### #1

Posted 13 February 2013 - 09:59 AM

My question is, where do I start? I actually accomplished to make a little gravity-system, but my problem is how everything is connected. When I move my plane upwards, I have to apply the vspeed to the speed somehow, and it just confuses me A LOT. (I've experimented with sin/cos/tan to affect the different speed-variables, but I can't seem to make it right..)

The reason I'm asking here, is because I've used GM a lot, and hope that someone can explain me as simlpe as possible how GM connects it all together, and how to recreate it.

Do I set angle and speed manually, and then calculate the vspeed and hspeed from that? If so - How do I apply the gravity which affects only vspeed? And if I apply it to vspeed, it won't apply to the "speed"-variable. See why I'm going crazy?

Thanks a lot for reading and helping.

### #3 Guest_Experimenator_*

Posted 13 February 2013 - 11:07 AM

Let us start:

First there are 4 things that are defining how the object will behave:

*force*>>

*acceleration*>>

*speed*>>

*position*

Each affects the other. (GM doesn't use force, so leave it)

And also you need to know that there are 2 kinds of vectors:

**polar**and

**cartesian**.

**Polar**: where you have a strength and a direction (Like: speed and direction)

**Cartesian**: where you have the coordinates where the vector finishes, or you can say, you have the strength of both X and Y vectors (Like: hspeed and vspeed, x and y)

Like this:

Cartesian is more efficient because Polar uses trigonometric functions.

Cartesian is for when you want to add a vector or something while

Polar is when you want to rotate the vector.

Now, you need to know how to convert them to eachother

//Polar to Cartesian: example speed = 5; direction = 30; //now converting to hspeed and vspeed (Euler equation) hspeed = cos(direction*pi/180)*speed; vspeed =-sin(direction*pi/180)*speed;

//Cartesian to polar: example hspeed = 4; vspeed = 3; //now converting to hspeed and vspeed (pitagorahs theorem) speed = sqrt(hspeed*hspeed + vspeed*vspeed); //point_distance(0,hspeed,0,vspeed); direction = arctan2(vspeed,hspeed)*180/pi; //point_direction(0,hspeed,0,vspeed);If you find trigonometry too hard, just search a little in the internet to learn, or you can just copy this without caring so much.

The question which representation will you use mainly is up to you, but I recommend you Cartesian because it's easier and faster.

Now remember when I said about what vector affects the other?

this comes alive now.

force>>acceleration>>speed>>position

Each affects the other. (GM doesn't use force, so leave it)

First, you have a

**position**of an object (where it is in space), and it is normally represented in Cartesian vector (x,y)

x=4; y=7;Second, you have

**speed**which affects the

**position**(changes it)

//like this x+=hspeed; y+=vspeed; //Or in polar (remember the convertion): x+=cos(direction*pi/180)*speed; y-=sin(direction*pi/180)*speed;Third you have the

**acceleration**(like

**gravity**) which affects the

**speed**.

hspeed+=hgravity; vspeed+=vgravity; //or when gravity in polar: hspeed+=cos(gravity_direction*pi/180)*gravity; vspeed+=sin(gravity_direction*pi/180)*gravity; //or everything in polar (Used Cartesian as main vector representation ): hspeed+=cos(gravity_direction*pi/180)*gravity; vspeed+=sin(gravity_direction*pi/180)*gravity; speed = sqrt(hspeed,vspeed); //point_distance(0,hspeed,0,vspeed); direction = arctan2(vspeed,hspeed)*180/pi; //point_direction(0,hspeed,0,vspeed);Force is just making acceleration being affected by the mass (but it's not used in GM):

acceleration = Force/mass;

Also what might come useful is to know how the point_* functions work

point_distance(x1,y1,x2,y2) = sqrt((x2-x1)*(x2-x1)+ (y2-y1)*(y2-y1)); point_direction(x1,y1,x2,y2) = arctan2(y2-y1,x2-x1)*180/pi;You can replace where I used normal functions above in the post with these functions if it's easier for you.

**Edited by Experimenator, 13 February 2013 - 11:21 AM.**

### #4

Posted 21 January 2014 - 11:56 PM

Sorry to resurrect such an old thread, but I was looking for information on this topic, and found this quite helpful. However, I do have a question:

In gm you can sort of use the cartesian and polar methods mentioned above together. For example: I increment my hspeed/vspeed using a variable, accel, set to .25, but I limit my total speed to 2.

if keyboard_check(vk_up) vspeed -= accel;

if keyboard_check(vk_down) vspeed += accel;

if keyboard_check(vk_left) hspeed -= accel;

if keyboard_check(vk_right)hspeed += accel;

if speed > 2 speed = 2;

Setting the speed variable like this directly changes the vspeed and hspeed variables (and the other way around). So, outside of game maker, or even in game maker itself, if say, I wanted to create a custom set of variables that work like vspeed, hspeed, and speed (this would be useful because the aforementioned variables move an object automatically, giving you less control), how would you program that?

[EDIT]

I've been playing around with this in game maker, and not having a lot of luck. Right now I'm using xspeed and yspeed as stand-ins for hspeed and vspeed, again, changing them with a variable called accel, set either to .25 or .125 (testing different settings). Right now I don't know how to affect them both with a single variable the way speed affects hspeed and vspeed, so what I have right now is:

x+=lengthdir_x((xspeed*sign(xspeed)),(point_direction(x,y,x+xspeed,y+yspeed)));

y+=lengthdir_y((yspeed*sign(yspeed)),(point_direction(x,y,x+xspeed,y+yspeed)));

So it's using the xpseed variable to set the distance, and a vector from xspeed and yspeed to set the direction. This creates some problems when compared to the built-in variables.

In the above example with hspeed, vspped, and speed, I have speed capped at 2. If I am moving up and right (at a 45 degree angle), hspeed will be equal to sqrt(2) (~1.41), and vspeed will be ~-1.41. To go from a 45 degree angle (up and right) to a 135 degree angle (up and left), x will move from ~1.41 to ~-1.41, or a total distance of ~2.82. This will take ~11 steps at an accel of .25, or ~23 at .125. In my method, xspeed and yspeed are not directly affected, so the vector for up and right is (2,-2), and for up and left is (-2,-2). (assuming full speed, 45 degree angle to 135 degree angle. The total distance for x to travel for the new angle is 4, which will take 16 steps at accel=.25, and 32 steps at accel=.125.

Of course, the faster I move, and/or the slower I accelerate, the greater the difference between the vspeed/hspeed method and my feeble attempts to recreate it.

I attempted this to create a new variable to store the hypotenuse and direction using the formulas in Experimenator's post, and use them with my new variables like this, but it didn't work (not that I thought it would, but you can't learn without trying):

**Edited by VampireJesus, 22 January 2014 - 03:59 AM.**