-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |