This repository has been archived by the owner on May 27, 2022. It is now read-only.
forked from vova616/chipmunk
-
Notifications
You must be signed in to change notification settings - Fork 5
/
body.go
362 lines (283 loc) · 7.15 KB
/
body.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package chipmunk
import (
"github.com/dataarts/chipmunk/vect"
//. "github.com/dataarts/chipmunk/transform"
"math"
)
type ComponentNode struct {
Root *Body
Next *Body
IdleTime vect.Float
}
type BodyType uint8
type UpdatePositionFunction func(body *Body, dt vect.Float)
type UpdateVelocityFunction func(body *Body, gravity vect.Vect, damping, dt vect.Float)
const (
BodyType_Static = BodyType(0)
BodyType_Dynamic = BodyType(1)
)
var Inf = vect.Float(math.Inf(1))
type CollisionCallback interface {
CollisionEnter(arbiter *Arbiter) bool
CollisionPreSolve(arbiter *Arbiter) bool
CollisionPostSolve(arbiter *Arbiter)
CollisionExit(arbiter *Arbiter)
}
type Body struct {
/// Mass of the body.
/// Must agree with cpBody.m_inv! Use cpBodySetMass() when changing the mass for this reason.
m vect.Float
/// Mass inverse.
m_inv vect.Float
/// Moment of inertia of the body.
/// Must agree with cpBody.i_inv! Use cpBodySetMoment() when changing the moment for this reason.
i vect.Float
/// Moment of inertia inverse.
i_inv vect.Float
/// Position of the rigid body's center of gravity.
p vect.Vect
/// Velocity of the rigid body's center of gravity.
v vect.Vect
/// Force acting on the rigid body's center of gravity.
f vect.Vect
//Transform Transform
/// Rotation of the body around it's center of gravity in radians.
/// Must agree with cpBody.rot! Use cpBodySetAngle() when changing the angle for this reason.
a vect.Float
/// Angular velocity of the body around it's center of gravity in radians/second.
w vect.Float
/// Torque applied to the body around it's center of gravity.
t vect.Float
/// Cached unit length vector representing the angle of the body.
/// Used for fast rotations using cpvrotate().
rot vect.Vect
v_bias vect.Vect
w_bias vect.Float
/// User definable data pointer.
/// Generally this points to your the game object class so you can access it
/// when given a cpBody reference in a callback.
UserData interface{}
CallbackHandler CollisionCallback
UpdatePositionFunc UpdatePositionFunction
UpdateVelocityFunc UpdateVelocityFunction
/// Maximum velocity allowed when updating the velocity.
v_limit vect.Float
/// Maximum rotational rate (in radians/second) allowed when updating the angular velocity.
w_limit vect.Float
space *Space
Shapes []*Shape
node ComponentNode
hash HashValue
deleted bool
Enabled bool
idleTime vect.Float
IgnoreGravity bool
}
func NewBodyStatic() (body *Body) {
body = &Body{}
body.Shapes = make([]*Shape, 0)
body.SetMass(Inf)
body.SetMoment(Inf)
body.IgnoreGravity = true
body.node.IdleTime = Inf
body.SetAngle(0)
body.Enabled = true
return
}
func NewBody(mass, i vect.Float) (body *Body) {
body = &Body{}
body.Shapes = make([]*Shape, 0)
body.SetMass(mass)
body.SetMoment(i)
body.SetAngle(0)
body.Enabled = true
return
}
func (body *Body) AddShape(shape *Shape) {
body.Shapes = append(body.Shapes, shape)
shape.Body = body
}
func (body *Body) Clone() *Body {
clone := *body
clone.Shapes = make([]*Shape, 0)
for _, shape := range body.Shapes {
clone.AddShape(shape.Clone())
}
clone.space = nil
clone.hash = 0
return &clone
}
func (body *Body) KineticEnergy() vect.Float {
vsq := vect.Dot(body.v, body.v)
wsq := body.w * body.w
if vsq != 0 {
vsq = vsq * body.m
}
if wsq != 0 {
wsq = wsq * body.i
}
return vsq + wsq
}
func (body *Body) SetMass(mass vect.Float) {
if mass <= 0 {
panic("Mass must be positive and non-zero.")
}
body.BodyActivate()
body.m = mass
body.m_inv = 1 / mass
}
func (body *Body) SetMoment(moment vect.Float) {
if moment <= 0 {
panic("Moment of Inertia must be positive and non-zero.")
}
body.BodyActivate()
body.i = moment
body.i_inv = 1 / moment
}
func (body *Body) Moment() float32 {
return float32(body.i)
}
func (body *Body) MomentIsInf() bool {
return math.IsInf(float64(body.i), 0)
}
func (body *Body) SetAngle(angle vect.Float) {
body.BodyActivate()
body.setAngle(angle)
}
func (body *Body) AddAngle(angle float32) {
body.SetAngle(vect.Float(angle) + body.Angle())
}
func (body *Body) Mass() vect.Float {
return body.m
}
func (body *Body) setAngle(angle vect.Float) {
body.a = angle
body.rot = vect.FromAngle(angle)
}
func (body *Body) BodyActivate() {
//TODO: make it work with sleeping
if body.IsStatic() {
return
}
if !body.IsRogue() {
body.node.IdleTime = 0
}
}
func (body *Body) ComponentRoot() *Body {
if body != nil {
return body.node.Root
}
return nil
}
func (body *Body) ComponentActive() {
if body.IsSleeping() || body.IsRogue() {
return
}
return
space := body.space
b := body
for b != nil {
next := b.node.Next
b.node.IdleTime = 0
b.node.Root = nil
b.node.Next = nil
space.ActiveBody(body)
b = next
}
//for i,sleeping
//cpArrayDeleteObj(space->sleepingComponents, root);
}
func (body *Body) IsRogue() bool {
return body.space == nil
}
func (body *Body) IsSleeping() bool {
return body.node.Root != nil
}
func (body *Body) IsStatic() bool {
return math.IsInf(float64(body.node.IdleTime), 0)
}
func (body *Body) UpdateShapes() {
for _, shape := range body.Shapes {
shape.Update()
}
}
func (body *Body) SetPosition(pos vect.Vect) {
body.p = pos
}
func (body *Body) AddForce(x, y float32) {
body.f.X += vect.Float(x)
body.f.Y += vect.Float(y)
}
func (body *Body) SetForce(x, y float32) {
body.f.X = vect.Float(x)
body.f.Y = vect.Float(y)
}
func (body *Body) AddVelocity(x, y float32) {
body.v.X += vect.Float(x)
body.v.Y += vect.Float(y)
}
func (body *Body) SetVelocity(x, y float32) {
body.v.X = vect.Float(x)
body.v.Y = vect.Float(y)
}
func (body *Body) AddTorque(t float32) {
body.t += vect.Float(t)
}
func (body *Body) Torque() float32 {
return float32(body.t)
}
func (body *Body) VBias() vect.Vect {
return body.v_bias
}
func (body *Body) WBias() float32 {
return float32(body.w_bias)
}
func (body *Body) SetVBias(v vect.Vect) {
body.v_bias = v
}
func (body *Body) SetWBias(w float32) {
body.w_bias = vect.Float(w)
}
func (body *Body) AngularVelocity() float32 {
return float32(body.w)
}
func (body *Body) SetTorque(t float32) {
body.t = vect.Float(t)
}
func (body *Body) AddAngularVelocity(w float32) {
body.w += vect.Float(w)
}
func (body *Body) SetAngularVelocity(w float32) {
body.w = vect.Float(w)
}
func (body *Body) Velocity() vect.Vect {
return body.v
}
func (body *Body) Position() vect.Vect {
return body.p
}
func (body *Body) Angle() vect.Float {
return body.a
}
func (body *Body) Rot() (rx, ry float32) {
return float32(body.rot.X), float32(body.rot.Y)
}
func (body *Body) UpdatePosition(dt vect.Float) {
if body.UpdatePositionFunc != nil {
body.UpdatePositionFunc(body, dt)
return
}
body.p = vect.Add(body.p, vect.Mult(vect.Add(body.v, body.v_bias), dt))
body.setAngle(body.a + (body.w+body.w_bias)*dt)
body.v_bias = vect.Vector_Zero
body.w_bias = 0.0
}
func (body *Body) UpdateVelocity(gravity vect.Vect, damping, dt vect.Float) {
if body.UpdateVelocityFunc != nil {
body.UpdateVelocityFunc(body, gravity, damping, dt)
return
}
body.v = vect.Add(vect.Mult(body.v, damping), vect.Mult(vect.Add(gravity, vect.Mult(body.f, body.m_inv)), dt))
body.w = (body.w * damping) + (body.t * body.i_inv * dt)
body.f = vect.Vector_Zero
}