separate Circle struct
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)
one disadvantage is boxing/unboxing but it's possible to avoid that
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 { ... }
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?
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.
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
aaaah Box != AABB, got it
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.
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.
Marking this issue as closed because I finally decided to make this change. Definitely for the best.