Documentation ¶
Overview ¶
molecule is a 3D physics engine that designed for realistic star system
The default units used by this package:
Distance and Position: m (meter) Time: s (second) Speed: m / s (meter per second) Acceleration: m / s^2 (meter per second squared) Mass: kg (kilogram) Force: N or kg*m / s^2 (Newton) Temperature: K (Kelvin) Heat: J or kg*m^2 / s^2 (Joules)
Index ¶
- Constants
- type Bitset
- type Block
- type Config
- type Cube
- type Engine
- func (e *Engine) AccFromForce(mass float64, speed float64, force Vec3) Vec3
- func (e *Engine) Config() Config
- func (e *Engine) Events() int
- func (e *Engine) ForeachBlock(cb func(b Block))
- func (e *Engine) ForeachObject(cb func(o *Object))
- func (e *Engine) GetObject(id uuid.UUID) *Object
- func (e *Engine) LorentzFactor(speed float64) float64
- func (e *Engine) MainAnchor() *Object
- func (e *Engine) Momentum(mass float64, velocity Vec3) Vec3
- func (e *Engine) NewObject(typ ObjType, anchor *Object, pos Vec3, processors ...func(*Object)) (o *Object)
- func (e *Engine) ProperTime(t float64, speed float64) float64
- func (e *Engine) ReLorentzFactor(speed float64) float64
- func (e *Engine) ReLorentzFactorSq(speedSq float64) float64
- func (e *Engine) Tick(dt time.Duration)
- type Facing
- type GravityField
- func (f *GravityField) Clone() (g *GravityField)
- func (f *GravityField) FieldAt(pos Vec3) Vec3
- func (f *GravityField) Mass() float64
- func (f *GravityField) Pos() Vec3
- func (f *GravityField) Radius() float64
- func (f *GravityField) SetMass(mass float64)
- func (f *GravityField) SetPos(pos Vec3)
- func (f *GravityField) SetRadius(radius float64)
- type MagnetField
- type Material
- type MaterialPair
- type MaterialProps
- type MaterialSet
- type ObjType
- type Object
- func (o *Object) AbsPos() (p Vec3)
- func (o *Object) AbsPosAndAnchor() (p Vec3, m *Object)
- func (o *Object) AbsPosAndAnchorLocked() (p Vec3, m *Object)
- func (o *Object) AbsPosLocked() (p Vec3)
- func (o *Object) AbsVelocity() (v Vec3)
- func (o *Object) AddBlock(blocks ...Block)
- func (o *Object) Anchor() *Object
- func (o *Object) AnchorLocked() *Object
- func (o *Object) Angle() Vec3
- func (o *Object) AttachTo(anchor *Object)
- func (o *Object) AttachToLocked(anchor *Object)
- func (o *Object) Blocks() []Block
- func (o *Object) Engine() *Engine
- func (o *Object) FillGfields()
- func (o *Object) GoString() string
- func (o *Object) GravityCenter() (center Vec3)
- func (o *Object) GravityCenterAndMass() (center Vec3, mass float64)
- func (o *Object) GravityField() *GravityField
- func (o *Object) GravityFieldAt(pos Vec3) Vec3
- func (o *Object) HeadingVel() Vec3
- func (o *Object) Id() uuid.UUID
- func (o *Object) MainAnchor() (m *Object)
- func (o *Object) MainAnchorLocked() (m *Object)
- func (o *Object) Mass() (mass float64)
- func (o *Object) Pos() Vec3
- func (o *Object) PosLocked() Vec3
- func (o *Object) ProperTime(dt time.Duration) float64
- func (o *Object) RelPos(a *Object) Vec3
- func (o *Object) RemoveBlock(target Block)
- func (o *Object) RotatePos(p *Vec3) *Vec3
- func (o *Object) SetAngle(angle Vec3)
- func (o *Object) SetBlocks(blocks []Block)
- func (o *Object) SetHeadingVel(v Vec3)
- func (o *Object) SetPos(pos Vec3)
- func (o *Object) SetRadius(radius float64)
- func (o *Object) SetType(t ObjType)
- func (o *Object) SetVelocity(velocity Vec3)
- func (o *Object) String() string
- func (o *Object) TickForce() *Vec3
- func (o *Object) Type() ObjType
- func (o *Object) Velocity() Vec3
- func (o *Object) VelocityLocked() Vec3
- type System
- type Vec3
- func (v Vec3) Abs() Vec3
- func (v *Vec3) Add(u Vec3) *Vec3
- func (v Vec3) Added(u Vec3) Vec3
- func (v Vec3) AngleX() float64
- func (v Vec3) AngleY() float64
- func (v Vec3) AngleZ() float64
- func (v *Vec3) Clone() *Vec3
- func (v Vec3) Dot(u Vec3) float64
- func (v Vec3) Equals(u Vec3) bool
- func (v Vec3) IsZero() bool
- func (v Vec3) Len() float64
- func (v *Vec3) Map(m func(float64) float64) *Vec3
- func (v Vec3) Mapped(m func(float64) float64) Vec3
- func (v *Vec3) Mod(u Vec3) *Vec3
- func (v *Vec3) ModN(n float64) *Vec3
- func (v Vec3) Moded(u Vec3) Vec3
- func (v Vec3) ModedN(n float64) Vec3
- func (v *Vec3) Negate() *Vec3
- func (v Vec3) Negated() Vec3
- func (v *Vec3) Normalize() *Vec3
- func (v Vec3) Normalized() Vec3
- func (v *Vec3) RotateX(angle float64) *Vec3
- func (v *Vec3) RotateXYZ(angles Vec3) *Vec3
- func (v *Vec3) RotateY(angle float64) *Vec3
- func (v *Vec3) RotateZ(angle float64) *Vec3
- func (v Vec3) RotatedX(angle float64) Vec3
- func (v Vec3) RotatedXYZ(angles Vec3) Vec3
- func (v Vec3) RotatedY(angle float64) Vec3
- func (v Vec3) RotatedZ(angle float64) Vec3
- func (v *Vec3) Scale(u Vec3) *Vec3
- func (v *Vec3) ScaleN(n float64) *Vec3
- func (v Vec3) Scaled(u Vec3) Vec3
- func (v Vec3) ScaledN(n float64) Vec3
- func (v Vec3) SqLen() float64
- func (v Vec3) String() string
- func (v *Vec3) Sub(u Vec3) *Vec3
- func (v Vec3) Subbed(u Vec3) Vec3
- func (v Vec3) XYZ() (x, y, z float64)
- type Vec4
- func (v Vec4) Abs() Vec4
- func (v *Vec4) Add(u Vec4) *Vec4
- func (v Vec4) Added(u Vec4) Vec4
- func (v Vec4) Equals(u Vec4) bool
- func (v Vec4) IsZero() bool
- func (v Vec4) Len() float64
- func (v *Vec4) Map(m func(float64) float64) *Vec4
- func (v Vec4) Mapped(m func(float64) float64) Vec4
- func (v *Vec4) Negate() *Vec4
- func (v Vec4) Negated() Vec4
- func (v *Vec4) Scale(u Vec4) *Vec4
- func (v *Vec4) ScaleN(n float64) *Vec4
- func (v Vec4) Scaled(u Vec4) Vec4
- func (v Vec4) ScaledN(n float64) Vec4
- func (v Vec4) SqLen() float64
- func (v Vec4) String() string
- func (v *Vec4) Sub(u Vec4) *Vec4
- func (v Vec4) Subbed(u Vec4) Vec4
- func (v Vec4) To3() Vec3
- func (v Vec4) XYZ() (x, y, z float64)
Constants ¶
const (
C = 299792458.0 // The speed of light
)
const (
G = 6.674e-11 // The gravitational constant is 6.674×10−11 N⋅m2/kg2
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bitset ¶
type Bitset struct {
// contains filtered or unexported fields
}
type Block ¶
type Block interface { // SetObject will be called when block is inited or it's moving between objects SetObject(o *Object) Mass() float64 // Material returns the material of the face, nil is allowed Material(f Facing) *Material // Outline specific the position and the maximum space of the block Outline() *Cube // Tick will be called when the block need to update it's state Tick(dt float64) }
type Cube ¶
func NewCubeFromCenter ¶
func (*Cube) OverlapBox ¶
OverlapBox will calcuate the overlapped area. If overlapped, the method will save the overlapped area into the second argument `area`, relative to the Cube `b`, and returns true. Note: `area` maybe changed even the box is not overlapped
type Engine ¶
Engine includes a sync.RWMutex which should be locked when operating global things inside a tick
func (*Engine) AccFromForce ¶
AccFromForce calculate the acceleration from force TODO: Not sure if this is correct in SR
func (*Engine) ForeachBlock ¶
func (*Engine) ForeachObject ¶
func (*Engine) LorentzFactor ¶
func (*Engine) MainAnchor ¶
func (*Engine) NewObject ¶
func (e *Engine) NewObject(typ ObjType, anchor *Object, pos Vec3, processors ...func(*Object)) (o *Object)
NewObject will create an object use random v7 UUID
func (*Engine) ProperTime ¶
ProperTime returns the delta time that relative to the moving object, with given speed and the delta time relative to the observer (or the server)
func (*Engine) ReLorentzFactor ¶
ReLorentzFactor is the reciprocal of the Lorentz Factor It's used for faster calculate in some specific cases See <https://en.wikipedia.org/wiki/Lorentz_factor>
func (*Engine) ReLorentzFactorSq ¶
ReLorentzFactorSq is same as ReLorentzFactor, but require squared speed as input
type GravityField ¶
type GravityField struct {
// contains filtered or unexported fields
}
func NewGravityField ¶
func NewGravityField(pos Vec3, mass float64, radius float64) (f *GravityField)
func (*GravityField) Clone ¶
func (f *GravityField) Clone() (g *GravityField)
func (*GravityField) FieldAt ¶
func (f *GravityField) FieldAt(pos Vec3) Vec3
FieldAt returns the acceleration at the position due to the gravity field
func (*GravityField) Mass ¶
func (f *GravityField) Mass() float64
func (*GravityField) Pos ¶
func (f *GravityField) Pos() Vec3
func (*GravityField) Radius ¶
func (f *GravityField) Radius() float64
func (*GravityField) SetMass ¶
func (f *GravityField) SetMass(mass float64)
func (*GravityField) SetPos ¶
func (f *GravityField) SetPos(pos Vec3)
func (*GravityField) SetRadius ¶
func (f *GravityField) SetRadius(radius float64)
type MagnetField ¶
type MagnetField struct {
// contains filtered or unexported fields
}
MagnetField represents a simulated magnetic field. For easier calculate, it's not the real magnetic field. Since the magnetic field disappears easily, the cubic distance is used
func NewMagnetField ¶
func NewMagnetField(power float64) *MagnetField
func (*MagnetField) FieldAt ¶
func (f *MagnetField) FieldAt(distance Vec3) Vec3
func (*MagnetField) Power ¶
func (f *MagnetField) Power() float64
func (*MagnetField) SetPower ¶
func (f *MagnetField) SetPower(power float64)
type Material ¶
type Material struct {
// contains filtered or unexported fields
}
Material represents a unique material. You should only handle the pointer of the Material. Copying or cloning Material is an illegal operation.
func NewMaterial ¶
func NewMaterial(id string, props MaterialProps) *Material
func (*Material) Props ¶
func (m *Material) Props() MaterialProps
type MaterialPair ¶
type MaterialPair struct {
// The id of the materials
MatterA, MatterB *Material
// Frictions see: <https://en.wikipedia.org/wiki/Friction>
SCOF, KCOF float64 // the coefficients of static/kinetic friction
}
MaterialPair represents the status that between two materials
func (*MaterialPair) CalcNetForce ¶
func (p *MaterialPair) CalcNetForce(natural, app float64, moving bool) float64
CalcNetForce returns the net force of a object after canceled out the friction The first argument `natural` is the natural force acting on the material The second argument `app` is the application force acting on the object Note: All input forces **must be** zero or positive, but the net force may be negative
type MaterialProps ¶
type MaterialProps struct { Brittleness float64 // <https://en.wikipedia.org/wiki/Brittleness> COR float64 // Coefficient of restitution <https://en.wikipedia.org/wiki/Coefficient_of_restitution> Density float64 // kg / m^3 Durability int64 // -1 means never break HeatCap float64 // J / (kg * K) <https://en.wikipedia.org/wiki/Specific_heat_capacity> FirePoint float64 // The temperature that can cause fire, zero means none }
MaterialProps saves the Material properties
type MaterialSet ¶
type MaterialSet struct {
// contains filtered or unexported fields
}
MaterialSet manages a set of Material and MaterialPair
func NewMaterialSet ¶
func NewMaterialSet() (s *MaterialSet)
func (*MaterialSet) Add ¶
func (s *MaterialSet) Add(m *Material)
Add push a Material into the MaterialSet If the Material's id is already exists, Add will panic
func (*MaterialSet) AddPair ¶
func (s *MaterialSet) AddPair(pair *MaterialPair)
AddPair push a MaterialPair into the MaterialSet If the MaterialPair already exists, AddPair will panic
func (*MaterialSet) Get ¶
func (s *MaterialSet) Get(id string) *Material
func (*MaterialSet) GetPair ¶
func (s *MaterialSet) GetPair(a, b *Material) *MaterialPair
type Object ¶
Object represents an object in the physics engine.
func (*Object) AbsPosAndAnchor ¶
AbsPosAndAnchor combine the results of AbsPos and MainAnchor
func (*Object) AbsPosAndAnchorLocked ¶
AbsPosAndAnchorLocked is same as AbsPosAndAnchor, but used under locked condition
func (*Object) AbsPosLocked ¶
AbsPosLocked is same as AbsPosAndAnchor, but used under locked condition
func (*Object) AbsVelocity ¶
func (*Object) Anchor ¶
Anchor returns this object's anchor object If Anchor returns nil, the object is the main anchor
func (*Object) AnchorLocked ¶
func (*Object) AttachTo ¶
AttachTo will change the object's anchor to another. The new position will be calculated at the same time.
func (*Object) AttachToLocked ¶
AttachToLocked is same as AttachTo, but used under locked condition e.g. inside the object's tick
func (*Object) FillGfields ¶
func (o *Object) FillGfields()
func (*Object) GravityCenter ¶
func (*Object) GravityCenterAndMass ¶
func (*Object) GravityField ¶
func (o *Object) GravityField() *GravityField
func (*Object) GravityFieldAt ¶
GravityFieldAt will returns the correct history gravity field by position. argument pos is the position relative to the zero position of this object
func (*Object) HeadingVel ¶
HeadingVel returns the angle velocity vector
func (*Object) MainAnchor ¶
MainAnchor returns the main anchor object
func (*Object) MainAnchorLocked ¶
MainAnchorLocked is same as MainAnchor, but used under locked condition
func (*Object) RelPos ¶
RelPos returns the relative position of the passed object about this object To be clear, return the displacement from o to a (a.pos - o.pos)
func (*Object) RemoveBlock ¶
func (*Object) SetHeadingVel ¶
SetHeadingVel sets the angle velocity vector
func (*Object) SetVelocity ¶
func (*Object) TickForce ¶
TickForce returns the force vector that can be edit during a tick. You should never read/write the vector concurrently or outside a tick.
func (*Object) VelocityLocked ¶
type System ¶
type System struct {
// contains filtered or unexported fields
}
System handle a bunch of main anchors (usually stars) that are affecting each other
type Vec3 ¶
type Vec3 struct {
X, Y, Z float64
}
func (Vec3) AngleX ¶
AngleX returns the angle between the vector and y-axis, about z-axis
Z ^ |/ --+--> | Y
func (Vec3) AngleY ¶
AngleY returns the angle between the vector and z-axis, about x-axis
X ^ |/ --+--> | Z
func (Vec3) AngleZ ¶
AngleZ returns the angle between the vector and x-axis, about y-axis
Y ^ |/ --+--> | X
func (*Vec3) Normalize ¶
Normalize make the length of the vector to 1 and keep the current direction.
func (Vec3) Normalized ¶
Normalized returns a vector of length 1 facing the direction of u with the same angle.