-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshapes.go
64 lines (49 loc) · 1.8 KB
/
shapes.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
package cpebiten
import "github.com/jakecoffman/cp/v2"
func AddWall(space *cp.Space, body *cp.Body, a, b cp.Vector, radius float64) *cp.Shape {
// swap so we always draw the same direction horizontally
if a.X < b.X {
a, b = b, a
}
seg := cp.NewSegment(body, a, b, radius).Class.(*cp.Segment)
shape := space.AddShape(seg.Shape)
shape.SetElasticity(1)
shape.SetFriction(1)
shape.SetFilter(NotGrabbable)
return shape
}
func AddSegment(space *cp.Space, pos cp.Vector, mass, width, height float64) *cp.Shape {
body := space.AddBody(cp.NewBody(mass, cp.MomentForBox(mass, width, height)))
body.SetPosition(pos)
a, b := cp.Vector{0, (height - width) / 2.0}, cp.Vector{0, (width - height) / 2.0}
seg := cp.NewSegment(body, a, b, width/2.0).Class.(*cp.Segment)
shape := space.AddShape(seg.Shape)
shape.SetElasticity(0)
shape.SetFriction(0.7)
return shape
}
func AddBox(space *cp.Space, pos cp.Vector, mass, width, height float64) *cp.Shape {
body := space.AddBody(cp.NewBody(mass, cp.MomentForBox(mass, width, height)))
body.SetPosition(pos)
shape := space.AddShape(cp.NewBox(body, width, height, 0))
shape.SetElasticity(0)
shape.SetFriction(0.7)
return shape
}
func AddStaticBox(space *cp.Space, pos cp.Vector, width, height float64) *cp.Shape {
body := space.AddBody(cp.NewKinematicBody())
body.SetPosition(pos)
shape := space.AddShape(cp.NewBox(body, width, height, 0))
shape.SetElasticity(0)
shape.SetFriction(0.7)
return shape
}
func AddCircle(space *cp.Space, pos cp.Vector, mass, radius float64) *cp.Shape {
body := space.AddBody(cp.NewBody(mass, cp.MomentForCircle(mass, 0, radius, cp.Vector{})))
body.SetPosition(pos)
circle := cp.NewCircle(body, radius, cp.Vector{}).Class.(*cp.Circle)
shape := space.AddShape(circle.Shape)
shape.SetElasticity(0)
shape.SetFriction(0.7)
return shape
}