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
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
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 ?
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.