Useful method to make concave collision mask

My task is to make the car go slower on the green grass than on the road. I did it initially using collision masks, but this method turned out to be very difficult. Drawing dots is very inconvenient in the Gdevelop mask editor. I had to make convex figures a lot. I’m sick of it. Then I started trying to make collisions with sprites. But this is also extremely inconvenient. It would be ideal to make a concave shape, which is easy to designate the entire route. But for some reason there is no such possibility in Gdevelop. Although in the same Construct 2/3, Godot such features are present.

How is it as easy and simple as possible to mark the track, separate it from the grass in order to give different speeds to the car?

image

Unfortunately, the majority of 2D physics engines and renderers do not support concave shapes for collision masks, including GDevelop (both static collisions and physics shapes).

There isn’t really a workaround, either you need to make multiple masks on a singular object, or make objects for the collision.

That said, you might be able to speed it up somewhat by making a few different collision objects for “common” collision shapes you are running into, as you can stretch and rotate as needed. That way you only have to manually modify a select few.

Edit: as a note. godots implementation is done by splitting concave shapes into hollow convex lines per what I can see in their documentation, and they have explicit warnings that concave shapes are inaccurate and slower, and cannot be used for physics (collision) nor detection.

Hi jkeks, I thought your problem was very interesting and I had fun coming up with a potential solution.

SpeedLimitCarTest


Here’s what I did. I made a green racetrack but instead of having the road black, I made it transparent.

image


Then I made a large dark background to go behind it.

image


Then I installed the Read Pixels extension from Bouh.




Then I made a transparent hole in the middle of my car.

image


I have it set up so that a variable is set to get the value of red pixels under the transparent section of the car. The red in my dark background (road) is 32.

If the readpixels red variable is 32 then the car goes fast. If it’s not 32 then the car goes slow. You could build in safety by saying less than 50 instead of is 32. And similar for the slow. But it depends what kind of colors you have for the car and grass and how much red they have in them.

Problems - the hole in the middle of the car needed to be pretty big or else the read pixels would read the color of the car when the car was moving fast. I’ve used a very small transparent hole in the middle of sprites in other projects to use the Read Pixels extension. But because of the car movement, I had to make the hole quite big. But if your car color and grass color have a red value (or green or blue if you use them) that’s similar and very different from the road value then a smaller transparent hole might work if you use less than and more than in your conditions. Actually I’m not sure about that. But interested in any feedback and improvements on this idea.

4 Likes

That’s a super smart alternative, although know that reading pixel colors is relatively CPU intensive. Shouldn’t be an issue in this use case, but if you start getting into hundreds of collisions it could be an issue.
Also it’ll only work with points and not an entire collision mask. Maybe you could set a point on each car tire and do an “Or” statement condition with each tire’s point?

1 Like

Here’s an update. It’s still a compromise though. I removed the transparent hole from the car and made a new object that was transparent (with a blue border in the pic so you can see it) and used the Sticker extension to stick it to the front of the car. The variable is now set to read the pixels in the square instead. But it means less accuracy because the car and sticker object are not aligned. Performance is the same in terms of speed limiting.

Actually this would work well with Silver-Streak’s idea of using the wheels as the detectors. There could be a transparent box outside each wheel. That would be very neat.

image

Track may contain objects with non-black color? like this…
image

Focusing only on color is a bad idea.
in addition, there may also be black spots on the grass, this will also distort the intended logic of work

image

I’ve done it with the Red sprited

Hi! Total newbie here.

Would it be less CPU expensive to use Tiled Map Editor and its collision system? I know you still would have to go tile by tile changing the collision mask, but you can make bigger tiles than usual for than kind of game. And then import it to Gdevelop using the New object->External Tilemap (Tiled/LDtk) collision mask.