Hi, I am practicing to a game. I have a ball and a ground. Both of them have collisionobject kinematicly, i made it kinematic because they are moving. Anyway, I make the ball jump and when it touches ground it should stop. However, sometimes it stops instantly and sometimes it passes through the ground. Here an example video and the code block that I wrote. Waiting for your help. Thanks.
local gravity = -70
local jump_takeoff_speed = 1500
function init(self)
msg.post(".", "acquire_input_focus")
self.velocity = vmath.vector3(0, 0, 0)
self.ground_contact = false
self.is_jumping = false
end
function final(self)
msg.post(".", "release_input_focus")
end
function update(self, dt)
local gravity = vmath.vector3(0, gravity, 0)
if not self.ground_contact then
self.velocity = self.velocity + gravity
go.set_position(go.get_position() + self.velocity * dt)
end
if self.is_jumping then
go.set_position(go.get_position() + self.velocity * dt)
end
self.ground_contact = false
self.is_jumping = false
end
function on_message(self, message_id, message, sender)
if message_id == hash("collision_response") then
self.ground_contact = true
end
end
local function jump(self)
-- only allow jump from ground
if self.ground_contact then
self.velocity.y = jump_takeoff_speed
self.is_jumping = true
end
end
local function abort_jump(self)
-- cut the jump short if we are still going up
if self.velocity.y > 0 then
self.velocity.y = self.velocity.y * 0.5
end
end
function on_input(self, action_id, action)
if action_id == hash("jump") or action_id == hash("touch") then
if action.pressed then
jump(self)
elseif action.released then
abort_jump(self)
end
end
end