From 5136a24da2eccd0013ee2754eda821f0838c380d Mon Sep 17 00:00:00 2001 From: zyxkad Date: Sat, 25 Nov 2023 20:12:05 -0700 Subject: [PATCH] add MagnetField --- engine.go | 17 ++++++++++++++- force.go | 65 +++++++++++++++++++++++++++++++++++++++---------------- motion.go | 15 +++++++++++++ 3 files changed, 77 insertions(+), 20 deletions(-) create mode 100644 motion.go diff --git a/engine.go b/engine.go index 2bcda09..1870709 100644 --- a/engine.go +++ b/engine.go @@ -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 } diff --git a/force.go b/force.go index 9b49589..1c3f31a 100644 --- a/force.go +++ b/force.go @@ -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 } diff --git a/motion.go b/motion.go new file mode 100644 index 0000000..a6905e3 --- /dev/null +++ b/motion.go @@ -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) +}