molecular

package module
v0.0.0-...-d3db5ba Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: AGPL-3.0 Imports: 8 Imported by: 0

README

molecular

molecule is a 3D physics engine that designed for realistic gravity

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

View Source
const (
	C = 299792458.0 // The speed of light

)
View Source
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
}

func NewBitset

func NewBitset(n int) *Bitset

NewBitset creates a Bitset with at least `n` bits

func (*Bitset) Bytes

func (b *Bitset) Bytes() (buf []byte)

Bytes encode the Bitset to bytes use LittleEndian mode

func (*Bitset) Cap

func (b *Bitset) Cap() int

Cap returns the bit slots of the Bitset

func (*Bitset) Clear

func (b *Bitset) Clear(i int)

Clear set the bit to zero

func (*Bitset) Flip

func (b *Bitset) Flip(i int) bool

Flip toggle the bit, and returns the old value

func (*Bitset) Get

func (b *Bitset) Get(i int) bool

Get will return true if the target bit is one

func (*Bitset) Set

func (b *Bitset) Set(i int)

Set set the bit to one

func (*Bitset) String

func (b *Bitset) String() string

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 Config

type Config struct {
	// MinSpeed means the minimum positive speed
	MinSpeed float64
	// MaxSpeed means the maximum positive speed
	MaxSpeed float64
	// MinAccel means the minimum positive acceleration
	MinAccel float64
}

type Cube

type Cube struct {
	P Vec3 // Pos
	S Vec3 // Size
}

func NewCube

func NewCube(pos, size Vec3) (b *Cube)

func NewCubeFromCenter

func NewCubeFromCenter(size Vec3) (b *Cube)

func (*Cube) Center

func (b *Cube) Center() Vec3

func (*Cube) EndPos

func (b *Cube) EndPos() Vec3

func (*Cube) Equals

func (b *Cube) Equals(x *Cube) bool

func (*Cube) Overlap

func (b *Cube) Overlap(x *Cube) bool

Overlap will return if the two Cube overlapped or not

func (*Cube) OverlapBox

func (b *Cube) OverlapBox(x *Cube, area *Cube) bool

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

func (*Cube) Pos

func (b *Cube) Pos() Vec3

func (*Cube) Size

func (b *Cube) Size() Vec3

func (*Cube) String

func (b *Cube) String() string

type Engine

type Engine struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Engine includes a sync.RWMutex which should be locked when operating global things inside a tick

func NewEngine

func NewEngine(cfg Config) (e *Engine)

func (*Engine) AccFromForce

func (e *Engine) AccFromForce(mass float64, speed float64, force Vec3) Vec3

AccFromForce calculate the acceleration from force TODO: Not sure if this is correct in SR

func (*Engine) Config

func (e *Engine) Config() Config

func (*Engine) Events

func (e *Engine) Events() int

Events returns the length of event waves

func (*Engine) ForeachBlock

func (e *Engine) ForeachBlock(cb func(b Block))

func (*Engine) ForeachObject

func (e *Engine) ForeachObject(cb func(o *Object))

func (*Engine) GetObject

func (e *Engine) GetObject(id uuid.UUID) *Object

func (*Engine) LorentzFactor

func (e *Engine) LorentzFactor(speed float64) float64

See <https://en.wikipedia.org/wiki/Lorentz_factor>

func (*Engine) MainAnchor

func (e *Engine) MainAnchor() *Object

func (*Engine) Momentum

func (e *Engine) Momentum(mass float64, velocity Vec3) Vec3

Note: F = dP / dt

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

func (e *Engine) ProperTime(t float64, speed float64) float64

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

func (e *Engine) ReLorentzFactor(speed float64) float64

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

func (e *Engine) ReLorentzFactorSq(speedSq float64) float64

ReLorentzFactorSq is same as ReLorentzFactor, but require squared speed as input

func (*Engine) Tick

func (e *Engine) Tick(dt time.Duration)

Tick will call tick on the main anchor

type Facing

type Facing uint8
const (
	TOP Facing = iota
	BOTTOM
	LEFT
	RIGHT
	FRONT
	BACK
)

func (Facing) String

func (i Facing) String() string

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) Id

func (m *Material) Id() string

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 ObjType

type ObjType uint8
const (
	ManMadeObj ObjType = 0
	NaturalObj ObjType = 1
	LivingObj  ObjType = 2
)

func (ObjType) String

func (t ObjType) String() string

type Object

type Object struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Object represents an object in the physics engine.

func (*Object) AbsPos

func (o *Object) AbsPos() (p Vec3)

AbsPos returns the position relative to the main anchor

func (*Object) AbsPosAndAnchor

func (o *Object) AbsPosAndAnchor() (p Vec3, m *Object)

AbsPosAndAnchor combine the results of AbsPos and MainAnchor

func (*Object) AbsPosAndAnchorLocked

func (o *Object) AbsPosAndAnchorLocked() (p Vec3, m *Object)

AbsPosAndAnchorLocked is same as AbsPosAndAnchor, but used under locked condition

