Beginner question about Collisions

Hello!
I’ve started recently with game development, and also, with Defold.
After follow all the Starting tutorials, I’m trying to make some modifications in the Camera Example (from here).
The first thing that I tryied was to make the car collide with a box. So, in camera.collection, I create a new Game Object called box, and inside this, box_sprite and a Static Collision Object:

And the same thing for the car (player), but with Kinematic type.

In player.script, I made some changes on the on_message function:

function on_message(self, message_id, message, sender)
	if message_id == hash("contact_point_response") then
		print("collision")
	else
	    if message_id == hash("left") then
	        -- Interpolate the steering angle.
	        self.steer_angle = vmath.slerp(turn_speed, self.steer_angle, max_steer_angle_left)
	    elseif message_id == hash("right") then
	        -- Interpolate the steering angle.
	        self.steer_angle = vmath.slerp(turn_speed, self.steer_angle, max_steer_angle_right)
	    elseif message_id == hash("up") then
	        self.acceleration.y = 2
	    elseif message_id == hash("down") then
	        self.acceleration.y = -2
	    end
	 end
end

I’m getting the collision message from Debug, every time the car hits the box, but the problem it’s that the car pass through the box, and don’t stop…
Could someone please help me to understand what should I do to stop the car when hits the box?
I tried many things before post here, I’ve read the Collision topic in the Manual…
Sorry for this begginer question and for my english…

I’ll be grateful for any help you can provide!

Matheus

Do you have on your car collision object the mask box and the group car and in your box collisionobject the mask car and the group box?

Hello Raul!
Yes I have:

But since I’m getting the response from “contact_point_response”, I think my problem isn’t ‘configurations’ but what to do with this response, and I don’t know what to do to make the car hit and stop…

Thank you!

Ok, so if you have the collision object of the car set to kinematic it will mean that you will get contact_point_response and collision_response messages every time the collision object and the underlying Box2D physics engine detects a collision with a matching group. It will do nothing more. It will not resolve the collision for you in any way.

If you change it to dynamic it will mean that the physics system will take full control of the game object the collision object is attached to. It will automatically resolve collisions, but you will not be able to manually position the game object using go.get_position(). Instead you need to apply_force to get it to move.

Now, dynamic objects are useful for certain kinds of games, typically Angry Birds type of games (see this post) or a game such as the Defold game Stack the Stuff. Perhaps it could work for a car game as well, but it would require quite a bit of work to make the car behave as you want through the apply_force method. So, this means we’re back to kinematic objects and resolving the collisions ourselves. If you pprint(message) when you receive a contact_point_response message_id you’ll see something like this in the console:

{
  normal = vmath.vector3(-1, 0, 0),
  position = vmath.vector3(298.53924560547, 171.23937988281, 0),
  other_position = vmath.vector3(0, 0, 0),
  relative_velocity = vmath.vector3(0, 0, 0),
  other_id = hash: [/level],
  other_mass = 0,
  group = hash: [ladder],
  applied_impulse = 0,
  distance = 9.9040031433105,
  life_time = 0,
  mass = 0,
}

You will get one such message per contact point and with this information we can manually adjust the position of the car in such a way that the car and whatever it collided with no longer overlap. If you do like this you’ll see that the objects detach:

go.set_position(go.get_position() + (message.normal * message.distance))

Depending on your game you may wish to do something more complex. An example of this can be seen in the Getting Started Tutorial, Step #4.

PS I would also recommend that you read the entire chapter on Physics in our manual to get an even better understanding of the different types of collision objects.

3 Likes

This is exactly what I’m searching for!
Works very well, and I think now I got it how collision works!

The only thing that happens now, is that the ‘bounding box’ (collision shape) isn’t in the right place, but a little more to the right… And the Box sprite make some ‘blinks’ (shaking) on every game screen update, when the camera moves…

But your answer helped me alot! Now I can continue practicing this and testing some other things…
Thank you!