[SOLVED] How to use collisions to make objects collide and stop?

I have a screen border collision shape and a player with three sphere collision shapes.

The player is in a group called “player” and can detect collisions with the “screen” group. Likewise, the screen border is in a group called “screen” and can detect collisions with the “player” group.

The player can move in all directions with no gravity affecting it, like a top-down game.

I want to stop the player from leaving the screen.

I set the screen collision object to Static and the player’s to Kinematic, but the player is still able to leave the screen.

I tried combing the Getting Started tutorial to find a way to make objects collide and stop, but I had no luck.

How do I get my collisions to work? Is there an easier way to do this?

1 Like

Kinematic collision objects will register collisions but it is up to you to solve the collision, i.e. decide how to handle it.

This is described here:

Dynamic collision objects are 100% solved by the physics engine. However, this means that you cannot manipulate the object directly but have to push the object indirectly with forces.

Forces are applied with the “apply_force” message:

3 Likes

Hi GameDevSITHS,

Sicher’s method is perfectly valid but I want to offer an alternative that you may find useful.

You are probably using the 4 arrow keys to move around and you have a barrier at each side of your play area. In my game, when the player walks into a solid object, a variable is changed which stops the player from moving in that direction (even if the key is pressed), until the player moves away. This is done using TRIGGER collisions, and the message.enter and message.exit messages that come from the TRIGGER collsion objects.

In your player script, create 4 properties called “moveableup, moveabledown, movableleft, and movableright”. Set them all to default to true.

When the player collides with the barrier on the left make “movableleft” false (on message.enter). When the player gets the message.exit, make “movableleft” true again. Do this in all four directions.

Then, in your movement part of your script, add an “if movableleft then” condition, to stop movement in that direction. Do this in all four directions.

After that, you can use the same code with solid objects (trees, houses, whatever) within your game space area. Just make sure that even the small objects have four different collision objects, to stop the movement in the relevant direction.

3 Likes

Please read my lengthy answer on this topic here: Beginner question about Collisions

An example of how to resolve the collisions in a top-down game can be seen here in my LD 37 entry:

1 Like

Note that this only works when colliding with objects that are of type Trigger (which I assume your level geometry is @88.josh?)

OOPS. Yes you’re right. I have edited my post to avoid confusion.

What if I had each of the four screen collisions as their own collision objects instead of adding four collision objects to the player? Would this still work?

Yes, that would work.

1 Like