1
\$\begingroup\$

I want to make a box "fly" so while user press D it's add thrust then on A it decrese untill 0, W and S control the direction of my box.

I have problems after I add the gravity to update correctly my X and Y position. My actual code is like this: (I'm using gamemaker but it's same as other languages, came execute 60 frames per second)

frametime = delta_time / 1000000;
spd = sqrt(power(velx ,2)+power(vely ,2));
tempgy = g * frametime ;
//tempay = ((thrust / m) * 32) * frametime; // or my box procede diagonally offcurse
tempax = ((thrust / m) * 32) * frametime;
y -= (vely + tempgy/2   + tempay/2 ) * frametime ;
x += (velx + tempax/2  ) * frametime ;
vely += tempgy + tempay; 
velx += tempax  ; 

m is mass of object = 500

g = 9.81

32 it's pixel per meter

So, basically I calculate the acceleration on gravity, and need to apply acceleration to x and y based on direction if I not understand wrong. So for the X component of thrust I have to multiply sin(degtorad(dir)) and for the Y must multiply cos(degtorad(dir)), but I have to apply the sin and cos only on acceleration?

I think I am doing it wrong because if I use g=0 and just using thrust my box act not as intended (ex: If I try to do a loop and stop accelerating my box will not follow the loop direction but continue on straight line)

Sorry for the stupid question but I really not know how to fix it. I see lot of tutorials with vectors but it's not supported here so can't use them. Thank you and best regards.

\$\endgroup\$
1
\$\begingroup\$

Your code seems to be using the formulas correctly, but let us check again what we want to do :

We want to update the position of our object. To do this we use the speed.

x += vx * frametime;
y += vy * frametime; 

And we want to update the speed of our object, using acceleration

vx += ax * frametime;
vy += ay * frametime;

Now how to calculate the acceleration a?

enter image description here

The acceleration is just the composition of the gravity and the thrust as you can see on my beautiful drawing above.

So we have

a.x = g.x + t.x;
a.y = g.y + t.y;

Now g.x = 0 and g.y = -9.81 (if going down means lowering y), so all we need is t.x and t.y, which can be easily calculated with basic trigonometry. Note that on my drawing I have put the angle compared to the floor, it is possible that you used the other one which means you need to reverse the sin and cos.

\$\endgroup\$
  • \$\begingroup\$ Thank you for the drawing you did, I think u wrongly type the g.x= -9.81. I still not sure what I am doing wrong but I'd like to ask you one question. \$\endgroup\$ – plaguebreath Feb 14 '17 at 15:43
  • \$\begingroup\$ Suppose I had uncommented the tempay part, then I write on the x and y part for the position: y -= (vely + tempgy/2 + tempay/2 ) * frametime * sin(degtorad(dir)) ; x += (velx + tempax/2 ) * frametime * cos (degtorad(dir)); and we not consider the gravity (I know already the formula is wrong) Now I start with thrust of 10 and start to turn, my plane turn, then suddenly set 0 to thrust, my plane not turn any more but just continue on his way rolling on himself. That's wrong, but why ?? \$\endgroup\$ – plaguebreath Feb 14 '17 at 15:51
  • \$\begingroup\$ If you do not take the gravity into account, if you stop the thrust then the total acceleration will be 0 and the plane will keep moving in the same direction, it is normal. I do not get what you mean about the plane rolling on itselft though. (I indeed made a typo, edited my answer) \$\endgroup\$ – realUser404 Feb 14 '17 at 16:00
  • \$\begingroup\$ so, if without thrust even changing the direction of fly my box will continue to fly on the last direction before ending the thrust ? I was thinking that, without gravity and friction if my box use 'flap' will change the direction of fly and start to loop with the velocity he got untill stop thrusting .... \$\endgroup\$ – plaguebreath Feb 14 '17 at 17:05
  • \$\begingroup\$ Sorry the fix was like this: tempgy = g * frametime ; // acceleration of gy tempay = ((thrust / m) * 32) * frametime ; tempax = ((thrust / m) * 32) * frametime ; y -= (vely + tempay/2) * frametime ; // update position y -= tempgy/2 * frametime; x += (velx + tempax/2 )* frametime ; vely += tempgysin(degtorad(270))*sign(tempgy) + tempaysin(degtorad(dir))*sign(tempay); // update speed velx += tempax*cos(degtorad(dir)); \$\endgroup\$ – plaguebreath Feb 14 '17 at 17:11
0
\$\begingroup\$

I'm not a good physicist, but I'll try to help a little bit.

There are some problems with understanding, what you're really want, but let's start with basic idea of movement and gravitation.

And, of course, we need the beatiful vector algebra for that.

What is a vector?

Vector is a kind of number, but with additional value - direction. Two vectors can be compared by value and direction.

Why vector algebra?

As in natural numbers, there are some operations, which can be applied to vectors. We will see later. If you have a rigid body and you want to apply gravitation on it, than you should just create a vector with value 9.81 pointing down to the earth. I will try to explain the last sentence in details.

Explanation

The gravitation is a force so it can be represented as vector (as I've said previously). The main point is that there are some another things, that can be represented as vector aswell: horizontal movement (your thrust), jumps and so on.

Also there is a little hack in vector algebra: we can represent a lot of vectors as one with calculated value and direction (this is called the sum).

Another important thing is that every vector can be splitted onto the X-part and the Y-part (corresponding axle projections). For velocity this will be your vertical and horizontal components of it. In other words: the values to apply at every frame to the body.

The recipe

  1. Calculate the vector of gravity (direction - negative part of y-axis, value - 9.83).
  2. Calculate the vector of horizontal movement (thrust, direction - positive part of x-axis, value - thrustPower).
  3. Calculate the vector of vertical movement (direction - positive part of y-axis, value - jumpPower).
  4. Get the sum of vectors (this is the best part), so from three vectors we will get one.
  5. Calculate y-component (suppose it is represented as yVelocity) and x-component (xVelocity) of sum.
  6. Apply the values to the body: newX = oldX + xVelocity and newY = oldY + yVelocity.

Links:

  1. https://www.mathsisfun.com/algebra/vectors.html
  2. https://en.wikipedia.org/wiki/Unit_circle
\$\endgroup\$
  • \$\begingroup\$ thank you for the comment, so basically I'm doing it wrong offcurse, I'm not good in algebra nor on physics but I will try to understand it. \$\endgroup\$ – plaguebreath Feb 13 '17 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.