How to code: Gravity (SOLVED)

Having trouble making gravity work in the game.
Planning to make a 2D platform game where different playable characters
( One with wings and one without wings ) fall at different speeds )

I have seen some codes in the examples, I would like to understand the codes rather then just copying them. Would appreciate anyone who is willing to explain them

Thanks for reading

1 Like

Gravity only affects dynamic objects. If you’re using kinematic, you’ll need to program it yourself.

If you’re making a platformer then I’d recommend kinematic physics. If you wish to create simple platformer physics then keep track of a vector3 with the player velocity.

JUMPING
When the player presses a jump action/key add a positive value to the y component of the velocity.

GRAVITY
Every frame in update() you need to simulate gravity by adding a negative value to the y component of the velocity and multiply it by dt (delta time). This will ensure that you always apply the same amount of gravity per second.

FALLING
Every frame you also need to get the position of the player and add the velocity multiplied by dt and then update the player position with the new position.

COLLISIONS
Since you’re using kinematic physics you need to deal with collisions yourself. Read the Tutorial for details on this

4 Likes

i had troubles understanding kinematic, and this thread helped me a lot:

I have tried to understand the code from the tutorial but I am having trouble with a few parts of it. Particularly the:

“overshoots and you will see jitter in many cases”

After reviewing the code again the game my character responds to the ground but seems to be shaking constantly while in contact with it. I even tried to just copy the whole thing instead of writing it out and the shaking still happends

Are there any additional tutorials or learning resources I can read though to get a better understanding ?

Please share the code/project and we’ll take a look.

function init(self)
    msg.post(".", "acquire_input_focus") 
    self.vel = vmath.vector3()     
end

function update(self, dt)
    local pos = go.get_position() 
    pos = pos + self.vel * dt 
    go.set_position(pos) 
    
    self.vel.x = 0 
    self.vel.y = 0
    
    self.vel = vmath.vector3(0,-10,0)
end

function on_message(self, message_id, message, sender)
  -- Handle collision
  if message_id == hash("contact_point_response") then
    local newpos = go.get_position() + message.normal * message.distance
    go.set_position(newpos)
  end
end




function on_input(self, action_id, action)
    if action_id == hash("up") then
        self.vel.y = 80
    elseif action_id == hash("down") then
        self.vel.y = -80
    elseif action_id == hash("left") then
        self.vel.x = -80
    elseif action_id == hash("right") then
        self.vel.x = 80
    end
end

This code seems to work but.
The players seems to stick to the walls as well as the floor and cant seem to get away from them.

I tried to apply the projection physics code to rectify this issue but got an error

ERROR:SCRIPT: main/Script/test.script:26: bad argument #1 to ‘project’ (vector3 expected, got nil)

Is the code you shared above actually from test.script? Do you have multiple scripts running? I don’t see any function call to vmath.project() (which I’m guessing that the error is referring to) in your shared code.

After going though multiple forums I have found a code that works
This question is now “SOLVED” :smiley: !

Would you mind sharing your solution to help others?

1 Like

I learned how by reviewing Mr Mikael Saker’s video called Jump frog jump in his tutorial series again

3 Likes