physics

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ImpulseIterationCount controls the number of iterations for impulse
	// solutions by the solvers.
	ImpulseIterationCount = 8

	// NudgeIterationCount controls the number of iterations for nudge
	// solutions by the solvers.
	NudgeIterationCount = 8

	// ImpulseDriftAdjustmentRatio controls the amount by which impulses should
	// try to correct positional drift.
	//
	// This is the `beta` coefficient in the Baumgarte stabilization approach.
	ImpulseDriftAdjustmentRatio = 0.2
	// NudgeDriftAdjustmentRatio controls the amount by which nudges should
	// try to correct positional drift.
	//
	// The value here is accumulated over all iterations. In fact, the total
	// remaining error is proportional to (1.0 - ratio) ^ iterations.
	//
	// Some error should be left in order to avoid jitters due to imprecise
	// integration of the correction and to leave some drift for the
	// impulse solution.
	NudgeDriftAdjustmentRatio = 0.2
)

Functions

func HollowSphereMomentOfInertia added in v0.10.0

func HollowSphereMomentOfInertia(mass, radius float64) dprec.Mat3

HollowSphereMomentOfInertia returns the moment of inertia of a hollow sphere with the specified mass and radius.

func NewCollisionGroup added in v0.10.0

func NewCollisionGroup() int

func SolidSphereMomentOfInertia added in v0.10.0

func SolidSphereMomentOfInertia(mass, radius float64) dprec.Mat3

SolidSphereMomentOfInertia returns the moment of inertia of a solid sphere with the specified mass and radius.

func SymmetricMomentOfInertia

func SymmetricMomentOfInertia(value float64) dprec.Mat3

SymmetricMomentOfInertia returns a moment of inertia tensor that represents a symmetric object across all axis.

Types

type AerodynamicShape

type AerodynamicShape struct {
	Transform
	// contains filtered or unexported fields
}

func NewAerodynamicShape added in v0.10.0

func NewAerodynamicShape(transform Transform, solver AerodynamicSolver) AerodynamicShape

func (AerodynamicShape) Transformed added in v0.10.0

func (p AerodynamicShape) Transformed(parent Transform) AerodynamicShape

Transformed returns a new Placement that is based on this one but has the specified transform applied to it.

type AerodynamicSolver added in v0.10.0

type AerodynamicSolver interface {
	Force(windSpeed dprec.Vec3, density float64) dprec.Vec3
}

AerodynamicSolver represents a shape that is affected by air or liquid motion and inflicts a force on the body.

type Body

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

Body represents a physical body that has physics act upon it.

func (Body) AngularVelocity

func (b Body) AngularVelocity() dprec.Vec3

AngularVelocity returns the angular velocity of this body.

func (Body) CollisionGroup added in v0.10.0

func (b Body) CollisionGroup() int

CollisionGroup returns the collision group for this body. Two bodies with the same collision group are not checked for collisions.

func (Body) CollisionSet added in v0.10.0

func (b Body) CollisionSet() collision.Set

CollisionSet contains the collision shapes for this body.

func (Body) Delete

func (b Body) Delete()

Delete removes this physical body.

func (Body) IntermediatePosition added in v0.18.0

func (b Body) IntermediatePosition() dprec.Vec3

IntermediatePosition returns the position of the Body as would be seen by the current frame.

NOTE: The physics engine can advance past the current frame, which is the reason for this method.

func (Body) IntermediateRotation added in v0.18.0

func (b Body) IntermediateRotation() dprec.Quat

IntermediateRotation returns the rotation of the Body as would be seen by the current frame.

Note: The physics engine can advance past the current frame, which is the reason for this method.

func (Body) Mass

func (b Body) Mass() float64

Mass returns the mass of this body in kg.

func (Body) MomentOfInertia

func (b Body) MomentOfInertia() dprec.Mat3

MomentOfInertia returns the moment of inertia, or rotational inertia of this body.

func (Body) Name

func (b Body) Name() string

// Name returns the name of this body.

func (Body) Position

func (b Body) Position() dprec.Vec3

Position returns the body's position in world space.

func (Body) Rotation added in v0.18.0

func (b Body) Rotation() dprec.Quat

Rotation returns the quaternion rotation of this body.

func (Body) SetAngularVelocity

func (b Body) SetAngularVelocity(angularVelocity dprec.Vec3)

SetAngularVelocity changes the angular velocity of this body.

