Hello everyone,
So I have followed tutorials and read through the docs. In doing so I was trying to implement collisions between my Kinematic Player and the Tile Map. I am new to defold so excuse me if the solution ends up being a simple one.
My Player game object has a collisionobject that has a box shape.
My collision handling code for the player looks like this:
local function handle_geometry_contact(self, normal, distance)
-- project the correction vector onto the contact normal
-- (the correction vector is the 0-vector for the first contact point)
local proj = vmath.dot(self.correction, normal)
-- calculate the compensation we need to make for this contact point
local comp = (distance - proj) * normal
-- add it to the correction vector
self.correction = self.correction + comp
-- apply the compensation to the player character
go.set_position(go.get_position() + comp)
-- check if the normal points enough up to consider the player standing on the ground
-- (0.7 is roughly equal to 45 degrees deviation from pure vertical direction)
if normal.y > 0.7 then
self.ground_contact = true
end
-- project the velocity onto the normal
proj = vmath.dot(self.velocity, normal)
-- if the projection is negative, it means that some of the velocity points towards the contact point
if proj < 0 then
-- remove that component in that case
self.velocity = self.velocity - proj * normal
end
end
function on_message(self, message_id, message, sender)
if message_id == hash("contact_point_response") then
print("we are colliding!")
-- check if we received a contact point message. One message for each contact point
if message.group == hash("default") then
handle_geometry_contact(self, message.normal, message.distance)
end
end
end
I have created a tilesource with my square image for floors. I added a tilemap as well. The tilemap references the tilesource and the tilesource I âpaintedâ or assigned the collision group named âdefaultâ.
My masks and groups for collisions are matching for both player and tilesource (where playerâs group is âplayerâ and mask is âdefaultâ and vice versa for the tilemap).
The collision shape assigned in the tilesource is the same placeholder image. All âZâ values are set to â0â for player and tilemap.
Finally I created the tilemap game object and included both the player and tilemap in the main collection. However my player is not colliding with the tilemap for some reason. I tried selecting the physics debug tool and I do not see a collider on the tilemap at all in game. Also my code in the player script has a console print message if it has any collisions but isnât printing that there are collisions. When I look back at the tilesource and my collision shape, it makes a colored square border for the tile. I am not sure at this point what could be causing the collision to not register. I briefly changed the tilemap physics to âDynamicâ to see if it would be affected by physics at least and it did fall, but still not sure why collisions arenât working.
Let me know if there is anything more I can share that might help debug the issue!