func (*Object) AbsPosLocked

func (o *Object) AbsPosLocked() (p Vec3)

AbsPosLocked is same as AbsPosAndAnchor, but used under locked condition

func (*Object) AbsVelocity

func (o *Object) AbsVelocity() (v Vec3)

func (*Object) AddBlock

func (o *Object) AddBlock(blocks ...Block)

func (*Object) Anchor

func (o *Object) Anchor() *Object

Anchor returns this object's anchor object If Anchor returns nil, the object is the main anchor

func (*Object) AnchorLocked

func (o *Object) AnchorLocked() *Object

func (*Object) Angle

func (o *Object) Angle() Vec3

Angle returns the rotate angles

func (*Object) AttachTo

func (o *Object) AttachTo(anchor *Object)

AttachTo will change the object's anchor to another. The new position will be calculated at the same time.

func (*Object) AttachToLocked

func (o *Object) AttachToLocked(anchor *Object)

AttachToLocked is same as AttachTo, but used under locked condition e.g. inside the object's tick

func (*Object) Blocks

func (o *Object) Blocks() []Block

func (*Object) Engine

func (o *Object) Engine() *Engine

Engine returns the engine of the object

func (*Object) FillGfields

func (o *Object) FillGfields()

func (*Object) GoString

func (o *Object) GoString() string

func (*Object) GravityCenter

func (o *Object) GravityCenter() (center Vec3)

func (*Object) GravityCenterAndMass

func (o *Object) GravityCenterAndMass() (center Vec3, mass float64)

func (*Object) GravityField

func (o *Object) GravityField() *GravityField

func (*Object) GravityFieldAt

func (o *Object) GravityFieldAt(pos Vec3) Vec3

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

func (o *Object) HeadingVel() Vec3

HeadingVel returns the angle velocity vector

func (*Object) Id

func (o *Object) Id() uuid.UUID

An object's id will never be changed

func (*Object) MainAnchor

func (o *Object) MainAnchor() (m *Object)

MainAnchor returns the main anchor object

func (*Object) MainAnchorLocked

func (o *Object) MainAnchorLocked() (m *Object)

MainAnchorLocked is same as MainAnchor, but used under locked condition

func (*Object) Mass

func (o *Object) Mass() (mass float64)

func (*Object) Pos

func (o *Object) Pos() Vec3

Pos returns the position relative to the anchor

func (*Object) PosLocked

func (o *Object) PosLocked() Vec3

PosLocked returns the position relative to the anchor

func (*Object) ProperTime

func (o *Object) ProperTime(dt time.Duration) float64

func (*Object) RelPos

func (o *Object) RelPos(a *Object) Vec3

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 (o *Object) RemoveBlock(target Block)

func (*Object) RotatePos

func (o *Object) RotatePos(p *Vec3) *Vec3

func (*Object) SetAngle

func (o *Object) SetAngle(angle Vec3)

SetAngle sets the rotate angles

func (*Object) SetBlocks

func (o *Object) SetBlocks(blocks []Block)

func (*Object) SetHeadingVel

func (o *Object) SetHeadingVel(v Vec3)

SetHeadingVel sets the angle velocity vector

func (*Object) SetPos

func (o *Object) SetPos(pos Vec3)

SetPos sets the position relative to the anchor

func (*Object) SetRadius

func (o *Object) SetRadius(radius float64)

func (*Object) SetType

func (o *Object) SetType(t ObjType)

func (*Object) SetVelocity

func (o *Object) SetVelocity(velocity Vec3)

func (*Object) String

func (o *Object) String() string

func (*Object) TickForce

func (o *Object) TickForce() *Vec3

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) Type

func (o *Object) Type() ObjType

func (*Object) Velocity

func (o *Object) Velocity() Vec3

func (*Object) VelocityLocked

func (o *Object) VelocityLocked() Vec3

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
}
var (
	UnitX     Vec3 = Vec3{1, 0, 0}
	UnitY     Vec3 = Vec3{0, 1, 0}
	UnitZ     Vec3 = Vec3{0, 0, 1}
	ZeroVec   Vec3
	OneVec    Vec3 = Vec3{1, 1, 1}
	NegOneVec Vec3 = Vec3{-1, -1, -1}
)

func (Vec3) Abs

func (v Vec3) Abs() Vec3

func (*Vec3) Add

func (v *Vec3) Add(u Vec3) *Vec3

func (Vec3) Added

func (v Vec3) Added(u Vec3) Vec3

func (Vec3) AngleX

func (v Vec3) AngleX() float64

AngleX returns the angle between the vector and y-axis, about z-axis

Z ^
  |/
--+-->
  |  Y

func (Vec3) AngleY

func (v Vec3) AngleY() float64

AngleY returns the angle between the vector and z-axis, about x-axis

X ^
  |/
--+-->
  |  Z

func (Vec3) AngleZ

func (v Vec3) AngleZ() float64

AngleZ returns the angle between the vector and x-axis, about y-axis

Y ^
  |/
--+-->
  |  X

func (*Vec3) Clone

