collision

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2024 License: GPL-3.0 Imports: 5 Imported by: 0

README

from github.com/solarlune/resolv

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(spaceWidth, spaceHeight, cellWidth, cellHeight int)

spaceWidth and spaceHeight is the width and height of the Space (usually in pixels), which is then populated with cells of size cellWidth by cellHeight. Generally, you want cells to be the size of the smallest collide-able colliders in your game, and you want to move Colliders at a maximum speed of one cell size per inspector check to avoid missing any possible inspectors.

Types

type Cell

type Cell struct {
	X, Y      int         // The X and Y position of the cell in the Space - note that this is in Grid position, not World position.
	Colliders []*Collider // The Colliders that a Cell contains.
}

Cell is used to contain and organize Collider information.

func (*Cell) Contains

func (cell *Cell) Contains(collider *Collider) bool

Contains returns whether a Cell contains the specified Collider at its position.

func (*Cell) ContainsTags

func (cell *Cell) ContainsTags(tags ...string) bool

ContainsTags returns whether a Cell contains an Collider that has the specified tag at its position.

func (*Cell) Occupied

func (cell *Cell) Occupied() bool

Occupied returns whether a Cell contains any Colliders at all.

type Circle

type Circle struct {
	// contains filtered or unexported fields
}

func NewCircle

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

NewCircle returns a new Circle, with its center at the X and Y position given, and with the defined radius.

func (*Circle) Bounds

func (circle *Circle) Bounds() (vector2.Vector, vector2.Vector)

Bounds returns the top-left and bottom-right corners of the Circle.

func (*Circle) Clone

func (circle *Circle) Clone() IShape

func (*Circle) Intersection

func (circle *Circle) Intersection(dx, dy float64, other IShape) *ContactSet

Intersection tests to see if a Circle intersects with the other given Shape. dx and dy are delta movement variables indicating movement to be applied before the intersection check (thereby allowing you to see if a Shape would collide with another if it were in a different relative location). If an Intersection is found, a ContactSet will be returned, giving information regarding the intersection.

func (*Circle) IntersectionForEach

func (c *Circle) IntersectionForEach(dx, dy float64, f func(c *ContactSet) bool, others ...IShape)

IntersectionForEach runs a specified function for each contact set caused by contact with any of the shapes passed. If the custom function returns false, then the intersection testing stops iterating through further colliders.

func (*Circle) IntersectionPointsCircle

func (circle *Circle) IntersectionPointsCircle(other *Circle) []vector2.Vector

IntersectionPointsCircle returns the intersection points of the two circles provided.

func (*Circle) Move

func (circle *Circle) Move(x, y float64)

Move translates the Circle by the designated X and Y values.

func (*Circle) MoveVec

func (circle *Circle) MoveVec(vec vector2.Vector)

MoveVec translates the Circle by the designated vector2.Vector.

func (*Circle) PointInside

func (circle *Circle) PointInside(point vector2.Vector) bool

PointInside returns if the given vector2.Vector is inside of the circle.

func (*Circle) Position

func (circle *Circle) Position() vector2.Vector

Position() returns the X and Y position of the Circle.

func (*Circle) Radius

func (circle *Circle) Radius() float64

Radius returns the radius of the Circle.

func (*Circle) Rotate

func (circle *Circle) Rotate(radians float64)

Circles can't rotate, of course. This function is just a stub to make them acceptable as IShapes.

func (*Circle) Rotation

func (circle *Circle) Rotation() float64

Circles can't rotate, of course. This function is just a stub to make them acceptable as IShapes.

func (*Circle) Scale

func (circle *Circle) Scale() vector2.Vector

Scale returns the scale multiplier of the Circle, twice; this is to have it adhere to the Shape interface.

func (*Circle) SetPosition

func (circle *Circle) SetPosition(x, y float64)

SetPosition sets the center position of the Circle using the X and Y values given.

func (*Circle) SetPositionVec

func (circle *Circle) SetPositionVec(vec vector2.Vector)

SetPosition sets the center position of the Circle using the vector2.Vector given.

func (*Circle) SetRadius

func (circle *Circle) SetRadius(radius float64)

SetRadius sets the radius of the Circle, updating the scale multiplier to reflect this change.