func (Body) SetCollisionGroup added in v0.10.0

func (b Body) SetCollisionGroup(group int)

SetCollisionGroup changes the collision group for this body.

A value of 0 disables the collision group.

func (Body) SetMass

func (b Body) SetMass(mass float64)

SetMass changes the mass of this body.

func (Body) SetMomentOfInertia

func (b Body) SetMomentOfInertia(inertia dprec.Mat3)

SetMomentOfInertia changes the moment of inertia of this body.

func (Body) SetName

func (b Body) SetName(name string)

SetName sets a new name for this body.

func (Body) SetPosition

func (b Body) SetPosition(position dprec.Vec3)

SetPosition changes the position of this body.

func (Body) SetRotation added in v0.18.0

func (b Body) SetRotation(rotation dprec.Quat)

SetRotation changes the quaterntion rotation of this body.

func (Body) SetVelocity

func (b Body) SetVelocity(velocity dprec.Vec3)

SetVelocity changes the velocity of this body.

func (Body) Velocity

func (b Body) Velocity() dprec.Vec3

Velocity returns the velocity of this body.

type BodyDefinition added in v0.9.0

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

func (*BodyDefinition) CollisionSet added in v0.10.0

func (d *BodyDefinition) CollisionSet() collision.Set

type BodyDefinitionInfo added in v0.9.0

type BodyDefinitionInfo struct {
	Mass                   float64
	MomentOfInertia        dprec.Mat3
	FrictionCoefficient    float64
	RestitutionCoefficient float64
	DragFactor             float64
	AngularDragFactor      float64
	CollisionGroup         int
	CollisionSpheres       []collision.Sphere
	CollisionBoxes         []collision.Box
	CollisionMeshes        []collision.Mesh
	AerodynamicShapes      []AerodynamicShape
}

type BodyInfo added in v0.9.0

type BodyInfo struct {
	Name       string
	Definition *BodyDefinition
	Position   dprec.Vec3
	Rotation   dprec.Quat
}

type ConstraintSet

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

ConstraintSet represents a set of constraints.

This type is useful when multiple constraints need to be managed (enabled,disabled,deleted) as a single unit.

func (*ConstraintSet) CreateDoubleBodyConstraint

func (s *ConstraintSet) CreateDoubleBodyConstraint(primary, secondary Body, solver solver.PairConstraint) DBConstraint

CreateDoubleBodyConstraint creates a new physics constraint that acts on two bodies and enables it for this scene.

Note: Constraints creates as part of this set should not be deleted individually.

func (*ConstraintSet) CreateSingleBodyConstraint

func (s *ConstraintSet) CreateSingleBodyConstraint(body Body, solver solver.Constraint) SBConstraint

CreateSingleBodyConstraint creates a new physics constraint that acts on a single body and stores it in this set.

Note: Constraints creates as part of this set should not be deleted individually.

func (*ConstraintSet) Delete

func (s *ConstraintSet) Delete()

Delete deletes all contained constraints and this set.

func (*ConstraintSet) Enabled

func (s *ConstraintSet) Enabled() bool

Enabled returns whether at least one of the constraints in this set is enabled.

func (*ConstraintSet) SetEnabled

func (s *ConstraintSet) SetEnabled(enabled bool)

SetEnabled changes the enabled state of all constraints in this set.

type DBConstraint

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

DBConstraint represents a restriction enforced on two bodies in conjunction.

func (DBConstraint) Delete

func (c DBConstraint) Delete()

Delete removes this constraint.

func (DBConstraint) Enabled

func (c DBConstraint) Enabled() bool

Enabled returns whether this constraint will be enforced.

func (DBConstraint) Logic added in v0.18.0

Logic returns the constraint solver that will be used to enforce mathematically this constraint.

func (DBConstraint) PrimaryBody

func (c DBConstraint) PrimaryBody() Body

PrimaryBody returns the primary body on which this constraint acts.

func (DBConstraint) SecondaryBody

func (c DBConstraint) SecondaryBody() Body

SecondaryBody returns the secondary body on which this constraint acts.

func (DBConstraint) SetEnabled

func (c DBConstraint) SetEnabled(enabled bool)

SetEnabled changes whether this constraint will be enforced.

type DoubleBodyCollisionCallback added in v0.18.0

type DoubleBodyCollisionCallback func(first, second Body, active bool)

DoubleBodyCollisionCallback is a mechanism to receive notifications about collisions between two bodies.