func (v *Vec3) Clone() *Vec3

func (Vec3) Dot

func (v Vec3) Dot(u Vec3) float64

func (Vec3) Equals

func (v Vec3) Equals(u Vec3) bool

func (Vec3) IsZero

func (v Vec3) IsZero() bool

func (Vec3) Len

func (v Vec3) Len() float64

func (*Vec3) Map

func (v *Vec3) Map(m func(float64) float64) *Vec3

func (Vec3) Mapped

func (v Vec3) Mapped(m func(float64) float64) Vec3

func (*Vec3) Mod

func (v *Vec3) Mod(u Vec3) *Vec3

func (*Vec3) ModN

func (v *Vec3) ModN(n float64) *Vec3

func (Vec3) Moded

func (v Vec3) Moded(u Vec3) Vec3

func (Vec3) ModedN

func (v Vec3) ModedN(n float64) Vec3

func (*Vec3) Negate

func (v *Vec3) Negate() *Vec3

Negate is a shortcut of ScaleN(-1)

func (Vec3) Negated

func (v Vec3) Negated() Vec3

Negated is a shortcut of ScaledN(-1)

func (*Vec3) Normalize

func (v *Vec3) Normalize() *Vec3

Normalize make the length of the vector to 1 and keep the current direction.

func (Vec3) Normalized

func (v Vec3) Normalized() Vec3

Normalized returns a vector of length 1 facing the direction of u with the same angle.

func (*Vec3) RotateX

func (v *Vec3) RotateX(angle float64) *Vec3

Rotate around x-axis

func (*Vec3) RotateXYZ

func (v *Vec3) RotateXYZ(angles Vec3) *Vec3

TODO: maybe we can do them once?

func (*Vec3) RotateY

func (v *Vec3) RotateY(angle float64) *Vec3

Rotate around y-axis

func (*Vec3) RotateZ

func (v *Vec3) RotateZ(angle float64) *Vec3

Rotate around z-axis

func (Vec3) RotatedX

func (v Vec3) RotatedX(angle float64) Vec3

Rotate around x-axis

func (Vec3) RotatedXYZ

func (v Vec3) RotatedXYZ(angles Vec3) Vec3

func (Vec3) RotatedY

func (v Vec3) RotatedY(angle float64) Vec3

Rotate around y-axis

func (Vec3) RotatedZ

func (v Vec3) RotatedZ(angle float64) Vec3

Rotate around z-axis

func (*Vec3) Scale

func (v *Vec3) Scale(u Vec3) *Vec3

func (*Vec3) ScaleN

func (v *Vec3) ScaleN(n float64) *Vec3

func (Vec3) Scaled

func (v Vec3) Scaled(u Vec3) Vec3

func (Vec3) ScaledN

func (v Vec3) ScaledN(n float64) Vec3

func (Vec3) SqLen

func (v Vec3) SqLen() float64

Squared length

func (Vec3) String

func (v Vec3) String() string

func (*Vec3) Sub

func (v *Vec3) Sub(u Vec3) *Vec3

func (Vec3) Subbed

func (v Vec3) Subbed(u Vec3) Vec3

func (Vec3) XYZ

func (v Vec3) XYZ() (x, y, z float64)

type Vec4

type Vec4 struct {
	T, X, Y, Z float64
}

func (Vec4) Abs

func (v Vec4) Abs() Vec4

func (*Vec4) Add

func (v *Vec4) Add(u Vec4) *Vec4

func (Vec4) Added

func (v Vec4) Added(u Vec4) Vec4

func (Vec4) Equals

func (v Vec4) Equals(u Vec4) bool

func (Vec4) IsZero

func (v Vec4) IsZero() bool

func (Vec4) Len

func (v Vec4) Len() float64

func (*Vec4) Map

func (v *Vec4) Map(m func(float64) float64) *Vec4

func (Vec4) Mapped

func (v Vec4) Mapped(m func(float64) float64) Vec4

func (*Vec4) Negate

func (v *Vec4) Negate() *Vec4

Negate is a shortcut of ScaleN(-1)

func (Vec4) Negated

func (v Vec4) Negated() Vec4

Negated is a shortcut of ScaledN(-1)

func (*Vec4) Scale

func (v *Vec4) Scale(u Vec4) *Vec4

func (*Vec4) ScaleN

func (v *Vec4) ScaleN(n float64) *Vec4

func (Vec4) Scaled

func (v Vec4) Scaled(u Vec4) Vec4

func (Vec4) ScaledN

func (v Vec4) ScaledN(n float64) Vec4

func (Vec4) SqLen

func (v Vec4) SqLen() float64

Squared length

func (Vec4) String

func (v Vec4) String() string

func (*Vec4) Sub

func (v *Vec4) Sub(u Vec4) *Vec4

func (Vec4) Subbed

func (v Vec4) Subbed(u Vec4) Vec4

func (Vec4) To3

func (v Vec4) To3() Vec3

func (Vec4) XYZ

func (v Vec4) XYZ() (x, y, z float64)

Jump to

Keyboard shortcuts

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