Package resolv is a simple collision detection and resolution library mainly geared towards simpler 2D arcade-style games. Its goal is to be lightweight, fast, simple, and easy-to-use for game development. Its goal is to also not become a physics engine or physics library itself, but to always leave the actual physics implementation and "game feel" to the developer, while making it very easy to do so.
Usage of resolv essentially centers around two main concepts: Spaces and Shapes.
A Shape can be used to test for collisions against another Shape. That's really all they have to do, but that capability is powerful when paired with the resolv.Resolve() function. You can then check to see if a Shape would have a collision if it attempted to move in a specified direction. If so, the Resolve() function would return a Collision object, which tells you some information about the Collision, like how far the checking Shape would have to move to come into contact with the other, and which Shape it comes into contact with.
A Space is just a slice that holds Shapes for detection. It doesn't represent any real physical space, and so there aren't any units of measurement to remember when using Spaces. Similar to Shapes, Spaces are simple, but also very powerful. Spaces allow you to easily check for collision with, and resolve collision against multiple Shapes within that Space. A Space being just a collection of Shapes means that you can manipulate and filter them as necessary.
Distance returns the distance from one pair of X and Y values to another.
BasicShape isn't to be used directly; it just has some basic functions and data, common to all structs that embed it, like position and tags. It is embedded in other Shapes.
AddTags adds the specified tags to the BasicShape.
ClearTags clears the tags active on the BasicShape.
GetData returns the data on the Shape.
GetTags returns a reference to the the string array representing the tags on the BasicShape.
GetXY returns the position of the Shape.
HasTags returns true if the Shape has all of the tags provided.
Move moves the Shape by the delta X and Y values provided.
RemoveTags removes the specified tags from the BasicShape.
SetData sets the data on the Shape.
SetXY sets the position of the Shape.
A Circle represents an ordinary circle, and has a radius, in addition to normal shape properties.
NewCircle returns a pointer to a new Circle object.
GetBoundingRect returns a Rectangle which has a width and height of 2*Radius.
IsColliding returns true if the Circle is colliding with the specified other Shape, including the other Shape being wholly within the Circle.
WouldBeColliding returns whether the Rectangle would be colliding with the specified other Shape if it were to move in the specified direction.
Collision describes the collision found when a Shape attempted to resolve a movement into another Shape in an isolated check, or when within the same Space as other existing Shapes. ResolveX and ResolveY represent the displacement of the Shape to the point of collision. How far along the Shape got when attempting to move along the direction given by deltaX and deltaY in the Resolve() function before touching another Shape. Teleporting is if moving according to ResolveX and ResolveY might be considered teleporting, which is moving greater than the deltaX or deltaY provided to the Resolve function * 1.5 (this is arbitrary, but can be useful when attempting to see if a movement would be ). ShapeA is a pointer to the Shape that initiated the resolution check. ShapeB is a pointer to the Shape that the colliding object collided with, if the Collision was successful.
Resolve attempts to move the checking Shape with the specified X and Y values, returning a Collision object if it collides with the specified other Shape. The deltaX and deltaY arguments are the movement displacement in pixels. For platformers in particular, you would probably want to resolve on the X and Y axes separately.
Colliding returns whether the Collision actually was valid because of a collision against another Shape.
IntersectionPoint represents a point of intersection from a Line with another Shape.
Line represents a line, from one point to another.
NewLine returns a new Line instance.
Center returns the center X and Y values of the Line.
GetBoundingCircle returns a circle centered on the Line's central point that would fully contain the Line.
GetBoundingRectangle returns a rectangle centered on the center point of the Line that would fully contain the Line.
GetDelta returns the delta (or difference) between the start and end point of a Line.
GetIntersectionPoints returns the intersection points of a Line with another Shape as an array of IntersectionPoints. The returned list of intersection points are always sorted in order of distance from the start of the casting Line to each intersection. Currently, Circle-Line collision is missing.
GetLength returns the length of the Line.
IsColliding returns if the Line is colliding with the other Shape. Currently, Circle-Line collision is missing.
Move moves the Line by the values specified.
SetLength sets the length of the Line to the value provided.
SetXY sets the position of the Line, also moving the end point of the line (so it wholly moves the line to the specified position).
WouldBeColliding returns if the Line would be colliding if it were moved by the designated delta X and Y values.
Rectangle represents a rectangle.
NewRectangle creates a new Rectangle and returns a pointer to it.
Center returns the center point of the Rectangle.
GetBoundingCircle returns a circle that wholly contains the Rectangle.
IsColliding returns whether the Rectangle is colliding with the specified other Shape or not, including the other Shape being wholly contained within the Rectangle.
WouldBeColliding returns whether the Rectangle would be colliding with the other Shape if it were to move in the specified direction.
Shape is a basic interface that describes a Shape that can be passed to collision testing and resolution functions and exist in the same Space.
A Space represents a collection that holds Shapes for collision detection in the same common space. A Space is arbitrarily large - you can use one Space for a single level, room, or area in your game, or split it up if it makes more sense for your game design. Technically, a Space is just a slice of Shapes. Spaces fulfill the required functions for Shapes, which means you can also use them as compound shapes themselves. In these cases, the first Shape is the "root" or pivot from which attempts to move the Shape will be focused. In other words, Space.SetXY(40, 40) will move all Shapes in the Space in such a way that the first Shape will be at 40, 40, and all other Shapes retain their original spacing relative to it.
NewSpace creates a new Space for shapes to exist in and be tested against in.
Add adds the designated Shapes to the Space. You cannot add the Space to itself.
AddTags sets the provided tags on all Shapes contained within the Space.
Clear "resets" the Space, cleaning out the Space of references to Shapes.
ClearTags removes all tags from all Shapes within the Space.
Contains returns true if the Shape provided exists within the Space.
Filter filters out a Space, returning a new Space comprised of Shapes that return true for the boolean function you provide. This can be used to focus on a set of object for collision testing or resolution, or lower the number of Shapes to test by filtering some out beforehand.
FilterByTags filters a Space out, creating a new Space that has just the Shapes that have all of the specified tags.
FilterOutByTags filters a Space out, creating a new Space that has just the Shapes that don't have all of the specified tags.
Get allows you to get a Shape by index from the Space easily. This is a convenience function, standing in for (*space)[index].
GetCollidingShapes returns a Space comprised of Shapes that collide with the checking Shape.
GetData returns the pointer to the object contained in the Data field of the first Shape within the Space. If there aren't any Shapes within the Space, it returns nil.
GetTags returns the tag list of the first Shape within the Space. If there are no Shapes within the Space, it returns an empty array of string type.
GetXY returns the X and Y position of the first Shape in the Space. If there aren't any Shapes within the Space, it returns 0, 0.
HasTags returns true if all of the Shapes contained within the Space have the tags specified.
IsColliding returns whether the provided Shape is colliding with something in this Space.
Length returns the length of the Space (number of Shapes contained within the Space). This is a convenience function, standing in for len(*space).
Move moves all Shapes in the Space by the displacement provided.
Remove removes the designated Shapes from the Space.
RemoveTags removes the provided tags from all Shapes contained within the Space.
Resolve runs Resolve() using the checking Shape, checking against all other Shapes in the Space. The first Collision that returns true is the Collision that gets returned.
SetData sets the pointer provided to the Data field of all Shapes within the Space.
SetXY sets the X and Y position of all Shapes within the Space to the position provided using the first Shape's position as reference. Basically, it moves the first Shape within the Space to the target location and then moves all other Shapes by the same delta movement.
WouldBeColliding returns true if any of the Shapes within the Space would be colliding should they move along the delta X and Y values provided (dx and dy).
☞ Line.IsColliding() and Line.GetIntersectionPoints() doesn't work with Circles.
☞ Line.IsColliding() and Line.GetIntersectionPoints() fail if testing two lines that intersect along the exact same slope.