type DoubleBodyCollisionSubscription added in v0.18.0

type DoubleBodyCollisionSubscription = observer.Subscription[DoubleBodyCollisionCallback]

DoubleBodyCollisionSubscription represents a notification subscription for double body collisions.

type DoubleBodyCollisionSubscriptionSet added in v0.18.0

type DoubleBodyCollisionSubscriptionSet = observer.SubscriptionSet[DoubleBodyCollisionCallback]

DoubleBodyCollisionSubscriptionSet represents a set of double body collision subscriptions.

func NewDoubleBodyCollisionSubscriptionSet added in v0.18.0

func NewDoubleBodyCollisionSubscriptionSet() *DoubleBodyCollisionSubscriptionSet

NewDoubleBodyCollisionSubscriptionSet creates a new DoubleBodyCollisionSubscriptionSet.

type Engine

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

Engine is the entrypoint to working with a physics simulation.

func NewEngine

func NewEngine(opts ...Option) *Engine

NewEngine creates a new physics engine.

func (*Engine) CreateBodyDefinition added in v0.9.0

func (e *Engine) CreateBodyDefinition(info BodyDefinitionInfo) *BodyDefinition

CreateBodyDefinition creates a new BodyDefinition that can be used to create Body instances.

func (*Engine) CreateMaterial added in v0.10.0

func (e *Engine) CreateMaterial(info MaterialInfo) *Material

CreateMaterial creates a new Material that can be used to describe an object's behavior.

func (*Engine) CreateScene

func (e *Engine) CreateScene() *Scene

CreateScene creates a new Scene and configures the simulation for it to run at maximum stepSeconds intervals.

type GlobalAccelerator added in v0.18.0

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

GlobalAccelerator represents a force that is applied to all bodies in the scene.

func (GlobalAccelerator) Delete added in v0.18.0

func (a GlobalAccelerator) Delete()

Delete removes this global accelerator.

func (GlobalAccelerator) Enabled added in v0.18.0

func (a GlobalAccelerator) Enabled() bool

Enabled returns whether this global accelerator will be applied.

func (GlobalAccelerator) Logic added in v0.18.0

Logic returns the acceleration solver that will be used to apply this global accelerator.

func (GlobalAccelerator) SetEnabled added in v0.18.0

func (a GlobalAccelerator) SetEnabled(enabled bool)

SetEnabled changes whether this global accelerator will be applied.

type Material added in v0.10.0

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

Material represents the surface properties of an object.

func (*Material) FrictionCoefficient added in v0.10.0

func (m *Material) FrictionCoefficient() float64

FrictionCoefficient returns the friction coefficient of this material.

func (*Material) RestitutionCoefficient added in v0.10.0

func (m *Material) RestitutionCoefficient() float64

RestitutionCoefficient returns the coefficient of restitution of this material.

func (*Material) SetFrictionCoefficient added in v0.10.0

func (m *Material) SetFrictionCoefficient(coefficient float64)

SetFrictionCoefficient changes the friction coefficient of this material.

func (*Material) SetRestitutionCoefficient added in v0.10.0

func (m *Material) SetRestitutionCoefficient(coefficient float64)

SetRestitutionCoefficient changes the coefficient of restitution of this material.

type MaterialInfo added in v0.10.0

type MaterialInfo struct {
	FrictionCoefficient    float64
	RestitutionCoefficient float64
}

MaterialInfo contains the data necessary to create a Material.

type Option added in v0.20.0

type Option func(c *config)

Option is a configuration function that can be used to customize the behavior of a physics engine.

func WithTimestep added in v0.20.0

func WithTimestep(timestep time.Duration) Option

WithTimestep configures the physics engine to use the provided timestep.

type Prop added in v0.10.0

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

func (Prop) Name added in v0.18.0

func (p Prop) Name() string

type PropInfo added in v0.10.0

type PropInfo struct {
	Name         string
	CollisionSet collision.Set
}

type SBConstraint

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

SBConstraint represents a restriction enforced on one body.

func (SBConstraint) Body

func (c SBConstraint) Body() Body

Body returns the body on which this constraint acts.

func (SBConstraint) Delete

func (c SBConstraint) Delete()

Delete removes this constraint.

func (SBConstraint) Enabled

func (c SBConstraint) Enabled() bool

Enabled returns whether this constraint will be enforced. By default a constraint is enabled.

