Beginner question about Collisions

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