physics

package
v0.0.0-...-9aa04a4 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2019 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package physics defines physical components and transformations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Halt

func Halt(b *Body, dir pixel.Vec) bool

Halt is a force function that stops motion in the given direction.

Types

type Acc

type Acc struct {
	Amount   float64
	MaxSpeed float64
}

Acc accelerates a body.

func (Acc) Apply

func (f Acc) Apply(b *Body, dir pixel.Vec) bool

type AirborneSubscriber

type AirborneSubscriber = func()

type Body

type Body struct {
	Acc  pixel.Vec
	Vel  pixel.Vec
	Pos  pixel.Vec
	Rot  float64
	Size *Rect

	Collision *CollisionComponent
	Movement  *MovementComponent
	Gravity   *GravityComponent
	// contains filtered or unexported fields
}

Body represents a physical object.

The object has a position which is affected each game turn by its velocity and Gravity. TODO: Add more.

func NewBody

func NewBody(pos pixel.Vec, gravity float64) *Body

func (*Body) AddAirborneSubscriber

func (b *Body) AddAirborneSubscriber(s AirborneSubscriber)

func (*Body) AddCollisionSubscriber

func (b *Body) AddCollisionSubscriber(s CollisionSubscriber)

func (Body) Bounds

func (b Body) Bounds() *Rect

func (*Body) Down

func (b *Body) Down() pixel.Vec

func (*Body) HandleCollision

func (b *Body) HandleCollision(event *CollisionEvent)

func (*Body) Left

func (b *Body) Left() pixel.Vec

func (*Body) Right

func (b *Body) Right() pixel.Vec

func (*Body) Speed

func (b *Body) Speed() float64

func (*Body) SpeedAlong

func (b *Body) SpeedAlong(direction pixel.Vec) float64

func (*Body) Up

func (b *Body) Up() pixel.Vec

func (*Body) Update

func (b *Body) Update(ptx *PhysicsContext)

type CollisionComponent

type CollisionComponent struct{}

func (*CollisionComponent) Update

func (c *CollisionComponent) Update(ptx *PhysicsContext, body *Body)

type CollisionEvent

type CollisionEvent struct {
	// Whether the collider collided with the left side of the target
	// object.
	LeftSide bool

	// Whether the collider collided with the right side of the target
	// object.
	RightSide bool

	// Whether the collider collided with the top side of the target
	// object.
	TopSide bool

	// Whether the collider collided with the bottom side of the target
	// object.
	BottomSide bool

	// The amount the collider needs to adjust its X position to stop
	// colliding with the target object.
	ResolveX float64

	// The amount the collider needs to adjust its Y position to stop
	// colliding with the target object.
	ResolveY float64

	// The direction the collider was moving when the collision occurred.
	Vec pixel.Vec
	// contains filtered or unexported fields
}

func (*CollisionEvent) Resolve

func (e *CollisionEvent) Resolve(body *Body)

type CollisionSubscriber

type CollisionSubscriber = func(CollisionEvent)

type CollisionSystem

type CollisionSystem struct {
	BoundingBox *Rect
	// contains filtered or unexported fields
}

func (*CollisionSystem) AddTerrain

func (cs *CollisionSystem) AddTerrain(rect *Rect)

func (*CollisionSystem) GetBoundingBoxCollision

func (cs *CollisionSystem) GetBoundingBoxCollision(rect *Rect) *CollisionEvent

Returns a CollisionEvent describing rect's collision with the BoundingBox, if any.

func (*CollisionSystem) GetCollision

func (cs *CollisionSystem) GetCollision(a, b *Rect) *CollisionEvent

Returns a CollisionEvent describing a's collision with b, if any.

func (*CollisionSystem) GetTerrainCollision

func (cs *CollisionSystem) GetTerrainCollision(rect *Rect) *CollisionEvent

Returns a CollisionEvent describing rect's collision with map terrain objects, if any.

type Dec

type Dec struct {
	Fraction float64
	MinSpeed float64
}

Dec decelerates a body to a fraction of its current velocity

func (Dec) Apply

func (f Dec) Apply(b *Body) bool

type Direction

type Direction int
const (
	Right Direction = iota
	Left
	Up
	Down
)

type GravityComponent

type GravityComponent struct {
	// The amount of change to a Body's velocity per frame.
	// For example, to accelerate an object at 10 units squared per
	// second at 60 fps, use Acceleration = 10 / 60.
	Acceleration float64
}

GravityComponent applies a constant acceleration to a Body.

Acceleration is applied relative the given Body's reference frame.

func (GravityComponent) Update

func (c GravityComponent) Update(_ *PhysicsContext, body *Body)

type MovementComponent

type MovementComponent struct{}

MovementComponent updates a Body's position accoriding to it velocity.

func (MovementComponent) Update

func (c MovementComponent) Update(_ *PhysicsContext, body *Body)

type PhysicsContext

type PhysicsContext struct {
	CollisionSystem *CollisionSystem
}

type Rect

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

func NewRect

func NewRect(rect pixel.Rect) *Rect

func (Rect) Center

func (r Rect) Center() pixel.Vec

func (Rect) Height

func (r Rect) Height() float64

func (Rect) Intersects

func (r Rect) Intersects(other Rect) bool

func (Rect) Max

func (r Rect) Max() pixel.Vec

func (Rect) Min

func (r Rect) Min() pixel.Vec

func (Rect) Moved

func (r Rect) Moved(to pixel.Vec) *Rect

func (Rect) Width

func (r Rect) Width() float64

type ReferenceFrame

type ReferenceFrame struct {
	Gravity pixel.Vec
}

func (*ReferenceFrame) Down

func (rf *ReferenceFrame) Down() pixel.Vec

func (*ReferenceFrame) Left

func (rf *ReferenceFrame) Left() pixel.Vec

func (*ReferenceFrame) Right

func (rf *ReferenceFrame) Right() pixel.Vec

func (*ReferenceFrame) Up

func (rf *ReferenceFrame) Up() pixel.Vec

Jump to

Keyboard shortcuts

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