func (SBConstraint) Logic added in v0.18.0

func (c SBConstraint) Logic() solver.Constraint

Logic returns the constraint solver that will be used to enforce mathematically this constraint.

func (SBConstraint) SetEnabled

func (c SBConstraint) SetEnabled(enabled bool)

SetEnabled changes whether this constraint will be enforced.

type Scene

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

Scene represents a physics scene that contains a number of bodies that are independent on any bodies managed by other scene objects.

func (*Scene) CreateBody

func (s *Scene) CreateBody(info BodyInfo) Body

CreateBody creates a new physics body and places it within this scene.

func (*Scene) CreateConstraintSet

func (s *Scene) CreateConstraintSet() *ConstraintSet

CreateConstraintSet creates a new ConstraintSet.

func (*Scene) CreateDoubleBodyConstraint

func (s *Scene) CreateDoubleBodyConstraint(primary, secondary Body, logic solver.PairConstraint) DBConstraint

CreateDoubleBodyConstraint creates a new physics constraint that acts on two bodies and enables it for this scene.

func (*Scene) CreateGlobalAccelerator added in v0.18.0

func (s *Scene) CreateGlobalAccelerator(logic solver.Acceleration) GlobalAccelerator

CreateGlobalAccelerator creates a new accelerator that affects the whole scene.

func (*Scene) CreateProp added in v0.10.0

func (s *Scene) CreateProp(info PropInfo)

CreateProp creates a new static Prop. A prop is an object that is static and rarely removed.

func (*Scene) CreateSingleBodyConstraint

func (s *Scene) CreateSingleBodyConstraint(body Body, logic solver.Constraint) SBConstraint

CreateSingleBodyConstraint creates a new physics constraint that acts on a single body and enables it for this scene.

func (*Scene) Delete

func (s *Scene) Delete()

Delete releases resources allocated by this scene. Users should not call any further methods on this object.

func (*Scene) Each added in v0.10.0

func (s *Scene) Each(cb func(b Body))

func (*Scene) Engine added in v0.9.0

func (s *Scene) Engine() *Engine

Engine returns the physics Engine that owns this Scene.

func (*Scene) MaxAngularAcceleration added in v0.18.0

func (s *Scene) MaxAngularAcceleration() float64

MaxAngularAcceleration returns the maximum angular acceleration that a body can have.

func (*Scene) MaxLinearAcceleration added in v0.18.0

func (s *Scene) MaxLinearAcceleration() float64

MaxLinearAcceleration returns the maximum linear acceleration that a body can have.

func (*Scene) MediumSolver added in v0.18.0

func (s *Scene) MediumSolver() solver.Medium

MediumSolver returns the solver that is used to calculate the medium properties of the scene.

func (*Scene) Nearby added in v0.10.0

func (s *Scene) Nearby(body Body, distance float64, cb func(b Body))

func (*Scene) SetMaxAngularAcceleration added in v0.18.0

func (s *Scene) SetMaxAngularAcceleration(acceleration float64)

SetMaxAngularAcceleration changes the maximum angular acceleration that a body can have.

func (*Scene) SetMaxLinearAcceleration added in v0.18.0

func (s *Scene) SetMaxLinearAcceleration(acceleration float64)

SetMaxLinearAcceleration changes the maximum linear acceleration that a body can have.

func (*Scene) SetMediumSolver added in v0.18.0

func (s *Scene) SetMediumSolver(solver solver.Medium)

SetMediumSolver changes the solver that is used to calculate the medium properties of the scene.

func (*Scene) SetTimeSpeed added in v0.10.0

func (s *Scene) SetTimeSpeed(timeSpeed float64)

SetTimeSpeed changes the rate at which time runs.

func (*Scene) SubscribeDoubleBodyCollision added in v0.18.0

func (s *Scene) SubscribeDoubleBodyCollision(callback DoubleBodyCollisionCallback) *DoubleBodyCollisionSubscription

SubscribeDoubleBodyCollision registers a callback that is invoked when two bodies collide.

func (*Scene) SubscribePostUpdate added in v0.18.0

func (s *Scene) SubscribePostUpdate(callback UpdateCallback) *UpdateSubscription

SubscribePostUpdate registers a callback that is invoked after each physics iteration.

func (*Scene) SubscribePreUpdate added in v0.18.0

func (s *Scene) SubscribePreUpdate(callback UpdateCallback) *UpdateSubscription

