This is a proposal for some changes to the hitbox/collision-detection system, for the purpose of improving efficiency and interoperability with other parts of Flame. This issue is intended as discussion / collaboration with @spydon, before we start the actual work.
First problem that needs to be addressed is that currently the collision detection system can only "live" at the root FlameGame level, which is not always optimal. In a more complicated game the game world can be several levels deep in the component tree, or there could be several worlds (say, for each level). So, I suggest then to
HasCollisionDetection
, but which could be applied to any Component
.
on
part of the current mixin.Second problem is that the current implementation of ShapeHitbox
is based on ShapeComponent
s, not on Shape
s. Thus, we need to
Hitbox
es based on Shape
class and its descendants.
Shape
class has support for intersection detection of convex shapes).
intersections()
method; or, rather, create a second more efficient method and deprecate the first. The signature of the new method would be Intersection? intersection(T other);
, which is to say it returns null
if the two shapes do not intersection, or an Intersection
object if they do. The Intersection
object can then be queried for the intersection points, or some other intersection properties. However, if the user is not interested in the intersection points - then that calculation can be skipped.
Intersection
class could also contain references to the hitboxes + components that are colliding.Hitbox
class is defined as implements GenericCollisionsCallbacks<T>
, where T
is the derived hitbox type. However, the GenericCollisionsCallbacks
class says that T
has to be the type of the object to which the hitbox is attached. It looks like a bug to me.possiblyIntersects()
method can be deprecated as well, since the intersection()
method covers that use-case.The biggest source of inefficiency with the current collision detection system is that whenever any two hitboxes need to be checked for intersection, they are projected into the "absolute" coordinates (i.e. the coordinate system of the canvas). This is expensive: not only some objects never move and therefore don't have to be projected each time, if there are N
objects, we currently end up doing O(N^2)
projections, whereas simply performing a projection once at the start of a tick and then computing the intersections on those would only be O(N)
.
HasCollisionDetection
mixin (see item 1 above).Shape
class, which already implements the project()
method.Current ShapeComponent
class (and descendants) contains both the "shape-component" logic, and also the logic for computing intersections between various shapes. This is too jumbled: we should
ShapeComponent
class to be used in examples etc.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too