-
Notifications
You must be signed in to change notification settings - Fork 0
/
force.go
138 lines (117 loc) · 3.29 KB
/
force.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// molecular is a 3D physics engine written in Go
// Copyright (C) 2023 Kevin Z <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package molecular
import (
"math"
)
const (
G = 6.674e-11 // The gravitational constant is 6.674×10−11 N⋅m2/kg2
)
var gravityFieldPool = newObjPool[GravityField]()
type GravityField struct {
pos Vec3
mass float64
radius float64
rSq float64 // radius * radius
rCube float64 // 1 / (radius * radius * radius)
}
func NewGravityField(pos Vec3, mass float64, radius float64) (f *GravityField) {
f = gravityFieldPool.Get()
f.pos = pos
f.mass = mass
f.radius = radius
f.rSq = radius * radius
f.rCube = 1 / (radius * radius * radius)
return
}
func (f *GravityField) Pos() Vec3 {
return f.pos
}
func (f *GravityField) SetPos(pos Vec3) {
f.pos = pos
}
func (f *GravityField) Mass() float64 {
return f.mass
}
func (f *GravityField) SetMass(mass float64) {
f.mass = mass
}
func (f *GravityField) Radius() float64 {
return f.radius
}
func (f *GravityField) SetRadius(radius float64) {
f.radius = radius
f.rSq = radius * radius
f.rCube = 1 / (radius * radius * radius)
}
func (f *GravityField) Clone() (g *GravityField) {
g = gravityFieldPool.Get()
*g = *f
return
}
// FieldAt returns the acceleration at the position due to the gravity field
func (f *GravityField) FieldAt(pos Vec3) Vec3 {
if f == nil {
return ZeroVec
}
acc := f.pos.Subbed(pos)
lSq := acc.SqLen()
if lSq == 0 {
return ZeroVec
}
if lSq < f.rSq {
acc.ScaleN(G * f.mass * f.rCube)
} else {
l := math.Sqrt(lSq)
// normalize 1 / l and G * m / l ^ 2
acc.ScaleN(G * f.mass / (lSq * l))
}
return acc
}
// GravityFieldRadius returns the max gravity field radius of given mass
func (e *Engine) GravityFieldRadius(mass float64) float64 {
return math.Sqrt(e.GravityFieldRadiusSq(mass))
}
// GravityFieldRadiusSq returns the squared max gravity field radius of given mass
func (e *Engine) GravityFieldRadiusSq(mass float64) float64 {
return G * mass / e.cfg.MinAccel
}
// 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 Vec3) Vec3 {
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
}