How make an object stop when colliding with a wall?

Im using a kinematic game object and I want to make a impassible wall. How would I make it so a object can get right up to the wall but walk away in the other direction. Acting simply like a wall.

You need to use the contact_point_response messages that your kinematic object will receive in on_message() when it collides with the wall (if you have set up the collision objects on the wall and player to interact). The message contains the collision normal (ie direction of the the collision) and distance (ie how much overlap there is). You can use these two values to separate the game objects. Naive approach:

function on_message(self, message_id, message, sender)
	if message_id == hash("contact_point_response") then
		go.set_position(go.get_position() + message.normal * message.distance)
	end
end

You can see this naive approach in action in this example: https://github.com/britzl/publicexamples/tree/master/examples/rotate_and_move

The above works but can introduce jitter when colliding with multiple objects in a single frame. A better approach is describe in the platformer tutorial:

4 Likes

Theres this formula u can just copy and paste and add to ur game object in the resolving kinematic collision section in the tutorial that works for anything in here https://www.defold.com/manuals/physics/

1 Like

Hello, I want to use kinematic collision object to block the movement of the character. And it stops but the problem is that if I press the input enough times it teleports through the wall.

Here is my code, please help me out I am really stuck with it.

When you correct the player’s position in on_message(), you’re not updating self.pos with the new position.

2 Likes

Thank you, finally works :slight_smile: