-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathCollisionB2ShapeChain.go
198 lines (159 loc) · 5.22 KB
/
CollisionB2ShapeChain.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
package box2d
/// A chain shape is a free form sequence of line segments.
/// The chain has two-sided collision, so you can use inside and outside collision.
/// Therefore, you may use any winding order.
/// Since there may be many vertices, they are allocated using b2Alloc.
/// Connectivity information is used to create smooth collisions.
/// WARNING: The chain will not collide properly if there are self-intersections.
/// A circle shape.
type B2ChainShape struct {
B2Shape
/// The vertices. Owned by this class.
M_vertices []B2Vec2
/// The vertex count.
M_count int
M_prevVertex B2Vec2
M_nextVertex B2Vec2
M_hasPrevVertex bool
M_hasNextVertex bool
}
func MakeB2ChainShape() B2ChainShape {
return B2ChainShape{
B2Shape: B2Shape{
M_type: B2Shape_Type.E_chain,
M_radius: B2_polygonRadius,
},
M_vertices: nil,
M_count: 0,
M_hasPrevVertex: false,
M_hasNextVertex: false,
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// B2ChainShape.cpp
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
func (chain *B2ChainShape) Destroy() {
chain.Clear()
}
func (chain *B2ChainShape) Clear() {
chain.M_vertices = nil
chain.M_count = 0
}
func (chain *B2ChainShape) CreateLoop(vertices []B2Vec2, count int) {
B2Assert(chain.M_vertices == nil && chain.M_count == 0)
B2Assert(count >= 3)
if count < 3 {
return
}
for i := 1; i < count; i++ {
v1 := vertices[i-1]
v2 := vertices[i]
// If the code crashes here, it means your vertices are too close together.
B2Assert(B2Vec2DistanceSquared(v1, v2) > B2_linearSlop*B2_linearSlop)
}
chain.M_count = count + 1
chain.M_vertices = make([]B2Vec2, chain.M_count)
for i, vertice := range vertices {
chain.M_vertices[i] = vertice
}
chain.M_vertices[count] = chain.M_vertices[0]
chain.M_prevVertex = chain.M_vertices[chain.M_count-2]
chain.M_nextVertex = chain.M_vertices[1]
chain.M_hasPrevVertex = true
chain.M_hasNextVertex = true
}
func (chain *B2ChainShape) CreateChain(vertices []B2Vec2, count int) {
B2Assert(chain.M_vertices == nil && chain.M_count == 0)
B2Assert(count >= 2)
for i := 1; i < count; i++ {
// If the code crashes here, it means your vertices are too close together.
B2Assert(B2Vec2DistanceSquared(vertices[i-1], vertices[i]) > B2_linearSlop*B2_linearSlop)
}
chain.M_count = count
chain.M_vertices = make([]B2Vec2, count)
for i, vertice := range vertices {
chain.M_vertices[i] = vertice
}
chain.M_hasPrevVertex = false
chain.M_hasNextVertex = false
chain.M_prevVertex.SetZero()
chain.M_nextVertex.SetZero()
}
func (chain *B2ChainShape) SetPrevVertex(prevVertex B2Vec2) {
chain.M_prevVertex = prevVertex
chain.M_hasPrevVertex = true
}
func (chain *B2ChainShape) SetNextVertex(nextVertex B2Vec2) {
chain.M_nextVertex = nextVertex
chain.M_hasNextVertex = true
}
func (chain B2ChainShape) Clone() B2ShapeInterface {
clone := MakeB2ChainShape()
clone.CreateChain(chain.M_vertices, chain.M_count)
clone.M_prevVertex = chain.M_prevVertex
clone.M_nextVertex = chain.M_nextVertex
clone.M_hasPrevVertex = chain.M_hasPrevVertex
clone.M_hasNextVertex = chain.M_hasNextVertex
return &clone
}
func (chain B2ChainShape) GetChildCount() int {
// edge count = vertex count - 1
return chain.M_count - 1
}
func (chain B2ChainShape) GetChildEdge(edge *B2EdgeShape, index int) {
B2Assert(0 <= index && index < chain.M_count-1)
edge.M_type = B2Shape_Type.E_edge
edge.M_radius = chain.M_radius
edge.M_vertex1 = chain.M_vertices[index+0]
edge.M_vertex2 = chain.M_vertices[index+1]
if index > 0 {
edge.M_vertex0 = chain.M_vertices[index-1]
edge.M_hasVertex0 = true
} else {
edge.M_vertex0 = chain.M_prevVertex
edge.M_hasVertex0 = chain.M_hasPrevVertex
}
if index < chain.M_count-2 {
edge.M_vertex3 = chain.M_vertices[index+2]
edge.M_hasVertex3 = true
} else {
edge.M_vertex3 = chain.M_nextVertex
edge.M_hasVertex3 = chain.M_hasNextVertex
}
}
func (chain B2ChainShape) TestPoint(xf B2Transform, p B2Vec2) bool {
return false
}
func (chain B2ChainShape) RayCast(output *B2RayCastOutput, input B2RayCastInput, xf B2Transform, childIndex int) bool {
B2Assert(childIndex < chain.M_count)
edgeShape := MakeB2EdgeShape()
i1 := childIndex
i2 := childIndex + 1
if i2 == chain.M_count {
i2 = 0
}
edgeShape.M_vertex1 = chain.M_vertices[i1]
edgeShape.M_vertex2 = chain.M_vertices[i2]
return edgeShape.RayCast(output, input, xf, 0)
}
func (chain B2ChainShape) ComputeAABB(aabb *B2AABB, xf B2Transform, childIndex int) {
B2Assert(childIndex < chain.M_count)
i1 := childIndex
i2 := childIndex + 1
if i2 == chain.M_count {
i2 = 0
}
v1 := B2TransformVec2Mul(xf, chain.M_vertices[i1])
v2 := B2TransformVec2Mul(xf, chain.M_vertices[i2])
aabb.LowerBound = B2Vec2Min(v1, v2)
aabb.UpperBound = B2Vec2Max(v1, v2)
}
func (chain B2ChainShape) ComputeMass(massData *B2MassData, density float64) {
massData.Mass = 0.0
massData.Center.SetZero()
massData.I = 0.0
}