sharpPhysics icon indicating copy to clipboard operation
sharpPhysics copied to clipboard

separate Circle struct

Open 7Bpencil opened this issue 5 years ago • 8 comments

I noticed that you use fileds Width and Height of PhysicsObject only when PhysicsObject is a Circle and field AABB only when PhysicsObject is AABB. Clearly Width is a circle's diameter and Height is not used at all. So you can extract those fields in separated structs and don't mix them in PhysicsObject.

Something like:

public interface IShape { }

public struct Circle : IShape { public float Radius; ... }

public struct AABB : IShape { public Vector2 Min; public Vector2 Max; ... }

public class PhysicsObject { public bool Locked; public IShape Shape; public Vec2 Velocity; public Vec2 Center; public float Restitution; public float Mass; public float IMass; ... }

And in UpdatePhysics():

if (manifold.A.Shape is Circle && manifold.B.Shape is Circle) Collision.CirclevsCircle(ref manifold) else if (manifold.A.Shape is AABB && manifold.B.Shape is Circle) Collision.AABBvsCircle(ref manifold) else if (manifold.A.Shape is AABB && manifold.B.Shape is AABB) Collision.AABBvsAABB(ref manifold)

7Bpencil avatar Jul 31 '20 10:07 7Bpencil

one disadvantage is boxing/unboxing but it's possible to avoid that

7Bpencil avatar Jul 31 '20 11:07 7Bpencil

Or turn structs into classes:

public abstract class Shape { public abstract float GetArea(); public abstract void Move(Vec2 dVector); public abstract bool ContainsPoint(Vec2 point); }

public class Circle : Shape { ... } public class AABB : Shape { ... }

7Bpencil avatar Jul 31 '20 11:07 7Bpencil

I would like to turn them into classes, but originally avoided that because of the potential performance increases in using structs (citation needed). I’m wondering how far I would have to scale this to see a performance difference at all though. Do you have experience in the performance side of using structs vs classes?

dankelley2 avatar Jul 31 '20 12:07 dankelley2

Hmm.. I’d like to see if I can stick with structs and avoid the boxing / unboxing issue.

https://medium.com/csharp-architects/whats-faster-in-c-a-struct-or-a-class-99e4761a7b76.

dankelley2 avatar Jul 31 '20 12:07 dankelley2

I browsed some code of BepuPhysics2. It's pretty advanced engine. It uses structs for shapes like box, triangle, capsule, etc. But these structs implement IConvexShape interface. So I think it has some special sause that allow struct's polymorhism without perfomance drawbacks.

Maybe I can find another example with more simple codebase

7Bpencil avatar Jul 31 '20 13:07 7Bpencil

aaaah Box != AABB, got it

7Bpencil avatar Jul 31 '20 13:07 7Bpencil

Hello again! I rewrote the whole engine to ECS paradigm (instead of OOP), it can be interesting to you too, so I published it as fork.

7Bpencil avatar Aug 28 '20 13:08 7Bpencil

Hello again! I rewrote the whole engine to ECS paradigm (instead of OOP), it can be interesting to you too, so I published it as fork.

That’s Awesome! Definitely taking a look at that today. I’ve never known much about game design / architecture and have never heard of the ECS style system before now.

dankelley2 avatar Aug 30 '20 13:08 dankelley2

Marking this issue as closed because I finally decided to make this change. Definitely for the best.

dankelley2 avatar Feb 20 '25 14:02 dankelley2