Skip to content

Conversation

@wesheng
Copy link
Contributor

@wesheng wesheng commented Dec 14, 2024

This is a two-part PR. One is a major implementation, while the other is a bug fix. I can split it into two on request.

This primarily does a very simple implementation of Physics Layers. The motivation for implementing this is to make it easier for projects that use this library to add complex collision, such as pickups & power-ups that can only interact with the player but not enemies.

It creates a struct, PhysicsLayer, that stores a list of excluded layers. All layers interact with each other by default.

Since it's a simple implementation, the code isn't optimized. The code allows for std::list::max_size() amount of layers, but I'm sure there will be slowdowns if you use too many objects and layers at the same time.

Usage should be easy to use: Call PhysicsShape.setLayer(unsigned layer) to set up an object's layer. To prevent a layer from interacting from another, use World.ExcludeCollision(unsigned layer1, unsigned layer2). By default, all objects are on layer 0.

The created functions all have documentation on them, so it shouldn't be confusing to use.

An MSTest, LayerInteractionTest, was created to showcase this in action.

While writing this test, I discovered that the PhysicsCircle rendering and bounds were inconsistent when using setSize(). This is because the PhysicsCircle takes in a radius and passes it into sf::CircleShape, but divides it for the collision test in CircleBounds, which assumed that the parameter was a diameter instead. To fix, I removed the division.

See comparison & demonstration below. Green objects are on separate layers and are set so that they cannot interact with each other.

Before After
testhost_WsJu77taoY testhost_9gg1XJfahK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant