I'm unsure if I should ask the mathematic part of StackOverflow or the gamedev part. I'm trying to figure out how the gravity changes the position of an element in a 3D world. I've been trying to rescript the gravity in a game so I could attach it to every element I want.

As far as I know; The movement of the player has something to do with the current position ( obviously ), the velocity, the gravity and Delta Time. As far as I'm concerned too, I know that when an element makes some kind of jump, he should go up since the velocity is still positive. Each frame should update the player's position, depending on the current position and velocity, the new position will be created. I've been able to create it in a platformer ( 2D ) but I haven't succeeded to do this in a 3D game. As far as I came, the object just flew into the air at a massive speed and ended up landing in another galaxy..

I'm currently working with Lua, if you were trying to explain something in a language. If you're working with another language and want to give me an example in that.. feel free to do so. I'm known with a lot of languages ( programmer here. ). It's just about the logic behind it, the actual math.

Thanks in advance

  • 1
    \$\begingroup\$ Something like this? gamedev.stackexchange.com/questions/45704/… If you can do it for 2D, you can do it for 3D. It's the same idea, you just do it for another axis. \$\endgroup\$ – MichaelHouse May 10 '16 at 19:38
  • \$\begingroup\$ Your issue is that you can do it correctly in 2d but not in 3d, is that it? \$\endgroup\$ – Vaillancourt May 10 '16 at 19:40
  • \$\begingroup\$ Yes, that's actually the issue. Once I get more than 2 different axis', I just majorly mess up. :P I'll give that one a try @Byte56 \$\endgroup\$ – Tosfera May 10 '16 at 19:41
  • 2
    \$\begingroup\$ You literally just do the same thing for each axis. Calculate the forces on the X axis, update the velocity for the X axis, update the position for the X axis. Then do the same for Y and the same for Z. That's the beauty of using cartesian coordinates. \$\endgroup\$ – MichaelHouse May 10 '16 at 19:44

It's the same as you'd do it for 2D, just with an extra axis. Use the same exact equations you do for a 2D game, something similar to the equations of motion or the kinematics equations.

  • For each axis (X, Y, Z)
    • Sum the forces for that axis (gravity, jump force, weapon knockback, etc. Keep in mind, some of these forces will be 0 on some of the axis, like gravity is only likely to affect one axis)
    • Calculate the velocity for that axis (force for that axis * delta time)
    • Calculate the new position for that axis (velocity for that axis * delta time)

After all these calculations have been done, you'll have a new velocity and position in 3D space. All three of these values can be stored in something like a vector3. This is just an object that contains three scalar values, in this case, one for each axis. Each calculation only affects the axis you're currently working with. i.e. force.x -> velocity.x - > position.x.

Simply removing one of these axis would result in your object being restricted to movement in a 2D plane. Removing two axis results in movement along a single line. All the axis still work, independent of each other.

  • \$\begingroup\$ Got the axis working, I just need to make the velocity dynamic. Mind checking my updated question? \$\endgroup\$ – Tosfera May 12 '16 at 19:11
  • 1
    \$\begingroup\$ You should remove that update and ask it as a new question. That will allow others to answer and keep this question/answer clean. \$\endgroup\$ – MichaelHouse May 12 '16 at 19:39
  • \$\begingroup\$ For those who want to follow the rest of the question; gamedev.stackexchange.com/questions/121354/… \$\endgroup\$ – Tosfera May 12 '16 at 19:47

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.