resolv: github.com/SolarLune/resolv/resolv Index | Files

package resolv

import "github.com/SolarLune/resolv/resolv"

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

Package Files

circle.go collision.go doc.go line.go rectangle.go shape.go space.go utils.go

func Distance Uses

func Distance(x, y, x2, y2 int32) int32

Distance returns the distance from one pair of X and Y values to another.

type BasicShape Uses

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 Uses

func (b *BasicShape) AddTags(tags ...string)

AddTags adds the specified tags to the BasicShape.

func (*BasicShape) ClearTags Uses

func (b *BasicShape) ClearTags()

ClearTags clears the tags active on the BasicShape.

func (*BasicShape) GetData Uses

func (b *BasicShape) GetData() interface{}

GetData returns the data on the Shape.

func (*BasicShape) GetTags Uses

func (b *BasicShape) GetTags() []string

GetTags returns a reference to the the string array representing the tags on the BasicShape.

func (*BasicShape) GetXY Uses

func (b *BasicShape) GetXY() (int32, int32)

GetXY returns the position of the Shape.

func (*BasicShape) HasTags Uses

func (b *BasicShape) HasTags(tags ...string) bool

HasTags returns true if the Shape has all of the tags provided.

func (*BasicShape) Move Uses

func (b *BasicShape) Move(x, y int32)

Move moves the Shape by the delta X and Y values provided.

func (*BasicShape) RemoveTags Uses

func (b *BasicShape) RemoveTags(tags ...string)

RemoveTags removes the specified tags from the BasicShape.

func (*BasicShape) SetData Uses

func (b *BasicShape) SetData(data interface{})

SetData sets the data on the Shape.

func (*BasicShape) SetXY Uses

func (b *BasicShape) SetXY(x, y int32)

SetXY sets the position of the Shape.

type Circle Uses

type Circle struct {
    BasicShape
    Radius int32
}

A Circle represents an ordinary circle, and has a radius, in addition to normal shape properties.

func NewCircle Uses

func NewCircle(x, y, radius int32) *Circle

NewCircle returns a pointer to a new Circle object.

func (*Circle) GetBoundingRect Uses

func (c *Circle) GetBoundingRect() *Rectangle

GetBoundingRect returns a Rectangle which has a width and height of 2*Radius.

func (*Circle) IsColliding Uses

func (c *Circle) IsColliding(other Shape) bool

IsColliding returns true if the Circle is colliding with the specified other Shape, including the other Shape being wholly within the Circle.

func (*Circle) WouldBeColliding Uses

func (c *Circle) WouldBeColliding(other Shape, dx, dy int32) bool

WouldBeColliding returns whether the Circle would be colliding with the specified other Shape if it were to move in the specified direction.

type Collision Uses

type Collision struct {
    ResolveX, ResolveY int32
    Teleporting        bool
    ShapeA             Shape
    ShapeB             Shape
}

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 Uses

func Resolve(firstShape Shape, other Shape, deltaX, deltaY int32) Collision

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.

func (*Collision) Colliding Uses

func (c *Collision) Colliding() bool

Colliding returns whether the Collision actually was valid because of a collision against another Shape.

type IntersectionPoint Uses

type IntersectionPoint struct {
    X, Y  int32
    Shape Shape
}

IntersectionPoint represents a point of intersection from a Line with another Shape.

type Line Uses

type Line struct {
    BasicShape
    X2, Y2 int32
}

Line represents a line, from one point to another.

func NewLine Uses

func NewLine(x, y, x2, y2 int32) *Line

NewLine returns a new Line instance.

func (*Line) Center Uses

func (l *Line) Center() (int32, int32)

Center returns the center X and Y values of the Line.

func (*Line) GetBoundingCircle Uses

func (l *Line) GetBoundingCircle() *Circle

GetBoundingCircle returns a circle centered on the Line's central point that would fully contain the Line.

func (*Line) GetBoundingRectangle Uses

func (l *Line) GetBoundingRectangle() *Rectangle

GetBoundingRectangle returns a rectangle centered on the center point of the Line that would fully contain the Line.

func (*Line) GetDelta Uses

func (l *Line) GetDelta() (int32, int32)

GetDelta returns the delta (or difference) between the start and end point of a Line.

func (*Line) GetIntersectionPoints Uses

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) GetLength Uses

func (l *Line) GetLength() int32

GetLength returns the length of the Line.

func (*Line) IsColliding Uses

func (l *Line) IsColliding(other Shape) bool

IsColliding returns if the Line is colliding with the other Shape. Currently, Circle-Line collision is missing.

func (*Line) Move Uses

func (l *Line) Move(x, y int32)

Move moves the Line by the values specified.

