insilmaril@650
|
1 |
#include "geometry.h"
|
insilmaril@650
|
2 |
|
insilmaril@656
|
3 |
#include <math.h>
|
insilmaril@662
|
4 |
#include "misc.h"
|
insilmaril@656
|
5 |
|
insilmaril@792
|
6 |
#include <QString>
|
insilmaril@792
|
7 |
|
insilmaril@789
|
8 |
#include <iostream>
|
insilmaril@662
|
9 |
using namespace std;
|
insilmaril@650
|
10 |
|
insilmaril@789
|
11 |
|
insilmaril@650
|
12 |
QRectF addBBox(QRectF r1, QRectF r2)
|
insilmaril@650
|
13 |
{
|
insilmaril@650
|
14 |
// Find smallest QRectF containing given rectangles
|
insilmaril@650
|
15 |
|
insilmaril@650
|
16 |
QRectF n;
|
insilmaril@650
|
17 |
// Set left border
|
insilmaril@650
|
18 |
if (r1.left() <= r2.left() )
|
insilmaril@650
|
19 |
n.setLeft(r1.left() );
|
insilmaril@650
|
20 |
else
|
insilmaril@650
|
21 |
n.setLeft(r2.left() );
|
insilmaril@650
|
22 |
|
insilmaril@650
|
23 |
// Set top border
|
insilmaril@650
|
24 |
if (r1.top() <= r2.top() )
|
insilmaril@650
|
25 |
n.setTop(r1.top() );
|
insilmaril@650
|
26 |
else
|
insilmaril@650
|
27 |
n.setTop(r2.top() );
|
insilmaril@650
|
28 |
|
insilmaril@650
|
29 |
// Set right border
|
insilmaril@650
|
30 |
if (r1.right() <= r2.right() )
|
insilmaril@650
|
31 |
n.setRight(r2.right() );
|
insilmaril@650
|
32 |
else
|
insilmaril@650
|
33 |
n.setRight(r1.right() );
|
insilmaril@650
|
34 |
|
insilmaril@650
|
35 |
// Set bottom
|
insilmaril@650
|
36 |
if (r1.bottom() <= r2.bottom() )
|
insilmaril@650
|
37 |
n.setBottom(r2.bottom() );
|
insilmaril@650
|
38 |
else
|
insilmaril@650
|
39 |
n.setBottom(r1.bottom() );
|
insilmaril@650
|
40 |
return n;
|
insilmaril@650
|
41 |
}
|
insilmaril@650
|
42 |
|
insilmaril@754
|
43 |
bool isInBox(const QPointF &p, const QRectF &box)
|
insilmaril@650
|
44 |
{
|
insilmaril@650
|
45 |
if (p.x() >= box.left() && p.x() <= box.right()
|
insilmaril@650
|
46 |
&& p.y() <= box.bottom() && p.y() >= box.top() )
|
insilmaril@650
|
47 |
return true;
|
insilmaril@650
|
48 |
return false;
|
insilmaril@650
|
49 |
}
|
insilmaril@656
|
50 |
|
insilmaril@792
|
51 |
Vector::Vector ():QPointF ()
|
insilmaril@792
|
52 |
{
|
insilmaril@792
|
53 |
}
|
insilmaril@792
|
54 |
|
insilmaril@792
|
55 |
Vector::Vector (const QPointF &p):QPointF (p)
|
insilmaril@792
|
56 |
{
|
insilmaril@792
|
57 |
}
|
insilmaril@792
|
58 |
|
insilmaril@792
|
59 |
Vector::Vector (qreal x, qreal y):QPointF (x,y)
|
insilmaril@792
|
60 |
{
|
insilmaril@792
|
61 |
}
|
insilmaril@792
|
62 |
|
insilmaril@792
|
63 |
//! Normalize vector
|
insilmaril@792
|
64 |
void Vector::normalize ()
|
insilmaril@792
|
65 |
{
|
insilmaril@792
|
66 |
if (x()==0 && y()==0) return;
|
insilmaril@792
|
67 |
qreal l=sqrt ( x()*x() + y()*y() );
|
insilmaril@792
|
68 |
setX (x()/l);
|
insilmaril@792
|
69 |
setY (y()/l);
|
insilmaril@792
|
70 |
}
|
insilmaril@792
|
71 |
|
insilmaril@792
|
72 |
//! Dot product of two vectors
|
insilmaril@792
|
73 |
qreal Vector::dotProduct (const QPointF &b)
|
insilmaril@792
|
74 |
{
|
insilmaril@792
|
75 |
return x()*b.x() + y()*b.y();
|
insilmaril@792
|
76 |
}
|
insilmaril@792
|
77 |
|
insilmaril@792
|
78 |
|
insilmaril@792
|
79 |
void Vector::scale (const qreal &f)
|
insilmaril@792
|
80 |
{
|
insilmaril@792
|
81 |
setX (x()*f);
|
insilmaril@792
|
82 |
setY (y()*f);
|
insilmaril@792
|
83 |
}
|
insilmaril@792
|
84 |
|
insilmaril@792
|
85 |
void Vector::invert ()
|
insilmaril@792
|
86 |
{
|
insilmaril@792
|
87 |
setX (-x());
|
insilmaril@792
|
88 |
setY (-y());
|
insilmaril@792
|
89 |
}
|
insilmaril@792
|
90 |
|
insilmaril@792
|
91 |
/*! Calculate the projection of a polygon on an axis
|
insilmaril@792
|
92 |
and returns it as a [min, max] interval */
|
insilmaril@789
|
93 |
ConvexPolygon::ConvexPolygon ()
|
insilmaril@789
|
94 |
{
|
insilmaril@789
|
95 |
}
|
insilmaril@789
|
96 |
|
insilmaril@789
|
97 |
ConvexPolygon::ConvexPolygon (QPolygonF p):QPolygonF (p)
|
insilmaril@789
|
98 |
{
|
insilmaril@789
|
99 |
}
|
insilmaril@789
|
100 |
|
insilmaril@789
|
101 |
void ConvexPolygon::calcCentroid()
|
insilmaril@789
|
102 |
{
|
insilmaril@789
|
103 |
// Calculate area and centroid
|
insilmaril@789
|
104 |
// http://en.wikipedia.org/wiki/Centroid
|
insilmaril@789
|
105 |
qreal cx,cy,p;
|
insilmaril@789
|
106 |
cx=cy=0;
|
insilmaril@789
|
107 |
_area=0;
|
insilmaril@789
|
108 |
|
insilmaril@789
|
109 |
append (at(0));
|
insilmaril@789
|
110 |
for (int i=0;i<size()-1;i++)
|
insilmaril@789
|
111 |
{
|
insilmaril@789
|
112 |
p=at(i).x() * at(i+1).y() - at(i+1).x() * at(i).y();
|
insilmaril@789
|
113 |
_area+=p;
|
insilmaril@789
|
114 |
cx+=(at(i).x()+at(i+1).x()) * p;
|
insilmaril@789
|
115 |
cy+=(at(i).y()+at(i+1).y()) * p;
|
insilmaril@789
|
116 |
}
|
insilmaril@789
|
117 |
pop_back();
|
insilmaril@789
|
118 |
// area is negative if vertices ordered counterclockwise
|
insilmaril@789
|
119 |
// (in mirrored graphicsview!)
|
insilmaril@789
|
120 |
_area=_area/2;
|
insilmaril@789
|
121 |
p=_area*6;
|
insilmaril@789
|
122 |
_centroid.setX (cx/p);
|
insilmaril@789
|
123 |
_centroid.setY (cy/p);
|
insilmaril@789
|
124 |
}
|
insilmaril@789
|
125 |
|
insilmaril@789
|
126 |
QPointF ConvexPolygon::centroid() const
|
insilmaril@789
|
127 |
{
|
insilmaril@789
|
128 |
return _centroid;
|
insilmaril@789
|
129 |
}
|
insilmaril@789
|
130 |
|
insilmaril@789
|
131 |
qreal ConvexPolygon::weight() const
|
insilmaril@789
|
132 |
{
|
insilmaril@789
|
133 |
return _area;
|
insilmaril@789
|
134 |
}
|
insilmaril@789
|
135 |
|
insilmaril@792
|
136 |
std::string ConvexPolygon::toStdString()
|
insilmaril@656
|
137 |
{
|
insilmaril@792
|
138 |
QString s ("(");
|
insilmaril@792
|
139 |
for (int i=0;i<size();++i)
|
insilmaril@792
|
140 |
{
|
insilmaril@792
|
141 |
s+=QString("(%1,%2)").arg(at(i).x()).arg(at(i).y());
|
insilmaril@792
|
142 |
if (i<size()-1) s+=",";
|
insilmaril@792
|
143 |
}
|
insilmaril@792
|
144 |
s+=")";
|
insilmaril@792
|
145 |
return s.toStdString();
|
insilmaril@656
|
146 |
}
|
insilmaril@656
|
147 |
|
insilmaril@792
|
148 |
Vector ConvexPolygon::at(const int &i) const
|
insilmaril@656
|
149 |
{
|
insilmaril@792
|
150 |
return Vector (QPolygonF::at(i).x(),QPolygonF::at(i).y());
|
insilmaril@656
|
151 |
}
|
insilmaril@656
|
152 |
|
insilmaril@792
|
153 |
void ConvexPolygon::translate ( const Vector & offset )
|
insilmaril@792
|
154 |
{ translate (offset.x(),offset.y());}
|
insilmaril@656
|
155 |
|
insilmaril@792
|
156 |
void ConvexPolygon::translate ( qreal dx, qreal dy )
|
insilmaril@789
|
157 |
{
|
insilmaril@792
|
158 |
QPolygonF::translate (dx,dy);
|
insilmaril@792
|
159 |
_centroid=_centroid+QPointF (dx,dy);
|
insilmaril@789
|
160 |
}
|
insilmaril@789
|
161 |
|
insilmaril@792
|
162 |
void projectPolygon(Vector axis, ConvexPolygon polygon, qreal &min, qreal &max)
|
insilmaril@656
|
163 |
{
|
insilmaril@656
|
164 |
// To project a point on an axis use the dot product
|
insilmaril@656
|
165 |
|
insilmaril@789
|
166 |
//cout << "Projecting on "<< axis<<endl;
|
insilmaril@792
|
167 |
qreal d = axis.dotProduct(polygon.at(0));
|
insilmaril@656
|
168 |
min = d;
|
insilmaril@656
|
169 |
max = d;
|
insilmaril@789
|
170 |
for (int i = 0; i < polygon.size(); i++)
|
insilmaril@789
|
171 |
{
|
insilmaril@792
|
172 |
d= polygon.at(i).dotProduct (axis);
|
insilmaril@656
|
173 |
if (d < min)
|
insilmaril@656
|
174 |
min = d;
|
insilmaril@656
|
175 |
else
|
insilmaril@656
|
176 |
if (d> max) max = d;
|
insilmaril@789
|
177 |
// cout << "p="<<polygon.at(i)<<" d="<<d<<" (min, max)=("<<min<<","<<max<<")\n";
|
insilmaril@656
|
178 |
}
|
insilmaril@656
|
179 |
}
|
insilmaril@656
|
180 |
|
insilmaril@789
|
181 |
// Calculate the signed distance between [minA, maxA] and [minB, maxB]
|
insilmaril@789
|
182 |
// The distance will be negative if the intervals overlap
|
insilmaril@656
|
183 |
|
insilmaril@656
|
184 |
qreal intervalDistance(qreal minA, qreal maxA, qreal minB, qreal maxB) {
|
insilmaril@656
|
185 |
if (minA < minB) {
|
insilmaril@656
|
186 |
return minB - maxA;
|
insilmaril@656
|
187 |
} else {
|
insilmaril@656
|
188 |
return minA - maxB;
|
insilmaril@656
|
189 |
}
|
insilmaril@656
|
190 |
}
|
insilmaril@789
|
191 |
|
insilmaril@789
|
192 |
/*!
|
insilmaril@656
|
193 |
Check if polygon A is going to collide with polygon B.
|
insilmaril@656
|
194 |
The last parameter is the *relative* velocity
|
insilmaril@656
|
195 |
of the polygons (i.e. velocityA - velocityB)
|
insilmaril@789
|
196 |
*/
|
insilmaril@656
|
197 |
|
insilmaril@792
|
198 |
PolygonCollisionResult polygonCollision(ConvexPolygon polygonA,
|
insilmaril@792
|
199 |
ConvexPolygon polygonB, Vector velocity)
|
insilmaril@789
|
200 |
{
|
insilmaril@656
|
201 |
PolygonCollisionResult result;
|
insilmaril@662
|
202 |
result.intersect = true;
|
insilmaril@662
|
203 |
result.willIntersect = true;
|
insilmaril@656
|
204 |
|
insilmaril@656
|
205 |
int edgeCountA = polygonA.size();
|
insilmaril@656
|
206 |
int edgeCountB = polygonB.size();
|
insilmaril@656
|
207 |
qreal minIntervalDistance = 1000000000;
|
insilmaril@656
|
208 |
QPointF translationAxis;
|
insilmaril@656
|
209 |
QPointF edge;
|
insilmaril@656
|
210 |
|
insilmaril@789
|
211 |
/*
|
insilmaril@662
|
212 |
cout << "\nA: ";
|
insilmaril@662
|
213 |
for (int k=0; k<edgeCountA;k++)
|
insilmaril@662
|
214 |
cout <<polygonA.at(k);
|
insilmaril@662
|
215 |
cout << "\nB: ";
|
insilmaril@662
|
216 |
for (int k=0; k<edgeCountB;k++)
|
insilmaril@662
|
217 |
cout <<polygonB.at(k);
|
insilmaril@789
|
218 |
cout <<endl;
|
insilmaril@789
|
219 |
*/
|
insilmaril@662
|
220 |
|
insilmaril@656
|
221 |
// Loop through all the edges of both polygons
|
insilmaril@789
|
222 |
for (int i=0;i<edgeCountA + edgeCountB;i++)
|
insilmaril@656
|
223 |
{
|
insilmaril@656
|
224 |
if (i< edgeCountA)
|
insilmaril@662
|
225 |
{
|
insilmaril@789
|
226 |
// Loop through polygon A
|
insilmaril@789
|
227 |
if (i<edgeCountA-1)
|
insilmaril@662
|
228 |
edge = QPointF (
|
insilmaril@662
|
229 |
polygonA.at(i+1).x()-polygonA.at(i).x(),
|
insilmaril@662
|
230 |
polygonA.at(i+1).y()-polygonA.at(i).y());
|
insilmaril@662
|
231 |
else
|
insilmaril@662
|
232 |
edge = QPointF (
|
insilmaril@662
|
233 |
polygonA.at(0).x()-polygonA.at(i).x(),
|
insilmaril@662
|
234 |
polygonA.at(0).y()-polygonA.at(i).y());
|
insilmaril@662
|
235 |
} else
|
insilmaril@662
|
236 |
{
|
insilmaril@789
|
237 |
// Loop through polygon B
|
insilmaril@789
|
238 |
if (i < edgeCountA +edgeCountB -1 )
|
insilmaril@662
|
239 |
edge = QPointF (
|
insilmaril@789
|
240 |
polygonB.at(i-edgeCountA+1).x() - polygonB.at(i-edgeCountA).x(),
|
insilmaril@789
|
241 |
polygonB.at(i-edgeCountA+1).y() - polygonB.at(i-edgeCountA).y());
|
insilmaril@662
|
242 |
else
|
insilmaril@662
|
243 |
edge = QPointF (
|
insilmaril@789
|
244 |
polygonB.at(0).x() - polygonB.at(i-edgeCountA).x(),
|
insilmaril@789
|
245 |
polygonB.at(0).y() - polygonB.at(i-edgeCountA).y());
|
insilmaril@662
|
246 |
}
|
insilmaril@656
|
247 |
|
insilmaril@656
|
248 |
// ===== 1. Find if the polygons are currently intersecting =====
|
insilmaril@656
|
249 |
|
insilmaril@656
|
250 |
// Find the axis perpendicular to the current edge
|
insilmaril@656
|
251 |
|
insilmaril@792
|
252 |
Vector axis (-edge.y(), edge.x());
|
insilmaril@792
|
253 |
axis.normalize();
|
insilmaril@656
|
254 |
|
insilmaril@656
|
255 |
// Find the projection of the polygon on the current axis
|
insilmaril@656
|
256 |
|
insilmaril@656
|
257 |
qreal minA = 0; qreal minB = 0; qreal maxA = 0; qreal maxB = 0;
|
insilmaril@789
|
258 |
projectPolygon(axis, polygonA, minA, maxA);
|
insilmaril@789
|
259 |
projectPolygon(axis, polygonB, minB, maxB);
|
insilmaril@656
|
260 |
|
insilmaril@656
|
261 |
// Check if the polygon projections are currentlty intersecting
|
insilmaril@656
|
262 |
|
insilmaril@789
|
263 |
qreal d = intervalDistance(minA, maxA, minB, maxB);
|
insilmaril@789
|
264 |
if (d > 0) result.intersect = false;
|
insilmaril@656
|
265 |
|
insilmaril@789
|
266 |
// ===== 2. Now find if the polygons *will* intersect =====
|
insilmaril@656
|
267 |
|
insilmaril@656
|
268 |
|
insilmaril@656
|
269 |
// Project the velocity on the current axis
|
insilmaril@656
|
270 |
|
insilmaril@792
|
271 |
qreal velocityProjection = axis.dotProduct(velocity);
|
insilmaril@656
|
272 |
|
insilmaril@656
|
273 |
// Get the projection of polygon A during the movement
|
insilmaril@656
|
274 |
|
insilmaril@662
|
275 |
if (velocityProjection < 0)
|
insilmaril@656
|
276 |
minA += velocityProjection;
|
insilmaril@662
|
277 |
else
|
insilmaril@656
|
278 |
maxA += velocityProjection;
|
insilmaril@656
|
279 |
|
insilmaril@656
|
280 |
// Do the same test as above for the new projection
|
insilmaril@656
|
281 |
|
insilmaril@789
|
282 |
// d = intervalDistance(minA, maxA, minB, maxB);
|
insilmaril@789
|
283 |
//if (d > 0) result.willIntersect = false;
|
insilmaril@662
|
284 |
/*
|
insilmaril@662
|
285 |
cout <<" ";
|
insilmaril@789
|
286 |
cout << "edge="<<edge<<" ";
|
insilmaril@789
|
287 |
cout <<"axis="<<axis<<" ";
|
insilmaril@789
|
288 |
cout <<"dA=("<<minA<<","<<maxA<<") dB=("<<minB<<","<<maxB<<")";
|
insilmaril@662
|
289 |
cout <<" d="<<d<<" ";
|
insilmaril@789
|
290 |
//cout <<"minD="<<minIntervalDistance<<" ";
|
insilmaril@662
|
291 |
cout <<"int="<<result.intersect<<" ";
|
insilmaril@789
|
292 |
//cout <<"wint="<<result.willIntersect<<" ";
|
insilmaril@662
|
293 |
//cout <<"velProj="<<velocityProjection<<" ";
|
insilmaril@662
|
294 |
cout <<endl;
|
insilmaril@789
|
295 |
*/
|
insilmaril@662
|
296 |
|
insilmaril@789
|
297 |
if (result.intersect )// || result.willIntersect)
|
insilmaril@662
|
298 |
{
|
insilmaril@662
|
299 |
// Check if the current interval distance is the minimum one. If so
|
insilmaril@662
|
300 |
// store the interval distance and the current distance. This will
|
insilmaril@662
|
301 |
// be used to calculate the minimum translation vector
|
insilmaril@656
|
302 |
|
insilmaril@662
|
303 |
if (d<0) d=-d;
|
insilmaril@662
|
304 |
if (d < minIntervalDistance) {
|
insilmaril@662
|
305 |
minIntervalDistance = d;
|
insilmaril@789
|
306 |
//translationAxis = axis;
|
insilmaril@789
|
307 |
//cout << "tAxix="<<translationAxis<<endl;
|
insilmaril@656
|
308 |
|
insilmaril@662
|
309 |
//QPointF t = polygonA.Center - polygonB.Center;
|
insilmaril@789
|
310 |
//QPointF t = polygonA.at(0) - polygonB.at(0);
|
insilmaril@789
|
311 |
//if (dotProduct(t,translationAxis) < 0)
|
insilmaril@789
|
312 |
// translationAxis = -translationAxis;
|
insilmaril@662
|
313 |
}
|
insilmaril@662
|
314 |
}
|
insilmaril@656
|
315 |
}
|
insilmaril@656
|
316 |
|
insilmaril@656
|
317 |
// The minimum translation vector
|
insilmaril@656
|
318 |
// can be used to push the polygons appart.
|
insilmaril@656
|
319 |
|
insilmaril@662
|
320 |
if (result.willIntersect)
|
insilmaril@662
|
321 |
result.minTranslation =
|
insilmaril@656
|
322 |
translationAxis * minIntervalDistance;
|
insilmaril@656
|
323 |
|
insilmaril@656
|
324 |
return result;
|
insilmaril@656
|
325 |
}
|
insilmaril@656
|
326 |
|
insilmaril@656
|
327 |
/* The function can be used this way:
|
insilmaril@656
|
328 |
QPointF polygonATranslation = new QPointF();
|
insilmaril@656
|
329 |
*/
|
insilmaril@656
|
330 |
|
insilmaril@656
|
331 |
|
insilmaril@656
|
332 |
/*
|
insilmaril@656
|
333 |
PolygonCollisionResult r = PolygonCollision(polygonA, polygonB, velocity);
|
insilmaril@656
|
334 |
|
insilmaril@656
|
335 |
if (r.WillIntersect)
|
insilmaril@656
|
336 |
// Move the polygon by its velocity, then move
|
insilmaril@656
|
337 |
// the polygons appart using the Minimum Translation Vector
|
insilmaril@662
|
338 |
polygonATranslation = velocity + r.minTranslation;
|
insilmaril@656
|
339 |
else
|
insilmaril@656
|
340 |
// Just move the polygon by its velocity
|
insilmaril@656
|
341 |
polygonATranslation = velocity;
|
insilmaril@656
|
342 |
|
insilmaril@656
|
343 |
polygonA.Offset(polygonATranslation);
|
insilmaril@656
|
344 |
|
insilmaril@656
|
345 |
*/
|
insilmaril@656
|
346 |
|
insilmaril@656
|
347 |
|