This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
/
body.go
75 lines (58 loc) · 1.52 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
package cp
/*
#cgo CFLAGS: -DCP_USE_CGPOINTS=0
#cgo LDFLAGS: -lchipmunk
#include <chipmunk/chipmunk.h>
*/
import "C"
type Body struct {
CPBody *C.cpBody
}
var bodyLookup map[*C.cpBody]*Body = make(map[*C.cpBody]*Body)
func NewBody(mass float64, moment float64) *Body {
var cpbody *C.cpBody = C.cpBodyNew(C.cpFloat(mass), C.cpFloat(moment))
body := Body{cpbody}
bodyLookup[cpbody] = &body
return &body
}
func LookupBody(cpbody *C.cpBody) *Body {
return bodyLookup[cpbody]
}
func (b *Body) Free() {
delete(bodyLookup, b.CPBody)
C.cpBodyFree(b.CPBody)
b.CPBody = nil
}
func (b *Body) GetPosition() Vect {
return Vect{C.cpBodyGetPos(b.CPBody)}
}
func (b *Body) SetPosition(pos Vect) {
C.cpBodySetPos(b.CPBody, pos.CPVect)
}
func (b *Body) GetAngle() float64 {
return float64(b.CPBody.a)
}
func (b *Body) SetAngle(angle float64) {
C.cpBodySetAngle(b.CPBody, C.cpFloat(angle))
}
func (b *Body) GetVelocity() Vect {
return Vect{C.cpBodyGetVel(b.CPBody)}
}
func (b *Body) SetVelocity(v Vect) {
b.CPBody.v = v.CPVect
}
func (b *Body) SetVelocityLimit(limit float64) {
b.CPBody.v_limit = C.cpFloat(limit)
}
func (b *Body) SetAngularRotation(omega float64) {
b.CPBody.w = C.cpFloat(omega)
}
func (b *Body) MultiplyAngularRotation(mult float64) {
b.CPBody.w *= C.cpFloat(mult)
}
func (b *Body) ApplyForce(force Vect, offset Vect) {
C.cpBodyApplyForce(b.CPBody, force.CPVect, offset.CPVect)
}
func (b *Body) ApplyImpulse(impulse Vect, offset Vect) {
C.cpBodyApplyImpulse(b.CPBody, impulse.CPVect, offset.CPVect)
}