How to tell when collision is finished

I have another Lua question.

I am using kinetic collisions in my top-down grid based game, and I’m not really sure on the best way to tell when a collision has finished.

Each of the solid blocks in my game has four collision shapes, each in a different group. The groups are called up, down, left, and right. If player is colliding with left, it means it is to the left of a wall, and therefore cannot move right.

At the moment I have this:

function on_message(self, message_id, message, sender)
  if message_id == hash("contact_point_response") and if message.group == hash("left") then
touchingleft = true

and in my movement code:

elseif direction == "r" and not touchingleft then 
pos.x = pos.x + speed
go.set_position(pos)

But the obvious problem is that touchingleft is never set to false again. Any help? (when I change the type to “trigger”, I don’t get any contact_point_response messages, so that’s not worked for me)

It should be ok to reset touchingleft at the end of every update() call. This is the same as how the ground contact variable is handled in the getting started tutorial.

Britzl saves the day again. Although, I just realised that the reason that I can use triggers if i use “collision_response” instead of “contact_point_response” which is another easy way… I’ll try both. Thanks!

Well, neither of these seem to work for me. I’m still investigating Britzl’s suggestion, but the triggers seemed to be a no starter. Is this code correct? I can’t

function on_message(self, message_id, message, sender)
if message_id == hash("collision_response") and message.group == hash("up") and message.enter then 
touchingup = true
print(message.enter)
elseif message_id == hash("collision_response") and message.group == hash("up") and not message.enter then 
touchingup = false
print(message.enter)
end

Because for me message.enter always returns nil.

Okay, the correct code is with trigger_response rather than collision_response

function on_message(self, message_id, message, sender)
if message_id == hash("trigger_response") and message.group == hash("up") and message.enter then 
touchingup = true
print(message.enter)
elseif message_id == hash("trigger_response") and message.group == hash("up") and not message.enter then 
touchingup = false
print(message.enter)
end

Thanks, self!

1 Like

… AAAAAAAaaaaand I’m stuck again.

I currently have collisions triggers that change four variables (touchingup, touchingdown… etc) to true or false depending on whether the collision is currently happening. They are working fine if I print those variables. However, the following code doesn’t work as I would expect.

The idea is that if "touchingdown"is true, the player is touching the lower part of a wall, and therefore can’t move up. But player should still be able to move down, left, or, right.

The problem that I have is that if “touchingdown” (or any of the other other touching variables) are true, none of the movement works at all.

code:

if direction == "u" and touchingdown == false then 
pos.y = pos.y + speed
go.set_position(pos)
elseif direction == "d" and touchingup == false then 
pos.y = pos.y - speed
go.set_position(pos)
elseif direction == "l" and touchingright == false then 
pos.x = pos.x - speed
go.set_position(pos)
elseif direction == "r" and touchingleft == false then 
pos.x = pos.x + speed
go.set_position(pos)
end
end

any ideas?

It’s impossible to give advice with that snippet of code. It would be easier to help if we could see how touchingleft/right/up//down are changed.

What conclusions can you draw when your print the different values?