Skip to content

Commit

Permalink
update Object
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Nov 28, 2023
1 parent 283a976 commit b3470b6
Show file tree
Hide file tree
Showing 7 changed files with 416 additions and 136 deletions.
5 changes: 4 additions & 1 deletion block.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const (

type Block interface {
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(dt float64)
// Tick will be called when the block need to update it's state
Tick(dt float64, o *Object)
}
12 changes: 6 additions & 6 deletions box.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package molecular

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

func NewCube(pos, size Vec) (b *Cube) {
func NewCube(pos, size Vec3) (b *Cube) {
if size.X < 0 {
pos.X += size.X
size.X = -size.X
Expand All @@ -32,15 +32,15 @@ func (b *Cube) Equals(x *Cube) bool {
return b.P == x.P && b.S == x.S
}

func (b *Cube) Pos() Vec {
func (b *Cube) Pos() Vec3 {
return b.P
}

func (b *Cube) Size() Vec {
func (b *Cube) Size() Vec3 {
return b.S
}

func (b *Cube) EndPos() Vec {
func (b *Cube) EndPos() Vec3 {
return b.P.Added(b.S)
}

Expand Down
43 changes: 35 additions & 8 deletions engine.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package molecular

import (
"sync"

"github.com/google/uuid"
)

Expand All @@ -11,20 +13,28 @@ type Config struct {
MaxSpeed float64
}

// Engine includes a sync.RWMutex which should be locked when operating global things inside a tick
type Engine struct {
cfg Config
mainAnchor *Object // the main anchor object, must be invincible and unmoveable
sync.RWMutex

// the config should not change while engine running
cfg Config
// the main anchor object must be invincible and unmovable
mainAnchor *Object
objects map[uuid.UUID]*Object
}

func NewEngine(cfg Config) *Engine {
return &Engine{
func NewEngine(cfg Config) (e *Engine) {
e = &Engine{
cfg: cfg,
mainAnchor: &Object{
id: uuid.Nil,
attachs: make(set[*Object], 10),
},
objects: make(map[uuid.UUID]*Object, 10),
}
e.objects[uuid.Nil] = e.mainAnchor
return
}

func (e *Engine) Config() Config {
Expand All @@ -36,18 +46,35 @@ func (e *Engine) MainAnchor() *Object {
}

// NewObject will create an object use v7 UUID
func (e *Engine) NewObject(anchor *Object, pos Vec) *Object {
func (e *Engine) NewObject(anchor *Object, pos Vec3) *Object {
e.Lock()
defer e.Unlock()

return e.newObjectLocked(anchor, pos)
}

func (e *Engine) newObjectLocked(anchor *Object, pos Vec3) *Object {
for i := 20; i > 0; i-- {
if id, err := uuid.NewV7(); err == nil {
if _, ok := e.objects[id]; !ok {
return newObject(id, anchor, pos)
return e.newAndPutObject(id, anchor, pos)
}
}
}
panic("molecular.Engine: Too many UUID generation failures")
}

// Tick will call Tick on the main anchor
func (e *Engine) GetObject(id uuid.UUID) *Object {
e.RLock()
defer e.RUnlock()

return e.objects[id]
}

// Tick will call tick on the main anchor
func (e *Engine) Tick(dt float64) {
e.mainAnchor.Tick(dt, e)
var wg sync.WaitGroup
wg.Add(1)
e.mainAnchor.tick(&wg, dt, e)
wg.Wait()
}
6 changes: 3 additions & 3 deletions force.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type GravityField struct {
mass float64
}

func NewGravityField(pos Vec, mass float64) *GravityField {
func NewGravityField(pos Vec3, mass float64) *GravityField {
return &GravityField{
mass: mass,
}
Expand All @@ -23,7 +23,7 @@ func (f *GravityField) SetMass(mass float64) {
}

// FieldAt returns the acceleration at the distance due to the gravity field
func (f *GravityField) FieldAt(distance Vec) Vec {
func (f *GravityField) FieldAt(distance Vec3) Vec3 {
l := distance.Len()
if l == 0 {
return ZeroVec
Expand Down Expand Up @@ -55,7 +55,7 @@ func (f *MagnetField) SetPower(power float64) {
f.power = power
}

func (f *MagnetField) FieldAt(distance Vec) Vec {
func (f *MagnetField) FieldAt(distance Vec3) Vec3 {
l := distance.Len()
if l == 0 {
return ZeroVec
Expand Down
33 changes: 30 additions & 3 deletions motion.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,36 @@ const (
C = 299792458.0 // The speed of light
)

// RelativeDeltaTime returns the actual delta time that relative to the moving object,
// See <https://en.wikipedia.org/wiki/Lorentz_factor>
func (e *Engine) LorentzFactor(speed float64) float64 {
return 1 / e.ReLorentzFactor(speed)
}

// 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 (e *Engine) ReLorentzFactor(speed float64) float64 {
if speed == 0 {
return 1
}
n := speed / C
return math.Sqrt(1 - n*n)
}

// Note: F = dP / dt
func (e *Engine) Momentum(mass float64, velocity Vec3) Vec3 {
return velocity.ScaledN(e.ReLorentzFactor(velocity.Len()) * mass)
}

// AccFromForce calculate the acceleration from force
// TODO: Not sure if this is correct in SR
func (e *Engine) AccFromForce(mass float64, speed float64, force Vec3) Vec3 {
return force.ScaledN(e.ReLorentzFactor(speed) / mass)
}

// 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 (e *Engine) RelativeDeltaTime(t float64, speed float64) float64 {
func (e *Engine) ProperTime(t float64, speed float64) float64 {
if t == 0 {
return 0
}
Expand All @@ -23,7 +50,7 @@ func (e *Engine) RelativeDeltaTime(t float64, speed float64) float64 {
if speed >= C {
return math.SmallestNonzeroFloat64
}
t2 := t * math.Sqrt(1-speed*speed/(C*C))
t2 := t * e.ReLorentzFactor(speed)
if t2 == 0 {
return math.SmallestNonzeroFloat64
}
Expand Down
Loading

0 comments on commit b3470b6

Please sign in to comment.