Is there a way to constantly check if two game objects are touching each other?

#1

I am making a 2d open map game. In my game I need the main character to be able to interact with NPC’s around the map. Right now I am using this code to track when the NPC and the main character are touching and when the player hits the interact key (enter):

function on_message(self, message_id, message, sender)
if message_id == hash("collision_response") then
	self.aretheytouching = true
end

function on_input(self, action_id, action)
if action_id == hash("enter") then
	if self.aretheytouching then
		msg.post("/testbox", "enable")
	end
end

end

The test box is what I want to trigger when they touch and interact.

The issue I have is once the player touches the NPC once, self.aretheytouching is always true, so if they player touches the NPC and walks away, they could still cause the textbox to enable. I am wondering if there is a better way to track if the NPC and player are touching (maybe a timer of sorts).

Thanks in advance!

0 Likes

#2

Yes, there are a couple ways to do this off the top of my head:

If you’re using trigger collision type, then you can keep a flag in your script that is set to true when you receive “message.enter = true” on the “trigger_repsonse” message. You can then set it to false when you receive “message.enter = false”.

If you are using a kinematic collision type, then you can keep a flag in your script that is set to true when you receive “message.other_group = hash("npc")” on the “collision_response” message. Each frame, you could check if you received this same message. If a frame passes by where the message was not received, then set your flag to false.

If you are using a kinematic collision type on both the player and the npc and do not want to check if you received a particular collision_response message every frame, you can also add a secondary collision object to your player of type trigger. You can then use the message.enter field from this collision object to query the world.

2 Likes

#3

How do I check this every frame?

0 Likes

#4

With collisions you don’t need to check it every frame, the physics engine does it for you and you will get a message, every time when a collision appears, so you can respond to such event. You can start with trigger collisions:

If you really need to do something (but please, not this) every frame, there is update function, but be careful, it should be quick and truth is that everything which can be done not every frame shouldn’t be in here.

If you are more advanced, you can check out AABB trees, for example @selimanac 's https://defold.com/assets/daabbcc/ for collisions instead of builtin physics.

2 Likes

#5

Trigger objects are perfect for this kind of thing!

Exactly. React to the messages from the physics system and avoid having code running every frame.

When you get the trigger enter message you show the NPC dialog and then you hide it when you get an exit message.

1 Like

#6

I would add an extra “trigger” type body to your character that responds to NPCs and other interactible objects. Triggers get enter and exit messages, so it’ll be easy to track things. Also, with an extra trigger shape, you can control exactly how close you need to be to the NPC, you don’t always have to be actually hitting the NPC to interact with them, which makes the useability a lot better.

3 Likes