2D walkable area

In a 2D environment that use TCastleScene I would like to define a walkway area for my character (a sort of polygon whose sides represent the limit that cannot be crossed).
Does Castle game provides some particular function or does it all have to be managed via code?


afaik there’s no basic collision detection for 2D, and the one that is currently work for 3D will be removed in the future in favor of kraft physics engine, which is already integrated in CGE.

You maybe able to do this with kraft physics engine (just maybe, because I haven’t used kraft, but have experience with other 2D physics engines). Create some MeshColliders as walls and wrap your character around a BoxCollider, and make sure to lock your character’s rotation so that it won’t rotate when hitting walls. Note that you will have to manipulate your character’s position via kraft, by applying force to RigidBody instead of using TCastleTransform directly.

You can take a look at physics_2d_game_sopwith demo in examples directory to understand how to setup physics in CGE.

If you dont want to do it “the physics way” then you will have to implement 2D collision detection yourself :slight_smile:

1 Like

Ok thanks, I’ll see if I can come up with some position control algorithm that is not too heavy.

Both existing collision detection mechanisms (our own, using octrees, and physics-based using Kraft) can be used for 2D.

That said, it may be easier to “roll your own” solution if you just want 2D tests to control the “walkable area” for the character. So I fully support what @kagamma wrote.

It can be as easy as using TFloatRectangle to define an area where sprite is, it has methods to check whether it contains some point (TVector2) or another rectangle. You can also look at particular scene’s bounding box MyScene.WorldBoundBox, and convert 3D bounding box to 2D bounding rectangle like MyScene.WorldBoundingBox.RectangleXY.

In “The Unholy Society”, to define a walkable area for a character, we indeed use this solution with custom implementation using TFloatRectangle . It was easy to make, and gave us full control.

1 Like