func (*Circle) SetRotation

func (circle *Circle) SetRotation(rotation float64)

Circles can't rotate, of course. This function is just a stub to make them acceptable as IShapes.

func (*Circle) SetScale

func (circle *Circle) SetScale(w, h float64)

SetScale sets the scale multiplier of the Circle (this is W and H to have it adhere to IShape as a contract; in truth, the Circle's radius will be set to 0.5 * the maximum out of the width and height height values given).

func (*Circle) SetScaleVec

func (circle *Circle) SetScaleVec(scale vector2.Vector)

SetScaleVec sets the scale multiplier of the Circle (this is W and H to have it adhere to IShape as a contract; in truth, the Circle's radius will be set to 0.5 * the maximum out of the width and height height values given).

type Collider

type Collider struct {
	Shape         IShape         // A shape for more specific inspector-checking.
	Space         *Space         // Reference to the Space the Collider exists within
	Position      vector2.Vector // The position of the Collider in the Space
	Size          vector2.Vector // The size of the Collider in the Space
	TouchingCells []*Cell        // An array of Cells the Collider is touching
	Data          interface{}    // A pointer to a user-definable collider
	// contains filtered or unexported fields
}

Collider represents an collider that can be spread across one or more Cells in a Space. An Collider is essentially an AABB (Axis-Aligned Bounding Box) Rectangle.

func NewCollider

func NewCollider(x, y, w, h float64, tags ...string) *Collider

NewCollider returns a new Collider of the specified position and size.

func RandNewCollider

func RandNewCollider(w, h float64, tags ...string) *Collider

RandNewCollider returns a new Collider of the

func (*Collider) AddTags

func (col *Collider) AddTags(tags ...string)

AddTags adds tags to the Collider.

func (*Collider) AddToIgnoreList

func (col *Collider) AddToIgnoreList(ignoreObj *Collider)

AddToIgnoreList adds the specified Collider to the Collider's internal inspector ignoral list. Cells that contain the specified Collider will not be counted when calling Check().

func (*Collider) Bottom

func (col *Collider) Bottom() float64

Bottom returns the bottom Y coordinate of the Collider (i.e. col.Y + col.H).

func (*Collider) BoundsToSpace

func (col *Collider) BoundsToSpace(dx, dy float64) (int, int, int, int)

BoundsToSpace returns the Space coordinates of the shape (x, y, w, and h), given its world position and size, and a supposed movement of dx and dy.

func (*Collider) CellPosition

func (col *Collider) CellPosition() (int, int)

CellPosition returns the cellular position of the Collider's center in the Space.

func (*Collider) Center

func (col *Collider) Center() vector2.Vector

Center returns the center position of the Collider.

func (*Collider) Check

func (col *Collider) Check(dx, dy float64, tags ...string) *Inspector

Check checks the space around the collider using the designated delta movement (dx and dy). This is done by querying the containing Space's Cells so that it can see if moving it would coincide with a cell that houses another Collider (filtered using the given selection of tag strings). If so, Check returns a Inspector. If no colliders are found or the Collider does not exist within a Space, this function returns nil.

func (*Collider) Clone

func (col *Collider) Clone() *Collider

Clone clones the Collider with its properties into another Collider. It also clones the Collider's Shape (if it has one).

func (*Collider) Destruction

func (col *Collider) Destruction()

销毁

func (*Collider) HasTags

func (col *Collider) HasTags(tags ...string) bool

HasTags indicates if an Collider has any of the tags indicated.

func (*Collider) Move

func (col *Collider) Move(move vector2.Vector)

func (*Collider) Overlaps

func (col *Collider) Overlaps(other *Collider) bool

Overlaps returns if an Collider overlaps another Collider.

func (*Collider) RemoveFromIgnoreList

func (col *Collider) RemoveFromIgnoreList(ignoreObj *Collider)

RemoveFromIgnoreList removes the specified Collider from the Collider's internal inspector ignoral list. Colliders removed from this list will once again be counted for Check().

func (*Collider) RemoveTags

func (col *Collider) RemoveTags(tags ...string)

RemoveTags removes tags from the Collider.

func (*Collider) Right

func (col *Collider) Right() float64

Right returns the right X coordinate of the Collider (i.e. col.X + col.W).

func (*Collider) SetBottom

func (col *Collider) SetBottom(y float64)

SetBottom sets the Y position of the Collider so that the bottom edge is at the Y position given.

func (*Collider) SetBounds

func (col *Collider) SetBounds(topLeft, bottomRight vector2.Vector)

func (*Collider) SetCenter

func (col *Collider) SetCenter(x, y float64)

SetCenter sets the Collider such that its center is at the X and Y position given.

func (*Collider) SetCenterVec

func (col *Collider) SetCenterVec(pos vector2.Vector)

SetCenterVec sets the Collider such that its center is at the X and Y position given.

func (*Collider) SetRight

func (col *Collider) SetRight(x float64)

SetRight sets the X position of the Collider so the right edge is at the X position given.

func (*Collider) SetShape

func (col *Collider) SetShape(shape IShape)

SetShape sets the Shape on the Collider, in case you need to use precise per-Shape intersection detection. SetShape calls Collider.Update() as well, so that it's able to update the Shape's position to match its Collider as necessary. (If you don't use this, the Shape's position might not match the Collider's, depending on if you set the Shape after you added the Collider to a Space and if you don't call Collider.Update() yourself afterwards.)

func (*Collider) SharesCells

func (col *Collider) SharesCells(other *Collider) bool

SharesCells returns whether the Collider occupies a cell shared by the specified other Collider.

func (*Collider) SharesCellsTags

func (col *Collider) SharesCellsTags(tags ...string) bool

SharesCellsTags returns if the Cells the Collider occupies have an collider with the specified tags.

func (*Collider) Tags

func (col *Collider) Tags() []string

Tags returns the tags an Collider has.

func (*Collider) Update

func (col *Collider) Update()

Update updates the collider's association to the Cells in the Space. This should be called whenever an Collider is moved. This is automatically called once when creating the Collider, so you don't have to call it for static colliders.

type ContactSet

type ContactSet struct {
	Points []vector2.Vector // Slice of points indicating contact between the two Shapes.
	MTV    vector2.Vector   // Minimum Translation vector2.Vector; this is the vector to move a Shape on to move it outside of its contacting Shape.
	Center vector2.Vector   // Center of the Contact set; this is the average of all Points contained within the Contact Set.
}

func NewContactSet

func NewContactSet() *ContactSet

func (*ContactSet) BottommostPoint

func (cs *ContactSet) BottommostPoint() vector2.Vector

BottommostPoint returns the bottom-most point out of the ContactSet's Points slice. If the Points slice is empty somehow, this returns nil.

func (*ContactSet) LeftmostPoint

func (cs *ContactSet) LeftmostPoint() vector2.Vector

LeftmostPoint returns the left-most point out of the ContactSet's Points slice. If the Points slice is empty somehow, this returns nil.

func (*ContactSet) RightmostPoint

func (cs *ContactSet) RightmostPoint() vector2.Vector

RightmostPoint returns the right-most point out of the ContactSet's Points slice. If the Points slice is empty somehow, this returns nil.

func (*ContactSet) TopmostPoint

func (cs *ContactSet) TopmostPoint() vector2.Vector

TopmostPoint returns the top-most point out of the ContactSet's Points slice. If the Points slice is empty somehow, this returns nil.

type ConvexPolygon

type ConvexPolygon struct {
	Points []vector2.Vector // Points represents the points constructing the ConvexPolygon.

	// X, Y           float64  // X and Y are the position of the ConvexPolygon.
	// ScaleW, ScaleH float64 // The width and height for scaling
	Closed bool // Closed is whether the ConvexPolygon is closed or not; only takes effect if there are more than 2 points.
	// contains filtered or unexported fields
}

ConvexPolygon represents a series of points, connected by lines, constructing a convex shape. The polygon has a position, a scale, a rotation, and may or may not be closed.

func NewConvexPolygon

func NewConvexPolygon(x, y float64, points ...float64) *ConvexPolygon

NewConvexPolygon creates a new convex polygon at the position given, from the provided set of X and Y positions of 2D points (or vertices). You don't need to pass any points at this stage, but if you do, you should pass whole pairs. The points should generally be ordered clockwise, from X and Y of the first, to X and Y of the last. For example: NewConvexPolygon(30, 20, 0, 0, 10, 0, 10, 10, 0, 10) would create a 10x10 convex polygon square, with the vertices at {0,0}, {10,0}, {10, 10}, and {0, 10}, with the polygon itself occupying a position of 30, 20. You can also pass the points using vectors with ConvexPolygon.AddPointsVec().

func NewConvexPolygonVec

func NewConvexPolygonVec(position vector2.Vector, points ...vector2.Vector) *ConvexPolygon

func NewLine

func NewLine(x1, y1, x2, y2 float64) *ConvexPolygon

NewLine is a helper function that returns a ConvexPolygon composed of a single line. The Polygon has a position of x1, y1, and the line stretches to x2-x1 and y2-y1.

func NewRectangle

func NewRectangle(x, y, w, h float64) *ConvexPolygon

NewRectangle returns a rectangular ConvexPolygon at the {x, y} position given with the vertices ordered in clockwise order, positioned at {0, 0}, {w, 0}, {w, h}, {0, h}. TODO: In actuality, an AABBRectangle should be its own "thing" with its own optimized Intersection code check.

func (*ConvexPolygon) AddPoints

func (cp *ConvexPolygon) AddPoints(vertexPositions ...float64)

AddPoints allows you to add points to the ConvexPolygon with a slice or selection of float64s, with each pair indicating an X or Y value for a point / vertex (i.e. AddPoints(0, 1, 2, 3) would add two points - one at {0, 1}, and another at {2, 3}).

func (*ConvexPolygon) AddPointsVec

func (cp *ConvexPolygon) AddPointsVec(points ...vector2.Vector)

AddPointsVec allows you to add points to the ConvexPolygon with a slice of Vectors, each indicating a point / vertex.

func (*ConvexPolygon) Bounds

func (cp *ConvexPolygon) Bounds() (vector2.Vector, vector2.Vector)

Bounds returns two Vectors, comprising the top-left and bottom-right positions of the bounds of the ConvexPolygon, post-transformation.

func (*ConvexPolygon) Center

func (cp *ConvexPolygon) Center() vector2.Vector

Center returns the transformed Center of the ConvexPolygon.

func (*ConvexPolygon) Clone

func (cp *ConvexPolygon) Clone() IShape

Clone returns a clone of the ConvexPolygon as an IShape.

func (*ConvexPolygon) ContainedBy

func (cp *ConvexPolygon) ContainedBy(otherShape IShape) bool

ContainedBy returns if the ConvexPolygon is wholly contained by the other shape provided.

func (*ConvexPolygon) FlipH

func (cp *ConvexPolygon) FlipH()

FlipH flips the ConvexPolygon's vertices horizontally, across the polygon's width, according to their initial offset when adding the points.

func (*ConvexPolygon) FlipV

func (cp *ConvexPolygon) FlipV()

FlipV flips the ConvexPolygon's vertices vertically according to their initial offset when adding the points.

func (*ConvexPolygon) Intersection

func (cp *ConvexPolygon) Intersection(dx, dy float64, other IShape) *ContactSet

Intersection tests to see if a ConvexPolygon intersects with the other given Shape. dx and dy are the delta movement to be applied before the intersection check (thereby allowing you to see if a Shape would collide with another if it were in a different relative location). If an Intersection is found, a ContactSet will be returned, giving information regarding the intersection.

func (*ConvexPolygon) IntersectionForEach

func (p *ConvexPolygon) IntersectionForEach(dx, dy float64, f func(c *ContactSet) bool, others ...IShape)

IntersectionForEach runs a specified function for each contact set caused by contact with any of the shapes passed. If the custom function returns false, then the intersection testing stops iterating through further colliders.

func (*ConvexPolygon) Lines

func (cp *ConvexPolygon) Lines() []*collidingLine

Lines returns a slice of transformed internalLines composing the ConvexPolygon.

func (*ConvexPolygon) Move

func (cp *ConvexPolygon) Move(x, y float64)

Move translates the ConvexPolygon by the designated X and Y values.

func (*ConvexPolygon) MoveVec

func (cp *ConvexPolygon) MoveVec(vec vector2.Vector)

MoveVec translates the ConvexPolygon by the designated vector2.Vector.

func (*ConvexPolygon) PointInside

func (polygon *ConvexPolygon) PointInside(point vector2.Vector) bool

PointInside returns if a Point (a vector2.Vector) is inside the ConvexPolygon.

func (*ConvexPolygon) Position

func (cp *ConvexPolygon) Position() vector2.Vector

Position returns the position of the ConvexPolygon.

func (*ConvexPolygon) Project

func (cp *ConvexPolygon) Project(axis vector2.Vector) Projection

Project projects (i.e. flattens) the ConvexPolygon onto the provided axis.

func (*ConvexPolygon) RecenterPoints

func (cp *ConvexPolygon) RecenterPoints()

RecenterPoints recenters the vertices in the polygon, such that they are all equidistant from the center. For example, say you had a polygon with the following three points: {0, 0}, {10, 0}, {0, 16}. After calling cp.RecenterPoints(), the polygon's points would be at {-5, -8}, {5, -8}, {-5, 8}.

func (*ConvexPolygon) ReverseVertexOrder

func (cp *ConvexPolygon) ReverseVertexOrder()

ReverseVertexOrder reverses the vertex ordering of the ConvexPolygon.

func (*ConvexPolygon) Rotate

func (polygon *ConvexPolygon) Rotate(radians float64)

Rotate is a helper function to rotate a ConvexPolygon by the radians given.

func (*ConvexPolygon) Rotation

func (polygon *ConvexPolygon) Rotation() float64

Rotation returns the rotation (in radians) of the ConvexPolygon.

func (*ConvexPolygon) SATAxes

func (cp *ConvexPolygon) SATAxes() []vector2.Vector

SATAxes returns the axes of the ConvexPolygon for SAT intersection testing.

func (*ConvexPolygon) Scale

func (polygon *ConvexPolygon) Scale() vector2.Vector

Scale returns the scale multipliers of the ConvexPolygon.

func (*ConvexPolygon) SetPosition

func (cp *ConvexPolygon) SetPosition(x, y float64)

SetPosition sets the position of the ConvexPolygon. The offset of the vertices compared to the X and Y position is relative to however you initially defined the polygon and added the vertices.

func (*ConvexPolygon) SetPositionVec

func (cp *ConvexPolygon) SetPositionVec(vec vector2.Vector)

SetPositionVec allows you to set the position of the ConvexPolygon using a vector2.Vector. The offset of the vertices compared to the X and Y position is relative to however you initially defined the polygon and added the vertices.

func (*ConvexPolygon) SetRotation

func (polygon *ConvexPolygon) SetRotation(radians float64)

SetRotation sets the rotation for the ConvexPolygon; note that the rotation goes counter-clockwise from 0 to pi, and then from -pi at 180 down, back to 0. This rotation scheme follows the way math.Atan2() works.

func (*ConvexPolygon) SetScale

func (polygon *ConvexPolygon) SetScale(x, y float64)

SetScale sets the scale multipliers of the ConvexPolygon.

func (*ConvexPolygon) SetScaleVec

func (polygon *ConvexPolygon) SetScaleVec(vec vector2.Vector)

SetScaleVec sets the scale multipliers of the ConvexPolygon using the provided vector2.Vector.

func (*ConvexPolygon) Transformed

func (cp *ConvexPolygon) Transformed() []vector2.Vector

Transformed returns the ConvexPolygon's points / vertices, transformed according to the ConvexPolygon's position.

type IShape

type IShape interface {
	// Intersection tests to see if a Shape intersects with the other given Shape. dx and dy are delta movement variables indicating
	// movement to be applied before the intersection check (thereby allowing you to see if a Shape would collide with another if it
	// were in a different relative location). If an Intersection is found, a ContactSet will be returned, giving information regarding
	// the intersection.
	Intersection(dx, dy float64, other IShape) *ContactSet
	// IntersectionForEach runs a specified function for each contact set caused by contact with any of
	// the shapes passed. If the custom function returns false, then the intersection testing stops
	// iterating through further colliders.
	IntersectionForEach(dx, dy float64, f func(c *ContactSet) bool, others ...IShape)
	// Bounds returns the top-left and bottom-right points of the Shape.
	Bounds() (vector2.Vector, vector2.Vector)
	// Position returns the X and Y position of the Shape.
	Position() vector2.Vector
	// SetPosition allows you to place a Shape at another location.
	SetPosition(x, y float64)
	// SetPositionVec allows you to place a Shape at another location using a vector2.Vector.
	SetPositionVec(position vector2.Vector)

	// Rotation returns the current rotation value for the Shape.
	Rotation() float64

	// SetRotation sets the rotation value for the Shape.
	// Note that the rotation goes counter-clockwise from 0 at right to pi/2 in the upwards direction,
	// pi or -pi at left, -pi/2 in the downwards direction, and finally back to 0.
	// This can be visualized as follows:
	//
	//   U
	// L   R
	//   D
	//
	// R: 0
	// U: pi/2
	// L: pi / -pi
	// D: -pi/2
	SetRotation(radians float64)

	// Rotate rotates the IShape by the radians provided.
	// Note that the rotation goes counter-clockwise from 0 at right to pi/2 in the upwards direction,
	// pi or -pi at left, -pi/2 in the downwards direction, and finally back to 0.
	// This can be visualized as follows:
	//
	//   U
	// L   R
	//   D
	//
	// R: 0
	// U: pi/2
	// L: pi / -pi
	// D: -pi/2
	Rotate(radians float64)

	Scale() vector2.Vector // Returns the scale of the IShape (the radius for Circles).

	// Sets the overall scale of the IShape; 1.0 is 100% scale, 2.0 is 200%, and so on.
	// The greater of these values is used for the radius for Circles.
	SetScale(w, h float64)

	// Sets the overall scale of the IShape using the provided vector2.Vector; 1.0 is 100% scale, 2.0 is 200%, and so on.
	// The greater of these values is used for the radius for Circles.
	SetScaleVec(vec vector2.Vector)

	// Move moves the IShape by the x and y values provided.
	Move(x, y float64)
	// MoveVec moves the IShape by the movement values given in the vector provided.
	MoveVec(vec vector2.Vector)

	// Clone duplicates the IShape.
	Clone() IShape
}

type Inspector

type Inspector struct {
	Colliders []*Collider // Slice of colliders that were collided with; sorted according to distance to calling Collider.
	Cells     []*Cell     // Slice of cells that were collided with; sorted according to distance to calling Collider.
	// contains filtered or unexported fields
}

Inspector contains the results of an Collider.Check() call, and represents a inspector between an Collider and cells that contain other Colliders. The Colliders array indicate the Colliders collided with.

func (*Inspector) CollidersByTags

func (cc *Inspector) CollidersByTags(tags ...string) []*Collider

CollidersByTags returns a slice of Colliders from the cells reported by a Inspector collider by searching for Colliders with a specific set of tags. This slice does not contain the Collider that called Check().

func (*Inspector) ContactWithCell

func (cc *Inspector) ContactWithCell(cell *Cell) vector2.Vector

ContactWithCell returns the delta to move to have the checking collider come into contact with the specified Cell.

func (*Inspector) ContactWithCollider

func (cc *Inspector) ContactWithCollider(collider *Collider) vector2.Vector

ContactWithCollider returns the delta to move to have the checking collider come into contact with the specified Collider.

func (*Inspector) HasTags

func (cc *Inspector) HasTags(tags ...string) bool

HasTags returns whether any colliders within the Inspector have all of the specified tags. This slice does not contain the Collider that called Check().

func (*Inspector) SlideAgainstCell

func (cc *Inspector) SlideAgainstCell(cell *Cell, avoidTags ...string) (vector2.Vector, bool)

SlideAgainstCell returns how much distance the calling Collider can slide to avoid a inspector with the targetCollider, and a boolean indicating if such a slide was possible. This only works on vertical and horizontal axes (x and y directly), primarily for platformers / top-down games. avoidTags is a sequence of tags (as strings) to indicate when sliding is valid (i.e. if a Cell contains an Collider that has the tag given in the avoidTags slice, then sliding CANNOT happen).

type Projection

type Projection struct {
	Min, Max float64
}

}

func (Projection) IsInside

func (projection Projection) IsInside(other Projection) bool

IsInside returns whether the Projection is wholly inside of the other, provided Projection.

func (Projection) Overlap

func (projection Projection) Overlap(other Projection) float64

Overlap returns the amount that a Projection is overlapping with the other, provided Projection. Credit to https://dyn4j.org/2010/01/sat/#sat-nointer

func (Projection) Overlapping

func (projection Projection) Overlapping(other Projection) bool

Overlapping returns whether a Projection is overlapping with the other, provided Projection. Credit to https://www.sevenson.com.au/programming/sat/

type Space

type Space struct {
	Cells                 [][]*Cell
	CellWidth, CellHeight int // Width and Height of each Cell in "world-space" / pixels / whatever
}

Space represents a inspector space. Internally, each Space contains a 2D array of Cells, with each Cell being the same size. Cells contain information on which Colliders occupy those spaces.

func (*Space) Add

func (sp *Space) Add(colliders ...*Collider)

Add adds the specified Colliders to the Space, updating the Space's cells to refer to the Collider.

func (*Space) Cell

func (sp *Space) Cell(x, y int) *Cell

Cell returns the Cell at the given cellular / spatial (not world) X and Y position in the Space. If the X and Y position are out of bounds, Cell() will return nil.

func (*Space) CellsInLine

func (sp *Space) CellsInLine(startX, startY, endX, endY int) []*Cell

func (*Space) CheckCells

func (sp *Space) CheckCells(x, y, w, h int, tags ...string) []*Collider

CheckCells checks a set of cells (from x,y to x + w, y + h in cellular coordinates) and returns a slice of the colliders found within those Cells. The colliders must have any of the tags provided (if any are provided).

func (*Space) CheckWorld

func (sp *Space) CheckWorld(x, y, w, h float64, tags ...string) []*Collider

CheckWorld checks the cells of the Grid with the given world coordinates. Internally, this is just syntactic sugar for calling Space.WorldToSpace() on the position and size given.

func (*Space) CheckWorldVec

func (sp *Space) CheckWorldVec(pos, size vector2.Vector, tags ...string) []*Collider

CheckWorldVec checks the cells of the Grid with the given world coordinates. This function takes vectors for the position and size of the checked area. Internally, this is just syntactic sugar for calling Space.WorldToSpace() on the position and size given.

func (*Space) Colliders

func (sp *Space) Colliders() []*Collider

Colliders loops through all Cells in the Space (from top to bottom, and from left to right) to return all Colliders that exist in the Space. Of course, each Collider is counted only once.

func (*Space) Height

func (sp *Space) Height() int

Height returns the height of the Space grid in Cells (so a 320x240 Space with 16x16 cells would have a height of 15).

func (*Space) Remove

func (sp *Space) Remove(colliders ...*Collider)

Remove removes the specified Colliders from being associated with the Space. This should be done whenever an Collider is removed from the game.

func (*Space) Resize

func (sp *Space) Resize(width, height int)

Resize resizes the internal Cells array.

func (*Space) SpaceToWorld

func (sp *Space) SpaceToWorld(x, y int) (float64, float64)

SpaceToWorld converts from a position in the Space (on a grid) to a world-based position, given the size of the Space when first created.

func (*Space) SpaceToWorldVec

func (sp *Space) SpaceToWorldVec(x, y int) vector2.Vector

func (*Space) UnregisterAllColliders

func (sp *Space) UnregisterAllColliders()

UnregisterAllColliders unregisters all Colliders registered to Cells in the Space.

func (*Space) Width

func (sp *Space) Width() int

Width returns the width of the Space grid in Cells (so a 320x240 Space with 16x16 cells would have a width of 20).

func (*Space) WorldToSpace

func (sp *Space) WorldToSpace(x, y float64) (int, int)

WorldToSpace converts from a world position (x, y) to a position in the Space (a grid-based position).

func (*Space) WorldToSpaceVec

func (sp *Space) WorldToSpaceVec(position vector2.Vector) (int, int)

WorldToSpaceVec converts from a world position vector2.Vector to a position in the Space (a grid-based position).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL