Search code examples
physicsgame-physicsgame-makercollision

GameMaker Studio Physics Object Precise Collisions


Is it possible to have a physics object in GameMaker Studio use precise collisions?

Here's some context for my question. I'm making a pirate game where the player sails around a large ocean with a number of islands. I've been using the physics engine to control the movement of the ship, and that is working well. However, the problem arises when trying to introduce collisions between the ship and the various islands. As far as I can tell, the underlying physics fixtures can only be formed into fairly simple shapes. Specifically, the collision shape editor is limited to 12 points, and only convex shapes. This is a problem, because many of my islands are relatively complicated non-convex shapes, and aren't necessarily a single piece. It would be nice to be able to use the island sprite as a precise collision mask, as would be possible for non physics based objects.

Is there a way to do this, or a possible work-around that I'm missing? Here's an example of one of my islands: enter image description here


Solution

  • I can see two solutions to your problem.

    1 - The easiest, but performance-unfriendly.

    In the sprite editor, click "Modify mask". There should be a "precise collision checking" box you can tick. This means that your sprite will be checked pixel by pixel for collisions. As you can guess, this is not performance friendly, but will do exactly what you want.

    2 - The one I would recommend.

    What you could do is just draw the island sprites, either through the background or via a dedicated object, and then create some simple shape objects (rectangle, circle and diamond), that would be invisible, and place them over your islands in the room editor. (Don't forget that you can stretch them).

    These simple shaped objects would be the ones to check for collisions. I used this technique make a hitbox for complex-shaped clouds in one of my games, so I know it works.

    I believe that the island you show us can be fairly well covered with a few ovals and a long rectangle.

    Bonus : after doing that graphically, you can copy the creation code of the shapes from the room create event to the island create event to repeat for multiple identical islands. Just don't forget the position/angle offset !