func (*Line) SetLength Uses

func (l *Line) SetLength(length int32)

SetLength sets the length of the Line to the value provided.

func (*Line) SetXY Uses

func (l *Line) SetXY(x, y int32)

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).

func (*Line) WouldBeColliding Uses

func (l *Line) WouldBeColliding(other Shape, dx, dy int32) bool

WouldBeColliding returns if the Line would be colliding if it were moved by the designated delta X and Y values.

type Rectangle Uses

type Rectangle struct {
    BasicShape
    W, H int32
}

Rectangle represents a rectangle.

func NewRectangle Uses

func NewRectangle(x, y, w, h int32) *Rectangle

NewRectangle creates a new Rectangle and returns a pointer to it.

func (*Rectangle) Center Uses

func (r *Rectangle) Center() (int32, int32)

Center returns the center point of the Rectangle.

func (*Rectangle) GetBoundingCircle Uses

func (r *Rectangle) GetBoundingCircle() *Circle

GetBoundingCircle returns a circle that wholly contains the Rectangle.

func (*Rectangle) IsColliding Uses

func (r *Rectangle) IsColliding(other Shape) bool

IsColliding returns whether the Rectangle is colliding with the specified other Shape or not, including the other Shape being wholly contained within the Rectangle.

func (*Rectangle) WouldBeColliding Uses

func (r *Rectangle) WouldBeColliding(other Shape, dx, dy int32) bool

WouldBeColliding returns whether the Rectangle would be colliding with the other Shape if it were to move in the specified direction.

type Shape Uses

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 Uses

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 Uses

func NewSpace() *Space

NewSpace creates a new Space for shapes to exist in and be tested against in.

func (*Space) Add Uses

func (sp *Space) Add(shapes ...Shape)

Add adds the designated Shapes to the Space. You cannot add the Space to itself.

func (*Space) AddTags Uses

func (sp *Space) AddTags(tags ...string)

AddTags sets the provided tags on all Shapes contained within the Space.

func (*Space) Clear Uses

func (sp *Space) Clear()

Clear "resets" the Space, cleaning out the Space of references to Shapes.

func (*Space) ClearTags Uses

func (sp *Space) ClearTags()

ClearTags removes all tags from all Shapes within the Space.

func (*Space) Contains Uses

func (sp *Space) Contains(shape Shape) bool

Contains returns true if the Shape provided exists within the Space.

func (*Space) Filter Uses

func (sp *Space) Filter(filterFunc func(Shape) bool) *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.

func (*Space) FilterByTags Uses

func (sp *Space) FilterByTags(tags ...string) *Space

FilterByTags filters a Space out, creating a new Space that has just the Shapes that have all of the specified tags.

func (*Space) FilterOutByTags Uses

func (sp *Space) FilterOutByTags(tags ...string) *Space

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 Uses

func (sp *Space) Get(index int) Shape

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 Uses

func (sp *Space) GetCollidingShapes(shape Shape) *Space

GetCollidingShapes returns a Space comprised of Shapes that collide with the checking Shape.

func (*Space) GetData Uses

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 Uses

func (sp *Space) GetTags() []string

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 Uses

func (sp *Space) GetXY() (int32, int32)

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 Uses

func (sp *Space) HasTags(tags ...string) bool

HasTags returns true if all of the Shapes contained within the Space have the tags specified.

func (*Space) IsColliding Uses

func (sp *Space) IsColliding(shape Shape) bool

IsColliding returns whether the provided Shape is colliding with something in this Space.

func (*Space) Length Uses

func (sp *Space) Length() int

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) Move Uses

func (sp *Space) Move(dx, dy int32)

Move moves all Shapes in the Space by the displacement provided.

func (*Space) Remove Uses

func (sp *Space) Remove(shapes ...Shape)

Remove removes the designated Shapes from the Space.

func (*Space) RemoveTags Uses

func (sp *Space) RemoveTags(tags ...string)

RemoveTags removes the provided tags from all Shapes contained within the Space.

func (*Space) Resolve Uses

func (sp *Space) Resolve(checkingShape Shape, deltaX, deltaY int32) Collision

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 Uses

func (sp *Space) SetData(data interface{})

SetData sets the pointer provided to the Data field of all Shapes within the Space.

func (*Space) SetXY Uses

func (sp *Space) SetXY(x, y int32)

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.

func (*Space) String Uses

func (sp *Space) String() string

func (*Space) WouldBeColliding Uses

func (sp *Space) WouldBeColliding(other Shape, dx, dy int32) bool

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).

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.

Package resolv imports 3 packages (graph) and is imported by 4 packages. Updated 2019-08-22. Refresh now. Tools for package owners.