Documentation
¶
Index ¶
- type Body
- func (body *Body) Extents() (x, y, w, h, r, b float32)
- func (body *Body) GetResponse(tag string) string
- func (body *Body) GetResponses() map[string]string
- func (body *Body) HasTag(tags ...string) bool
- func (body *Body) IsStatic() bool
- func (body *Body) Move(x, y float32) (gx, gy float32, cols []*Collision)
- func (body *Body) Position() (x, y float32)
- func (body *Body) Remove()
- func (body *Body) SetResponse(tag, resp string)
- func (body *Body) SetResponses(respMap map[string]string)
- func (body *Body) SetStatic(isStatic bool)
- func (body *Body) Tag() string
- func (body *Body) Update(x, y float32)
- type Collision
- type Point
- type Resp
- type World
- func (world *World) Add(tag string, left, top, w, h float32) *Body
- func (world *World) AddResponse(name string, response Resp)
- func (world *World) Project(body *Body, goalX, goalY float32) []*Collision
- func (world *World) QueryPoint(x, y float32, tags ...string) []*Body
- func (world *World) QueryRect(x, y, w, h float32, tags ...string) []*Body
- func (world *World) QuerySegment(x1, y1, x2, y2 float32, tags ...string) []*Body
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Body ¶
type Body struct {
ID uint32
// contains filtered or unexported fields
}
Body represents a rectangle that will collide with other rectangles/bodies
func (*Body) GetResponse ¶
GetResponse will return the filter name for the tag passed. If the tag is not defined in the response map then the default reponse will be returned
func (*Body) GetResponses ¶
GetResponses will return the response map set on this body
func (*Body) HasTag ¶
HasTag will check a list of tags to see if this body matches any of them. This is good for checking groups of object that collide.
func (*Body) IsStatic ¶
IsStatic will return if the body is a static body or not. See SetStatic for why it would be a static body
func (*Body) Move ¶
Move moves a body to a new location and will return the point where the body managed to get to (gx, gy). It will also return any collisions that happened inbetween the movements.
func (*Body) Remove ¶
func (body *Body) Remove()
Remove will remove this body from the world and will no longer collide with any other bodies.
func (*Body) SetResponse ¶
SetResponse will set an entry in the response map for the provided tag.
func (*Body) SetResponses ¶
SetResponses will set a map of responses for a body. This map defines how this body will react to certain collisions. The map is formatted map[object_tag]filter_name By default all items will collide and not resolve. To change the default behaviour use the "default" entry in the response map. For instance on an item that would bounce like a ball you would call `body.SetResponses(map[string]string{"default": "bounce"})
type Collision ¶
type Collision struct {
Intersection float32
Distance float32
Body *Body
Move Point
Normal Point
Touch Point
Data Point
RespType string
}
Collision represents a touch of two objects. The intersection is the fraction of the movement where the two items touched. Distanse is how far the two object are from each other. Body is the other body that was collided.
- Move is the amount the body needs to move to resolve the collision - Normal is the normal of the collision - Touch is the point where the two bodies first touched - Data is used for misc data that can be pass in a filter - RespType describes which filter was used to resolve the collision
type Point ¶
type Point struct {
X, Y float32
}
Point is used as data points in a collision. This could represent a touch or a normal or something else.
type Resp ¶
type Resp func(world *World, col *Collision, body *Body, goalX, goalY float32) (gx, gy float32, cols []*Collision)
Resp is a function that will handle and resolve a collision. For instance the bound filter will return the bounce goal gx gy, and then project for the new direction to make sure there are not collisions in that direction.
type World ¶
type World struct {
// contains filtered or unexported fields
}
World is the virtual world in which all these collisions happen. The world contains a grid, which contains several cells, which contains collidable bodies.
A world also has registered responses to filter collisions please see Resp for this.
func NewWorld ¶
NewWorld builds a physics world with the provided cell size. A good default is 64. It represents the size of the sides of the (squared) cells that will be used internally to provide the data. In tile based games, it's usually a multiple of the tile side size. So in a game where tiles are 32x32, cellSize will be 32, 64 or 128. In more sparse games, it can be higher.
func (*World) Add ¶
Add will create a new Body to be tracked in this world. The tag is important. It is used to decided which filter to use but also for you to decide what to do when a body collides with your new body.
left, top, w, and h describe a rectangle for the body to inhabit.
func (*World) AddResponse ¶
AddResponse will add a new filter response for this world. This is helpful if you are creating custom reactions in your world.
func (*World) Project ¶
Project will project the goal location of the provided body but not move it. This is good for checking a future location of a body and see if there are any collisions in that space.
func (*World) QueryPoint ¶
QueryPoint will return any bodies that are underneathe the point.
If tags are passed into the query then it will only return the bodies with those tags.