This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshape.go
78 lines (62 loc) · 1.95 KB
/
shape.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
package cp
/*
#cgo CFLAGS: -DCP_USE_CGPOINTS=0
#cgo LDFLAGS: -lchipmunk
#include <chipmunk/chipmunk.h>
*/
import "C"
var shapeLookup map[*C.cpShape]*Shape = make(map[*C.cpShape]*Shape)
type Shape struct {
CPShape *C.cpShape
}
func createAndRegister(cpshape *C.cpShape) *Shape {
shape := Shape{cpshape}
shapeLookup[cpshape] = &shape
return &shape
}
func NewSegmentShape(body *Body, a Vect, b Vect, radius float64) *Shape {
var cpshape *C.cpShape = C.cpSegmentShapeNew(body.CPBody, a.CPVect, b.CPVect, C.cpFloat(radius))
return createAndRegister(cpshape)
}
func NewCircleShape(body *Body, radius float64, offset Vect) *Shape {
var cpshape *C.cpShape = C.cpCircleShapeNew(body.CPBody, C.cpFloat(radius), offset.CPVect)
return createAndRegister(cpshape)
}
func NewBoxShape(body *Body, width float64, height float64) *Shape {
var cpshape *C.cpShape = C.cpBoxShapeNew(body.CPBody, C.cpFloat(width), C.cpFloat(height))
return createAndRegister(cpshape)
}
func NewPolyShape(body *Body, verts []Vect, offset Vect) *Shape {
cpverts := make([]C.cpVect, 0)
for _, vert := range verts {
cpverts = append(cpverts, vert.CPVect)
}
var cpshape *C.cpShape = C.cpPolyShapeNew(body.CPBody, C.int(len(verts)), &cpverts[0], offset.CPVect)
return createAndRegister(cpshape)
}
func LookupShape(cpshape *C.cpShape) *Shape {
return shapeLookup[cpshape]
}
func (s *Shape) Free() {
delete(shapeLookup, s.CPShape)
C.cpShapeFree(s.CPShape)
s.CPShape = nil
}
func (s *Shape) SetFriction(friction float64) {
s.CPShape.u = C.cpFloat(friction)
}
func (s *Shape) GetCollisionType() uint {
return uint(s.CPShape.collision_type)
}
func (s *Shape) SetCollisionType(ctype uint) {
s.CPShape.collision_type = C.cpCollisionType(ctype)
}
func (s *Shape) GetCollisionGroup() uint {
return uint(s.CPShape.group)
}
func (s *Shape) SetCollisionGroup(group uint) {
s.CPShape.group = C.cpGroup(group)
}
func (s *Shape) GetBody() *Body {
return LookupBody(s.CPShape.body)
}