physics

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2021 License: Apache-2.0 Imports: 3 Imported by: 2

Documentation

Overview

Package physics provides vector types and trivial physics manipulation for them.

Index

Examples

Constants

View Source
const (
	// CUTOFF is used for rounding after floating point operations to
	// zero out vector values that are sufficiently close to zero
	CUTOFF = 0.001
)

Variables

This section is empty.

Functions

func Push

func Push(a Pushes, b Pushable) error

Push applies the force from the pushing object its target

Types

type Attachable

type Attachable interface {
	Detach()
	Attach(Vecer, float64, float64)
	AttachX(Vecer, float64)
	AttachY(Vecer, float64)
	Vecer
}

An Attachable can be attached to static or moving vectors.

Example
v1 := NewVector(0, 0)
v2 := NewVector(0, 0)
v3 := NewVector(0, 0)
v2.Attach(v1, 5, 5)
v3.Attach(v1, 0, 0)
v1.ShiftX(1)
fmt.Printf("V2: x is %f, y is %f\n", v2.X(), v2.Y())
fmt.Printf("V3: x is %f, y is %f", v3.X(), v3.Y())
Output:

V2: x is 6.000000, y is 5.000000
V3: x is 1.000000, y is 0.000000

type ForceVector

type ForceVector struct {
	Vector
	Force *float64
}

ForceVector is a vector that has some force and can operate on entites with mass

func DefaultForceVector

func DefaultForceVector(delta Vector, mass float64) ForceVector

DefaultForceVector returns a force vector that converts the mass given into a force float

func NewForceVector

func NewForceVector(direction Vector, force float64) ForceVector

NewForceVector returns a force vector

func (ForceVector) GetForce

func (f ForceVector) GetForce() ForceVector

GetForce is a self-returning call

type Mass

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

A Mass can have forces applied against it

func (*Mass) Freeze

func (m *Mass) Freeze()

Freeze changes a pushables mass such that it can no longer be pushed.

func (*Mass) GetMass

func (m *Mass) GetMass() float64

GetMass returns the mass of an object

func (*Mass) SetMass

func (m *Mass) SetMass(inMass float64) error

SetMass of an object

type Pushable

type Pushable interface {
	GetDelta() Vector
	GetMass() float64
}

Pushable is implemented by anything that has mass and directional movement, and therefore can be pushed.

type Pushes

type Pushes interface {
	GetForce() ForceVector
}

A Pushes can push Pushable things through its associated ForceVector, or how hard the Pushable should move in a given direction

type Vecer

type Vecer interface {
	Vec() Vector
}

A Vecer can be converted into a Vector

type Vector

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

A Vector is a two-dimensional point or vector used throughout oak to maintain functionality between packages.

func AngleVector

func AngleVector(angle float64) Vector

AngleVector creates a unit vector by the cosine and sine of the given angle in degrees

func MaxVector

func MaxVector(a, b Vector) Vector

MaxVector returns whichever vector has a greater magnitude

func NewVector

func NewVector(x, y float64) Vector

NewVector returns a vector with the given x and y components

func NewVector32

func NewVector32(x, y float32) Vector

NewVector32 accepts float32s (and will cast them to float64s), to create a vector

func PtrVector

func PtrVector(x, y *float64) Vector

PtrVector takes in pointers as opposed to float values-- these same pointers will be used in the returned vector and by attachments to that vector.

func (Vector) Add

func (v Vector) Add(vs ...Vector) Vector

Add combines a set of vectors through addition

func (Vector) Angle

func (v Vector) Angle() float64

Angle returns this vector as an angle in degrees

func (*Vector) Attach

func (v *Vector) Attach(a Vecer, offX, offY float64)

Attach takes in something for this vector to attach to and a set of offsets.

func (*Vector) AttachX

func (v *Vector) AttachX(a Vecer, offX float64)

AttachX performs an attachment that only attaches on the X axis.

func (*Vector) AttachY

func (v *Vector) AttachY(a Vecer, offY float64)

AttachY performs an attachment that only attaches on the Y axis.

func (Vector) Copy

func (v Vector) Copy() Vector

Copy copies a Vector

func (*Vector) Detach

func (v *Vector) Detach()

Detach modifies a vector to no longer be attached to anything.

func (*Vector) DetachX

func (v *Vector) DetachX()

DetachX modifies a vector to no longer be attached on the X Axis.

func (*Vector) DetachY

func (v *Vector) DetachY()

DetachY modifies a vector to no longer be attached on the Y Axis.

func (Vector) Distance

func (v Vector) Distance(v2 Vector) float64

Distance on two vectors returns the euclidean distance from v to v2

func (Vector) Dot

func (v Vector) Dot(v2 Vector) float64

Dot returns the dot product of the vectors

func (Vector) GetForce

func (v Vector) GetForce() ForceVector

GetForce on a non-force vector returns a zero-value for force

func (Vector) GetPos

func (v Vector) GetPos() (float64, float64)

GetPos returns both v.X() and v.Y()

func (Vector) Magnitude

func (v Vector) Magnitude() float64

Magnitude returns the magnitude of the combined components of a Vector

func (Vector) Normalize

func (v Vector) Normalize() Vector

Normalize divides both components in a vector by the vector's magnitude

func (Vector) Rotate

func (v Vector) Rotate(fs ...float64) Vector

Rotate takes in a set of angles and rotates v by their sum the input angles are assumed to be in degrees.

func (Vector) Scale

func (v Vector) Scale(fs ...float64) Vector

Scale scales a vector by a set of floating points Scale(f1,f2,f3) is equivalent to Scale(f1*f2*f3)

func (Vector) SetPos

func (v Vector) SetPos(x, y float64) Vector

SetPos is equivalent to NewVector(x,y)

func (Vector) SetX

func (v Vector) SetX(x float64) Vector

SetX returns a vector with its x component set to x

func (Vector) SetY

func (v Vector) SetY(y float64) Vector

SetY returns a vector with its y component set to y

func (Vector) ShiftX

func (v Vector) ShiftX(x float64) Vector

ShiftX is equivalent to v.X() += x

func (Vector) ShiftY

func (v Vector) ShiftY(y float64) Vector

ShiftY is equivalent to v.Y() += y

func (Vector) Sub

func (v Vector) Sub(vs ...Vector) Vector

Sub combines a set of vectors through subtraction

func (Vector) Vec

func (v Vector) Vec() Vector

Vec returns a vector itself

func (Vector) X

func (v Vector) X() float64

X returns this vector's x component

func (Vector) Xp

func (v Vector) Xp() *float64

Xp returns the real pointer behind this vector's x component

func (Vector) Y

func (v Vector) Y() float64

Y returns this vector's x component

func (Vector) Yp

func (v Vector) Yp() *float64

Yp returns the real pointer behind this vector's y component

func (Vector) Zero

func (v Vector) Zero() Vector

Zero is shorthand for NewVector(0,0), but uses the input vector

Jump to

Keyboard shortcuts

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