forked from jakecoffman/cp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.go
139 lines (109 loc) · 3.55 KB
/
slice.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
package main
import (
"math"
. "github.com/jakecoffman/cp"
"github.com/jakecoffman/cp/examples"
)
const DENSITY = 1.0 / 10000.0
func ClipPoly(space *Space, shape *Shape, n Vector, dist float64) {
body := shape.Body()
poly := shape.Class.(*PolyShape)
count := poly.Count()
var clippedCount int
clipped := make([]Vector, count+1)
j := count - 1
for i := 0; i < count; i++ {
a := body.LocalToWorld(poly.Vert(j))
aDist := a.Dot(n) - dist
if aDist < 0 {
clipped[clippedCount] = a
clippedCount++
}
b := body.LocalToWorld(poly.Vert(i))
bDist := b.Dot(n) - dist
if aDist*bDist < 0 {
t := math.Abs(aDist) / (math.Abs(aDist) + math.Abs(bDist))
clipped[clippedCount] = a.Lerp(b, t)
clippedCount++
}
j = i
}
centroid := CentroidForPoly(clippedCount, clipped)
mass := AreaForPoly(clippedCount, clipped, 0) * DENSITY
moment := MomentForPoly(mass, clippedCount, clipped, centroid.Neg(), 0)
newBody := space.AddBody(NewBody(mass, moment))
newBody.SetPosition(centroid)
newBody.SetVelocityVector(body.VelocityAtWorldPoint(centroid))
newBody.SetAngularVelocity(body.AngularVelocity())
transform := NewTransformTranslate(centroid.Neg())
newShape := space.AddShape(NewPolyShape(newBody, clippedCount, clipped, transform, 0))
newShape.SetFriction(shape.Friction())
}
// Context structs are annoying, use blocks or closures instead if your compiler supports them.
type SliceContext struct {
a, b Vector
space *Space
}
func SliceShapePostStep(space *Space, ptr, obj interface{}) {
shape := ptr.(*Shape)
context := obj.(*SliceContext)
a := context.a
b := context.b
// Clipping plane normal and distance.
n := b.Sub(a).Perp().Normalize()
dist := a.Dot(n)
ClipPoly(space, shape, n, dist)
ClipPoly(space, shape, n.Neg(), -dist)
body := shape.Body()
space.RemoveShape(shape)
space.RemoveBody(body)
}
func SliceQuery(shape *Shape, point, normal Vector, alpha float64, data interface{}) {
context := data.(*SliceContext)
a := context.a
b := context.b
// Check that the slice was complete by checking that the endpoints aren't in the sliced shape.
if shape.PointQuery(a).Distance > 0 && shape.PointQuery(b).Distance > 0 {
// Can't modify the space during a query.
// Must make a post-step callback to do the actual slicing.
context.space.AddPostStepCallback(SliceShapePostStep, shape, context)
}
}
func main() {
space := NewSpace()
space.Iterations = 30
space.SetGravity(Vector{0, -500})
space.SleepTimeThreshold = 0.5
space.SetCollisionSlop(0.5)
shape := space.AddShape(NewSegment(space.StaticBody, Vector{-1000, -240}, Vector{1000, -240}, 0))
shape.SetElasticity(1)
shape.SetFriction(1)
shape.SetFilter(examples.NotGrabbableFilter)
width := 200.0
height := 300.0
mass := width * height * DENSITY
moment := MomentForBox(mass, width, height)
body := space.AddBody(NewBody(mass, moment))
shape = space.AddShape(NewBox(body, width, height, 0))
shape.SetFriction(0.6)
examples.Main(space, 1.0/60.0, update, examples.DefaultDraw)
}
var lastClickState bool
var sliceStart Vector
func update(space *Space, dt float64) {
space.Step(dt)
// Annoying state tracking code that you wouldn't need
// in a real event driven system.
if examples.RightClick != lastClickState {
if examples.RightClick {
sliceStart = examples.Mouse
} else {
context := SliceContext{sliceStart, examples.Mouse, space}
space.SegmentQuery(sliceStart, examples.Mouse, 0, examples.GrabFilter, SliceQuery, &context)
}
lastClickState = examples.RightClick
}
if examples.RightClick {
examples.DrawSegment(sliceStart, examples.Mouse, FColor{1, 0, 0, 1})
}
}