camelCase
The Case of the Mysterious Camel.
- Reaction score
- 362
I just implemented a collider system for a crude Android game engine (for educational purposes) and it works. I use the Separating Axis Theorem (SAT) to detect collisions and calculate the minimum translation vector.
I know that SAT only works for convex polygons and that's why I have two classes:
ConvexPolygon
Collider
Class ConvexPolygon's constructor: [ljass]ConvexPolygon (Vector2[] points)[/ljass]
Class Collider's constructor: [ljass]Collider(ConvexPolygon convexPolygon)[/ljass]
So, ConvexPolygon ensures that the points form a convex polygon (duh) and Collider takes advantage of that safety and implements SAT (pre-computes the axes, too).
I have a method [ljass]ConvexPolygon rotate(float radian, Vector2 center)[/ljass] that returns a new instance of ConvexPolygon that's the original polygon rotated by 'radian' around 'center'. So, this method allocates a new array of Vector2 and fills it up, calling cos() and sin() for each point.
So, every time my game object rotates, I have to rotate its collider accordingly, too. And the way I do this is.. [ljass]m_Collider = new Collider(m_Collider.getConvexPolygon().rotate(radian, center));[/ljass]
Which means..
If I rotate my game object by an arbitrary angle every frame (meaning I can't pre-compute the polygons..) I'll have to:
allocate a new array of Vector2
Call cos() and sin() for each point
Pre-compute the axes of the rotated convex polygon for SAT
It doesn't affect my frame rate and I know that this isn't really an OMGWTFMEGAHUGE drain on the CPU but seeing that kind of memory allocation and sin() and cos() calls EVERY FRAME really irks me =/
There might not be a better way to do this if my rotations are going to be arbitrary but I thought I might get some suggestions.
I know that SAT only works for convex polygons and that's why I have two classes:
ConvexPolygon
Collider
Class ConvexPolygon's constructor: [ljass]ConvexPolygon (Vector2[] points)[/ljass]
Class Collider's constructor: [ljass]Collider(ConvexPolygon convexPolygon)[/ljass]
So, ConvexPolygon ensures that the points form a convex polygon (duh) and Collider takes advantage of that safety and implements SAT (pre-computes the axes, too).
I have a method [ljass]ConvexPolygon rotate(float radian, Vector2 center)[/ljass] that returns a new instance of ConvexPolygon that's the original polygon rotated by 'radian' around 'center'. So, this method allocates a new array of Vector2 and fills it up, calling cos() and sin() for each point.
So, every time my game object rotates, I have to rotate its collider accordingly, too. And the way I do this is.. [ljass]m_Collider = new Collider(m_Collider.getConvexPolygon().rotate(radian, center));[/ljass]
Which means..
If I rotate my game object by an arbitrary angle every frame (meaning I can't pre-compute the polygons..) I'll have to:
allocate a new array of Vector2
Call cos() and sin() for each point
Pre-compute the axes of the rotated convex polygon for SAT
It doesn't affect my frame rate and I know that this isn't really an OMGWTFMEGAHUGE drain on the CPU but seeing that kind of memory allocation and sin() and cos() calls EVERY FRAME really irks me =/
There might not be a better way to do this if my rotations are going to be arbitrary but I thought I might get some suggestions.