SubscribePreUpdate registers a callback that is invoked before each physics iteration.

func (*Scene) SubscribeSingleBodyCollision added in v0.18.0

func (s *Scene) SubscribeSingleBodyCollision(callback SingleBodyCollisionCallback) *SingleBodyCollisionSubscription

SubscribeSingleBodyCollision registers a callback that is invoked when a body collides with a static object.

func (*Scene) TimeSpeed added in v0.10.0

func (s *Scene) TimeSpeed() float64

TimeSpeed returns the speed at which time runs, where 1.0 is the default and 0.0 is stopped.

func (*Scene) Update

func (s *Scene) Update(elapsedTime time.Duration)

Update runs a number of physics iterations until the specified duration has been reached or surpassed (yes physics can get ahead).

type SingleBodyCollisionCallback added in v0.18.0

type SingleBodyCollisionCallback func(body Body, prop Prop, active bool)

SingleBodyCollisionCallback is a mechanism to receive notifications about collisions between a body and a prop in the scene.

type SingleBodyCollisionSubscription added in v0.18.0

type SingleBodyCollisionSubscription = observer.Subscription[SingleBodyCollisionCallback]

SingleBodyCollisionSubscription represents a notification subscription for single body collisions.

type SingleBodyCollisionSubscriptionSet added in v0.18.0

type SingleBodyCollisionSubscriptionSet = observer.SubscriptionSet[SingleBodyCollisionCallback]

SingleBodyCollisionSubscriptionSet represents a set of single body collision subscriptions.

func NewSingleBodyCollisionSubscriptionSet added in v0.18.0

func NewSingleBodyCollisionSubscriptionSet() *SingleBodyCollisionSubscriptionSet

NewSingleBodyCollisionSubscriptionSet creates a new SingleBodyCollisionSubscriptionSet.

type SurfaceAerodynamicShape added in v0.10.0

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

func NewSurfaceAerodynamicShape added in v0.10.0

func NewSurfaceAerodynamicShape(width, height, length float64) *SurfaceAerodynamicShape

func (*SurfaceAerodynamicShape) BoundingSphereRadius added in v0.10.0

func (s *SurfaceAerodynamicShape) BoundingSphereRadius() float64

func (*SurfaceAerodynamicShape) Force added in v0.10.0

func (s *SurfaceAerodynamicShape) Force(windSpeed dprec.Vec3, density float64) dprec.Vec3

func (*SurfaceAerodynamicShape) SetDragCoefficient added in v0.18.0

func (s *SurfaceAerodynamicShape) SetDragCoefficient(coefficient float64) *SurfaceAerodynamicShape

func (*SurfaceAerodynamicShape) SetLiftCoefficient added in v0.18.0

func (s *SurfaceAerodynamicShape) SetLiftCoefficient(coefficient float64) *SurfaceAerodynamicShape

type Transform added in v0.10.0

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

Transform represents a shape transformation - translation and rotation.

func IdentityTransform added in v0.10.0

func IdentityTransform() Transform

IdentityTransform returns a new Transform that represents the origin.

func NewTransform added in v0.10.0

func NewTransform(position dprec.Vec3, rotation dprec.Quat) Transform

NewTransform creates a new Transform with the specified position and rotation.

func (Transform) Position added in v0.10.0

func (t Transform) Position() dprec.Vec3

Position returns the translation of this Transform.

func (Transform) Rotation added in v0.10.0

func (t Transform) Rotation() dprec.Quat

Rotation returns the orientation of this Transform.

func (Transform) Transformed added in v0.10.0

func (t Transform) Transformed(transform Transform) Transform

Transformed returns a new Transform that is based on this one but has the specified Transform applied to it.

type UpdateCallback added in v0.18.0

type UpdateCallback func(elapsedTime time.Duration)

UpdateCallback is a mechanism to receive update notifications.

type UpdateSubscription added in v0.18.0

type UpdateSubscription = observer.Subscription[UpdateCallback]

UpdateSubscription represents a notification subscription for updates.

type UpdateSubscriptionSet added in v0.18.0

type UpdateSubscriptionSet = observer.SubscriptionSet[UpdateCallback]

UpdateSubscriptionSet represents a set of update subscriptions.

func NewUpdateSubscriptionSet added in v0.18.0

func NewUpdateSubscriptionSet() *UpdateSubscriptionSet

NewUpdateSubscriptionSet creates a new UpdateSubscriptionSet.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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