I made a simple 2d physics test scene with a StaticBody2D acting as the “floor” and gravity pointing downwards along the Y axis.
On mouse click I instantiate (clone) a RigidBody2D with CollisionPolygon2D as collision shape. The polygon is a simple square of 100x100 size.
I am expecting the blocks to fall and behave according to some realistic pyhsics, but instead I notice a lot of unexpected movements that seem impossible in reality.
It is hard to explain, so I recorded a small video: https://imgur.com/a/yZmvxiq
Some blocks move for no reason, some others weirdly float halfway in the air.
What is also really strange is that if I replace the CollisionPolygon2D with a CollisionShape2D + rectangle shape (same dimensions) the physics seem to work much more as you would intuitively expect.
Any ideas on what I might be doing wrong with my 2D polygon ?
Your collision shape is probably not centered on the RigidBody’s origin, so it tries to rotate around that corner of the shape, like it’s center of mass is just the corner.
Thank you @gertkeno . I checked and the center of mass was in the top-left corner.
I moved the collision shape so that the center of mass was exactly in the center of the polygon:
Then I saw that on the RigidBody2D there’s a “mass distribution” property group and “center of mass mode” property. I change that from “auto” to “custom” without changing any other value and that did solve the problem (together with the centering of the collision polygon).
The documentation says “In this [auto] mode, the body’s center of mass is calculated automatically based on its shapes. This assumes that the shapes’ origins are also their center of mass.”
So if I read that right, the shape’s origin in my case is always the top-left corner, regardless of how I move it around within the body.
In custom mode instead the center of mass “Defaults to the body’s origin position.”. So if I center the shape on the body and turn custom mode on, it would make sense that it fixes things.
That’s OK, but makes the auto mode default computation a little bit useless, if I understand things correctly.
My answer would be to use a CollisionShape2D and not a CollisionPolygon2D, because the former act the way you expect as a beginner, and the latter require a lot more work. Also, I’ve really only ever used CollisionPolygon2D with a Skeleton2D for Inverse Kinematics animation - and there’s a whole process for creating them which is not what you did to create them.
In a 2D game you can apply CollisionPolygon2D shapes to TileSets without ever actually creating a CollisionPolygon2D yourself. For everything else, either you’re making a Skeleton2D or usually just using simpler CollisionShape2D shapes.
It sounds like you have enough knowledge to jump into a new language and play around, but you’ve got some assumptions based on previous languages that might complicate your entry into the world of Godot.
Thank you for your suggestions about the tutorials @dragonforge-dev .
I should indeed brush up on the tutorials. I did them back in the days of godot 3.? before taking a long break, and only recently getting back into game dev. I’ll look into them again.
Regarding your suggestion to use CollisionShape2D, it would work for the video I posted, where I used square blocks to reproduce the problem in the simplest situation possible.
However the reason why I am trying to use a CollisionPolygon2D as the collision shape is because in the game I’m prototyping I have a mechanic where the player can cut arbitrarily shaped 2d polygons out of larger polygons. These cut polygons then need to fall and bounce according to physics.
Think of a game like the classic Qix but with the claimed shapes falling down towards gravity instead of just disappearing.
Given what I’ve learned in this thread, I think it would make sense to keep the collision shape as a CollisionPolygon2D, but then I would calculate manually the polygon center of mass and use this value for the custom center of mass property.
From your experience can you recommend a more proper way to do this “the Godot way”, if you can think of any ?
Thank you in advance.
If you’ve done them before the tutorials will probably only take you 30 min apiece.
Your implementation is unique and advanced. I think you’re heading down the right track. However I’d recommend you check out the Skeleton2D documentation I linked above and then do the Inverse Kinematics tutorial. Because I think that you will find some of what you learn might give you new ideas about how to implement what you’re doing.