/
Poly.java
209 lines (178 loc) · 5.6 KB
/
Poly.java
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
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright (c) 2012 matheusdev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.matheusdev.util.collision;
import org.matheusdev.util.Average;
import org.matheusdev.util.NumUtils;
import org.matheusdev.util.vecmath.Mat4;
import org.matheusdev.util.vecmath.Vec2;
/**
* @author matheusdev
*
*/
public class Poly implements SATObject {
protected Vec2 center;
protected Vec2 centerCached;
protected final Vec2[] vertices;
protected final Vec2[] verticesCached;
protected final Vec2[] normals;
protected Rect aabb;
protected Circle circBounds;
protected Mat4 mat;
public Poly(Vec2... vertices) {
final int elements = vertices.length;
if (elements < 3) {
throw new IllegalArgumentException("elements < 3: This is not a Polygon.");
}
this.vertices = vertices;
this.verticesCached = new Vec2[elements];
this.center = calculateCenter();
this.centerCached = new Vec2();
this.normals = new Vec2[elements];
for (int i = 0; i < normals.length; i++) normals[i] = new Vec2();
this.mat = new Mat4();
this.aabb = new Rect();
this.circBounds = createCircularBounds(circBounds);
updateFromMatrix();
}
protected Vec2 calculateCenter() {
float[] vertX = new float[vertices.length];
float[] vertY = new float[vertices.length];
for (int i = 0; i < vertices.length; i++) {
vertX[i] = vertices[i].x;
vertY[i] = vertices[i].y;
}
return new Vec2(Average.get(vertX), Average.get(vertY));
}
public Poly updateFromMatrix() {
return updateFromMatrix(mat);
}
public Poly updateFromMatrix(Mat4 mat) {
for (int i = 0; i < vertices.length; i++) {
// if verticesChached[i] is null in the call to Vec2.transform,
// then a new Vec2 instance will be automatically created.
verticesCached[i] = Vec2.transform(vertices[i], mat, verticesCached[i]);
}
// The same applies to the single center Vec2:
centerCached = Vec2.transform(center, mat, centerCached);
// Finally update normals:
for (int i = 0; i < normals.length; i++) {
calculateNormal(i);
}
return this;
}
protected Vec2 calculateNormal(int side) {
Vec2 v0 = getTransformedVertex(side);
Vec2 v1 = getTransformedVertex(side + 1);
return normals[side].set(v1.x - v0.x, v1.y - v0.y).perpLeft().normalize();
}
protected Circle createCircularBounds(Circle dest) {
float maxDist = 0f;
for (int i = 0; i < vertices.length; i++) {
float dx = vertices[i].x - center.x;
float dy = vertices[i].y - center.y;
maxDist = Math.max(maxDist, (float) Math.sqrt(dx * dx + dy * dy));
}
if (dest == null) dest = new Circle(null, 0f);
dest.set(centerCached, maxDist);
return dest;
}
public Mat4 getMatrix() {
return mat;
}
public Poly setMatrix(Mat4 mat) {
this.mat = mat;
return this;
}
public int getNumVertices() {
return vertices.length;
}
public Vec2 getOrigin() {
return center;
}
public Vec2 getPosition() {
return centerCached;
}
public Vec2 getVertex(int i) {
return vertices[NumUtils.clamp(i, vertices.length)];
}
public Vec2 getTransformedVertex(int i) {
return verticesCached[NumUtils.clamp(i, verticesCached.length)];
}
public Vec2 getNormal(int side) {
return normals[NumUtils.clamp(side, normals.length)];
}
@Override
public Vec2[] getTransformedVertices() {
return verticesCached;
}
/* (non-Javadoc)
* @see org.matheusdev.util.collision.SATObject#getAxes()
*/
@Override
public Vec2[] getAxes() {
return normals;
}
/* (non-Javadoc)
* @see org.matheusdev.util.collision.SATObject#project(org.matheusdev.util.vecmath.Vec2)
*/
@Override
public Vec2 project(Vec2 axis, Vec2 dest) {
if (dest == null) dest = new Vec2();
float min = Vec2.dot(axis, verticesCached[0]);
float max = min;
for (int i = 1; i < verticesCached.length; i++) {
float p = Vec2.dot(axis, verticesCached[i]);
if (p < min) {
min = p;
} else if (p > max) {
max = p;
}
}
return dest.set(min, max);
}
/* (non-Javadoc)
* @see org.matheusdev.util.collision.SATObject#getAABB()
*/
@Override
public Rect getAABB() {
float minx = verticesCached[0].x;
float miny = verticesCached[0].y;
float maxx = verticesCached[0].x;
float maxy = verticesCached[0].y;
for (int i = 1; i < verticesCached.length; i++) {
minx = Math.min(verticesCached[i].x, minx);
miny = Math.min(verticesCached[i].y, miny);
maxx = Math.max(verticesCached[i].x, maxx);
maxy = Math.max(verticesCached[i].y, maxy);
}
float w = maxx-minx;
float h = maxy-miny;
return aabb.set(minx, miny, w, h);
}
/* (non-Javadoc)
* @see org.matheusdev.util.collision.Easifyable#getBounds()
*/
@Override
public Circle getBounds() {
return circBounds.setCenter(centerCached);
}
}