2

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

aknight0
  • 163
  • 2
  • 14
  • You can write a function to find the outline of the island. find a pixel that is next to a transparent pixel then continue finding pixels next to the current pixel the has at least one transparent neighbour ( always searching in the sane direction eg from above clockwise) until you return to the first pixel, then you need to triangulate and group into convex shapes. This will help https://en.wikipedia.org/wiki/Polygon_triangulation. Only needs to be done once so you could do it in production by hand. – Blindman67 Jun 05 '16 at 04:53
  • So you are suggesting splitting each object in to multiple convex objects? I suppose I could do that, though fitting them all back together seamlessly might be troublesome. – aknight0 Jun 09 '16 at 23:04

2 Answers2

2

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 !

  • 1 - This goes back to my original question, is there a way to use precise collision checking with objects that are using physics? It seems that with physics objects, the only option is to use collision shape editor. 2- This general idea (create a complex collision shape from multiple simple ones) is the most common suggestion so far, and may be the best way to move forward. I'll give it a try, but I'd still be interested in knowing the answer to the original question. – aknight0 Jun 16 '16 at 22:37
  • Yes, go in the sprite editor, click "modifiy mask", then there is a box you can tick that says "precise collision checking" – An intern has no name Jun 17 '16 at 07:23
  • Yes, that is how to enable precise collision checking for a normal object. What I am asking about is precise collisions for objects where the "Uses Physics" option is checked. Checking that option automatically adds a collision shape to the object that is separate from the sprite collision mask. It's the limitations of these new collision shapes that causes the problems (see answer by Duphus for a screenshot). I have tried enabling precise collision checking on the sprites, but when the game runs the collisions that happen are still based on the physics shapes, not the sprite masks. – aknight0 Jun 17 '16 at 17:11
  • Oh, I'm sorry, I didn't understand. My apologies – An intern has no name Jun 17 '16 at 17:39
  • I ended up taking your second suggestion. I created an invisible hexagon object (with physics, and density 0) and used that to tile over my different islands. Then I do physics collisions with the hexagons. It's not perfect, but it seems to work okay. Because there are so many of them, I set the physics on the hexagons to start asleep, and that keeps them from affecting the game speed. Thanks for the ideas! – aknight0 Jul 10 '16 at 00:04
0

By using the Shape options when defining the collision shape, you can have any kind of Convex polygon as your collision shape. Example:

The spot where you choose the Shape option is in red.

After you select that option, you can just click & drag to add/edit a vertex to the polygon. Just bear in mind, it has to be a convex polygon, GameMaker is very strict about that. You can also remove vertices by right clicking on them.

Duphus
  • 191
  • 1
  • 10
  • I edited my question to clarify. The issue I'm having is that the collision shape editor can't make shapes complex enough fit my islands well (See the new image above). The convex shape limitation in particular is a problem. That's why I asked if precise collisions are possible. – aknight0 Jun 09 '16 at 23:02
  • Wow. Yeah, I sea what you mean. Maybe break it up into smaller shapes? Like instead of having one big collision box, have a bunch of smaller ones. For example, for the island way up at the top, instead of having a complex polygon, use two simpler ones. – Duphus Jun 12 '16 at 02:12