Documentation ¶
Overview ¶
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.
Index ¶
- func Distance(x, y, x2, y2 int32) int32
- type BasicShape
- func (b *BasicShape) AddTags(tags ...string)
- func (b *BasicShape) ClearTags()
- func (b *BasicShape) GetData() interface{}
- func (b *BasicShape) GetTags() []string
- func (b *BasicShape) GetXY() (int32, int32)
- func (b *BasicShape) HasTags(tags ...string) bool
- func (b *BasicShape) Move(x, y int32)
- func (b *BasicShape) RemoveTags(tags ...string)
- func (b *BasicShape) SetData(data interface{})
- func (b *BasicShape) SetXY(x, y int32)
- type Circle
- type Collision
- type IntersectionPoint
- type Line
- func (l *Line) Center() (int32, int32)
- func (l *Line) GetBoundingCircle() *Circle
- func (l *Line) GetBoundingRectangle() *Rectangle
- func (l *Line) GetDelta() (int32, int32)
- func (l *Line) GetIntersectionPoints(other Shape) []IntersectionPoint
- func (l *Line) GetLength() int32
- func (l *Line) IsColliding(other Shape) bool
- func (l *Line) Move(x, y int32)
- func (l *Line) SetLength(length int32)
- func (l *Line) SetXY(x, y int32)
- func (l *Line) WouldBeColliding(other Shape, dx, dy int32) bool
- type Rectangle
- type Shape
- type Space
- func (sp *Space) Add(shapes ...Shape)
- func (sp *Space) AddTags(tags ...string)
- func (sp *Space) Clear()
- func (sp *Space) ClearTags()
- func (sp *Space) Contains(shape Shape) bool
- func (sp *Space) Filter(filterFunc func(Shape) bool) *Space
- func (sp *Space) FilterByTags(tags ...string) *Space
- func (sp *Space) FilterOutByTags(tags ...string) *Space
- func (sp *Space) Get(index int) Shape
- func (sp *Space) GetCollidingShapes(shape Shape) *Space
- func (sp *Space) GetData() interface{}
- func (sp *Space) GetTags() []string
- func (sp *Space) GetXY() (int32, int32)
- func (sp *Space) HasTags(tags ...string) bool
- func (sp *Space) IsColliding(shape Shape) bool
- func (sp *Space) Length() int
- func (sp *Space) Move(dx, dy int32)
- func (sp *Space) Remove(shapes ...Shape)
- func (sp *Space) RemoveTags(tags ...string)
- func (sp *Space) Resolve(checkingShape Shape, deltaX, deltaY int32) Collision
- func (sp *Space) SetData(data interface{})
- func (sp *Space) SetXY(x, y int32)
- func (sp *Space) String() string
- func (sp *Space) WouldBeColliding(other Shape, dx, dy int32) bool
- Bugs
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BasicShape ¶
type BasicShape struct {
X, Y int32
Data interface{}
// contains filtered or unexported fields
}
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.
func (*BasicShape) AddTags ¶
func (b *BasicShape) AddTags(tags ...string)
AddTags adds the specified tags to the BasicShape.
func (*BasicShape) ClearTags ¶
func (b *BasicShape) ClearTags()
ClearTags clears the tags active on the BasicShape.
func (*BasicShape) GetData ¶
func (b *BasicShape) GetData() interface{}
GetData returns the data on the Shape.
func (*BasicShape) GetTags ¶
func (b *BasicShape) GetTags() []string
GetTags returns a reference to the the string array representing the tags on the BasicShape.
func (*BasicShape) GetXY ¶
func (b *BasicShape) GetXY() (int32, int32)
GetXY returns the position of the Shape.
func (*BasicShape) HasTags ¶
func (b *BasicShape) HasTags(tags ...string) bool
HasTags returns true if the Shape has all of the tags provided.
func (*BasicShape) Move ¶
func (b *BasicShape) Move(x, y int32)
Move moves the Shape by the delta X and Y values provided.
func (*BasicShape) RemoveTags ¶
func (b *BasicShape) RemoveTags(tags ...string)
RemoveTags removes the specified tags from the BasicShape.
func (*BasicShape) SetData ¶
func (b *BasicShape) SetData(data interface{})
SetData sets the data on the Shape.
func (*BasicShape) SetXY ¶
func (b *BasicShape) SetXY(x, y int32)
SetXY sets the position of the Shape.
type Circle ¶
type Circle struct { BasicShape Radius int32 }
A Circle represents an ordinary circle, and has a radius, in addition to normal shape properties.
func (*Circle) GetBoundingRect ¶
GetBoundingRect returns a Rectangle which has a width and height of 2*Radius.
func (*Circle) IsColliding ¶
IsColliding returns true if the Circle is colliding with the specified other Shape, including the other Shape being wholly within the Circle.
type Collision ¶
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.
func Resolve ¶
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.
type IntersectionPoint ¶
IntersectionPoint represents a point of intersection from a Line with another Shape.
type Line ¶
type Line struct { BasicShape X2, Y2 int32 }
Line represents a line, from one point to another.
func (*Line) GetBoundingCircle ¶
GetBoundingCircle returns a circle centered on the Line's central point that would fully contain the Line.
func (*Line) GetBoundingRectangle ¶
GetBoundingRectangle returns a rectangle centered on the center point of the Line that would fully contain the Line.
func (*Line) GetDelta ¶
GetDelta returns the delta (or difference) between the start and end point of a Line.
func (*Line) GetIntersectionPoints ¶
func (l *Line) GetIntersectionPoints(other Shape) []IntersectionPoint
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.
func (*Line) IsColliding ¶
IsColliding returns if the Line is colliding with the other Shape. Currently, Circle-Line collision is missing.
type Rectangle ¶
type Rectangle struct { BasicShape W, H int32 }
Rectangle represents a rectangle.
func NewRectangle ¶
NewRectangle creates a new Rectangle and returns a pointer to it.
func (*Rectangle) GetBoundingCircle ¶
GetBoundingCircle returns a circle that wholly contains the Rectangle.
func (*Rectangle) IsColliding ¶
IsColliding returns whether the Rectangle is colliding with the specified other Shape or not, including the other Shape being wholly contained within the Rectangle.
type Shape ¶
type Shape interface { IsColliding(Shape) bool WouldBeColliding(Shape, int32, int32) bool GetTags() []string ClearTags() AddTags(...string) RemoveTags(...string) HasTags(...string) bool GetData() interface{} SetData(interface{}) GetXY() (int32, int32) SetXY(int32, int32) Move(int32, int32) }
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.
type Space ¶
type Space []Shape
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.
func NewSpace ¶
func NewSpace() *Space
NewSpace creates a new Space for shapes to exist in and be tested against in.
func (*Space) Add ¶
Add adds the designated Shapes to the Space. You cannot add the Space to itself.
func (*Space) Clear ¶
func (sp *Space) Clear()
Clear "resets" the Space, cleaning out the Space of references to Shapes.
func (*Space) ClearTags ¶
func (sp *Space) ClearTags()
ClearTags removes all tags from all Shapes within the Space.
func (*Space) Filter ¶
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.
func (*Space) FilterByTags ¶
FilterByTags filters a Space out, creating a new Space that has just the Shapes that have all of the specified tags.
func (*Space) FilterOutByTags ¶
FilterOutByTags filters a Space out, creating a new Space that has just the Shapes that don't have all of the specified tags.
func (*Space) Get ¶
Get allows you to get a Shape by index from the Space easily. This is a convenience function, standing in for (*space)[index].
func (*Space) GetCollidingShapes ¶
GetCollidingShapes returns a Space comprised of Shapes that collide with the checking Shape.
func (*Space) GetData ¶
func (sp *Space) GetData() interface{}
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.
func (*Space) GetTags ¶
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.
func (*Space) GetXY ¶
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.
func (*Space) HasTags ¶
HasTags returns true if all of the Shapes contained within the Space have the tags specified.
func (*Space) IsColliding ¶
IsColliding returns whether the provided Shape is colliding with something in this Space.
func (*Space) Length ¶
Length returns the length of the Space (number of Shapes contained within the Space). This is a convenience function, standing in for len(*space).
func (*Space) RemoveTags ¶
RemoveTags removes the provided tags from all Shapes contained within the Space.
func (*Space) Resolve ¶
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.
func (*Space) SetData ¶
func (sp *Space) SetData(data interface{})
SetData sets the pointer provided to the Data field of all Shapes within the Space.
Notes ¶
Bugs ¶
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.