Skip to content

Commit

Permalink
add MagnetField
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Nov 26, 2023
1 parent 9025ae1 commit 5136a24
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 20 deletions.
17 changes: 16 additions & 1 deletion engine.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
package molecular

type Config struct {
MinSpeed float64
MaxSpeed float64
}

type Engine struct {
//
cfg Config
}

func NewEngine(cfg Config) *Engine {
return &Engine{
cfg: cfg,
}
}

func (e *Engine) Config() Config {
return e.cfg
}
65 changes: 46 additions & 19 deletions force.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,66 @@
package molecular

import (
"math"
)

const (
G = 6.674e-11 // The gravitational constant is 6.674×10−11 N⋅m2/kg2
C = 299792458.0 // The speed of light
G = 6.674e-11 // The gravitational constant is 6.674×10−11 N⋅m2/kg2
)

func RelativeDeltaTime(t float64, speed float64) float64 {
return t / math.Sqrt(1-speed*speed/C*C)
type GravityField struct {
mass float64
}

type ForceField struct {
pos Vec
mass float64
func NewGravityField(pos Vec, mass float64) *GravityField {
return &GravityField{
mass: mass,
}
}

func (f *ForceField) Mass() float64 {
func (f *GravityField) Mass() float64 {
return f.mass
}

func (f *ForceField) SetMass(mass float64) {
func (f *GravityField) SetMass(mass float64) {
f.mass = mass
}

// FieldAt returns the acceleration at the pos due to the force field
func (f *ForceField) FieldAt(pos Vec) Vec {
acc := f.pos.Subbed(pos)
l := acc.Len()
// FieldAt returns the acceleration at the distance due to the gravity field
func (f *GravityField) FieldAt(distance Vec) Vec {
l := distance.Len()
if l == 0 {
return ZeroVec
}
distance.Negate()
// normalize 1 / l and G * m / l ^ 2
acc.ScaleN(G * f.mass / (l * l * l))
return acc
distance.ScaleN(G * f.mass / (l * l * l))
return distance
}

// 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
type MagnetField struct {
power float64 // in m^3 / s^2
}

func NewMagnetField(power float64) *MagnetField {
return &MagnetField{
power: power,
}
}

func (f *MagnetField) Power() float64 {
return f.power
}

func (f *MagnetField) SetPower(power float64) {
f.power = power
}

func (f *MagnetField) FieldAt(distance Vec) Vec {
l := distance.Len()
if l == 0 {
return ZeroVec
}
// normalize will scale with factor 1 / l, so we merge two steps into one
distance.ScaleN(f.power / (l * l * l))
return distance
}
15 changes: 15 additions & 0 deletions motion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package molecular

import (
"math"
)

const (
C = 299792458.0 // The speed of light
)

// RelativeDeltaTime returns the actuall time that relative to the obeserver (server),
// with given speed and the time relative to the moving object
func RelativeDeltaTime(t float64, speed float64) float64 {
return t / math.Sqrt(1-speed*speed/C*C)
}

0 comments on commit 5136a24

Please sign in to comment.