Skip to content

2D Collisions

Neil Ashford edited this page Oct 22, 2017 · 5 revisions

Overall

In 2D, collision is handled via Shape2D instances. The Shape2D interface tracks a 2D point location (typically the centre of the mask) and provides some collision / distance methods.

Interactions with other collision masks

It should be noted that all of these collision / distance methods should operate on any Shape2D object. There is a defined hierarchy of collision masks (see below). Each class that implements Shape2D should have custom logic for interactions with itself, and for interactions with classes that are higher on the hierarchy than itself. In dealings with classes that are lower on the hierarchy, other.methodName(this) should be called to. This avoids code duplication, and saves people from having to add code to every single Shape2D when they make a new class that implements the interface.

The hierarchy (so far) is as follows:

  1. Point2D
  2. Circle2D
  3. Box2D
  4. Line2D

(If you want to make a new Shape2D, please add it after Line2D)

Interface methods

Firstly, there are 5 trivial methods that are simply getters / setters for the x/y location of the mask and a copy method. The x/y points do not necessarily have to be the centre of the mask, as that may be hard to define for more complicated masks, but if it could be the centre of the mask that would be nice.

Secondly, there are 2 distance methods. The first of these takes another Shape2D object, and calculates the minimum edge-to-edge distance between them.

minimum edge-to-edge distance diagram

In the case of the square and circle in the above diagram, though other lines could be drawn from the edge of the circle to the edge of the square, they would not be as short. As such, it is the length of the line featured in this diagram that should be returned by the distance method in this case.

If the two collision masks overlap in any way, a negative number should be returned by this method.

The other distance method takes 4 floats as arguments, which represent the starting and stopping x,y coordinates of a line segment. This method should return the minimum distance from the edge of the collision mask to the line. If the line intersects the collision mask, it should return a negative number, much like the other distance method. Below is a diagram of the expected behavior of this method in some example cases.

distance-to-line diagram

Finally, there is an overlaps method. Functionally, this is equivalent to calling distance with the same arguments, and seeing if the result is negative or not. However, there are some optimisations that can be made if all you need to calculate is whether two objects intersect, instead of how far away they are. This method should follow the same hierarchy as the distance method.


Gameplay

Design

Asset Creation

User Testing

Code Guidelines


http://cultofthepartyparrot.com/parrots/partyparrot.gifhttp://cultofthepartyparrot.com/parrots/partyparrot.gifhttp://cultofthepartyparrot.com/parrots/partyparrot.gifhttp://cultofthepartyparrot.com/parrots/partyparrot.gifhttp://cultofthepartyparrot.com/parrots/partyparrot.gif

Clone this wiki locally