I’m doing a platform game and the character is going through a static collision
Do you have the Group and Mask set up correctly?
1 Like
Does the collision objects look correct?
1 Like
Do you have collision code attached to the same game object?
Yes
@britzl Yes
@britzl i active the debug mode and…
The character not collide with the ground : (
Compare the working sample with your project and examine the changes you have made.
You have probably misconfigured the group and mask of the collision objects.
1 Like
@britzl remade the collision groups that the tutorial said and nothing
Post your collision code
This is the player code:
function init(self)
msg.post(".", “acquire_input_focus”)
self.moving = false
self.input = vmath.vector3()
self.dir = vmath.vector3(0, 1, 0)
self.speed = 100
self.ysp = 0
self.correction = vmath.vector3()
end
function update(self, dt)
if self.moving then
local pos = go.get_position()
pos = pos + self.dir * self.speed * dt
go.set_position(pos)
end
self.correction = vmath.vector3()
self.input.x = 0
self.input.y = 0
self.moving = false
self.ysp = self.ysp - 5
local pos = go.get_position()
go.set_position(pos + vmath.vector3(0, self.ysp, 0) * dt)
if self.moving then
local pos = go.get_position()
pos = pos + self.dir * self.speed * dt
go.set_position(pos)
end
end
function on_message(self, message_id, message, sender)
-- Handle collision
if message_id == hash("contact_point_response") then
-- Get the info needed to move out of collision. We might
-- get several contact points back and have to calculate
-- how to move out of all of them by accumulating a
-- correction vector for this frame:
if message.distance > 0 then
-- First, project the accumulated correction onto
-- the penetration vector
local proj = vmath.project(self.correction, message.normal * message.distance)
if proj < 1 then
-- Only care for projections that does not overshoot.
local comp = (message.distance - message.distance * proj) * message.normal
-- Apply compensation
go.set_position(go.get_position() + comp)
-- Accumulate correction done
self.correction = self.correction + comp
end
end
end
end
function on_input(self, action_id, action)
if action_id == hash('Up') then
self.input.y = 1
print("up")
elseif action_id == hash("left") then
self.input.x = -1
print("left")
elseif action_id == hash("right") then
self.input.x = 1
print("right")
end
if vmath.length(self.input) > 0 then
self.moving = true
self.dir = vmath.normalize(self.input)
end
end
function final(self)
msg.post(".", "release_input_focus")
end
Share the project as a zip file (exclude build and .internal folders)
1 Like