12 QRectF addBBox(QRectF r1, QRectF r2)
14 // Find smallest QRectF containing given rectangles
18 if (r1.left() <= r2.left() )
19 n.setLeft(r1.left() );
21 n.setLeft(r2.left() );
24 if (r1.top() <= r2.top() )
30 if (r1.right() <= r2.right() )
31 n.setRight(r2.right() );
33 n.setRight(r1.right() );
36 if (r1.bottom() <= r2.bottom() )
37 n.setBottom(r2.bottom() );
39 n.setBottom(r1.bottom() );
43 bool isInBox(const QPointF &p, const QRectF &box)
45 if (p.x() >= box.left() && p.x() <= box.right()
46 && p.y() <= box.bottom() && p.y() >= box.top() )
51 Vector::Vector ():QPointF ()
55 Vector::Vector (const QPointF &p):QPointF (p)
59 Vector::Vector (qreal x, qreal y):QPointF (x,y)
64 void Vector::normalize ()
66 if (x()==0 && y()==0) return;
67 qreal l=sqrt ( x()*x() + y()*y() );
72 //! Dot product of two vectors
73 qreal Vector::dotProduct (const QPointF &b)
75 return x()*b.x() + y()*b.y();
79 void Vector::scale (const qreal &f)
85 void Vector::invert ()
91 /*! Calculate the projection of a polygon on an axis
92 and returns it as a [min, max] interval */
93 ConvexPolygon::ConvexPolygon ()
97 ConvexPolygon::ConvexPolygon (QPolygonF p):QPolygonF (p)
101 void ConvexPolygon::calcCentroid()
103 // Calculate area and centroid
104 // http://en.wikipedia.org/wiki/Centroid
110 for (int i=0;i<size()-1;i++)
112 p=at(i).x() * at(i+1).y() - at(i+1).x() * at(i).y();
114 cx+=(at(i).x()+at(i+1).x()) * p;
115 cy+=(at(i).y()+at(i+1).y()) * p;
118 // area is negative if vertices ordered counterclockwise
119 // (in mirrored graphicsview!)
122 _centroid.setX (cx/p);
123 _centroid.setY (cy/p);
126 QPointF ConvexPolygon::centroid() const
131 qreal ConvexPolygon::weight() const
136 std::string ConvexPolygon::toStdString()
139 for (int i=0;i<size();++i)
141 s+=QString("(%1,%2)").arg(at(i).x()).arg(at(i).y());
142 if (i<size()-1) s+=",";
145 return s.toStdString();
148 Vector ConvexPolygon::at(const int &i) const
150 return Vector (QPolygonF::at(i).x(),QPolygonF::at(i).y());
153 void ConvexPolygon::translate ( const Vector & offset )
154 { translate (offset.x(),offset.y());}
156 void ConvexPolygon::translate ( qreal dx, qreal dy )
158 QPolygonF::translate (dx,dy);
159 _centroid=_centroid+QPointF (dx,dy);
162 void projectPolygon(Vector axis, ConvexPolygon polygon, qreal &min, qreal &max)
164 // To project a point on an axis use the dot product
166 //cout << "Projecting on "<< axis<<endl;
167 qreal d = axis.dotProduct(polygon.at(0));
170 for (int i = 0; i < polygon.size(); i++)
172 d= polygon.at(i).dotProduct (axis);
177 // cout << "p="<<polygon.at(i)<<" d="<<d<<" (min, max)=("<<min<<","<<max<<")\n";
181 // Calculate the signed distance between [minA, maxA] and [minB, maxB]
182 // The distance will be negative if the intervals overlap
184 qreal intervalDistance(qreal minA, qreal maxA, qreal minB, qreal maxB) {
193 Check if polygon A is going to collide with polygon B.
194 The last parameter is the *relative* velocity
195 of the polygons (i.e. velocityA - velocityB)
198 PolygonCollisionResult polygonCollision(ConvexPolygon polygonA,
199 ConvexPolygon polygonB, Vector velocity)
201 PolygonCollisionResult result;
202 result.intersect = true;
203 result.willIntersect = true;
205 int edgeCountA = polygonA.size();
206 int edgeCountB = polygonB.size();
207 qreal minIntervalDistance = 1000000000;
208 QPointF translationAxis;
213 for (int k=0; k<edgeCountA;k++)
214 cout <<polygonA.at(k);
216 for (int k=0; k<edgeCountB;k++)
217 cout <<polygonB.at(k);
221 // Loop through all the edges of both polygons
222 for (int i=0;i<edgeCountA + edgeCountB;i++)
226 // Loop through polygon A
229 polygonA.at(i+1).x()-polygonA.at(i).x(),
230 polygonA.at(i+1).y()-polygonA.at(i).y());
233 polygonA.at(0).x()-polygonA.at(i).x(),
234 polygonA.at(0).y()-polygonA.at(i).y());
237 // Loop through polygon B
238 if (i < edgeCountA +edgeCountB -1 )
240 polygonB.at(i-edgeCountA+1).x() - polygonB.at(i-edgeCountA).x(),
241 polygonB.at(i-edgeCountA+1).y() - polygonB.at(i-edgeCountA).y());
244 polygonB.at(0).x() - polygonB.at(i-edgeCountA).x(),
245 polygonB.at(0).y() - polygonB.at(i-edgeCountA).y());
248 // ===== 1. Find if the polygons are currently intersecting =====
250 // Find the axis perpendicular to the current edge
252 Vector axis (-edge.y(), edge.x());
255 // Find the projection of the polygon on the current axis
257 qreal minA = 0; qreal minB = 0; qreal maxA = 0; qreal maxB = 0;
258 projectPolygon(axis, polygonA, minA, maxA);
259 projectPolygon(axis, polygonB, minB, maxB);
261 // Check if the polygon projections are currentlty intersecting
263 qreal d = intervalDistance(minA, maxA, minB, maxB);
264 if (d > 0) result.intersect = false;
266 // ===== 2. Now find if the polygons *will* intersect =====
269 // Project the velocity on the current axis
271 qreal velocityProjection = axis.dotProduct(velocity);
273 // Get the projection of polygon A during the movement
275 if (velocityProjection < 0)
276 minA += velocityProjection;
278 maxA += velocityProjection;
280 // Do the same test as above for the new projection
282 // d = intervalDistance(minA, maxA, minB, maxB);
283 //if (d > 0) result.willIntersect = false;
286 cout << "edge="<<edge<<" ";
287 cout <<"axis="<<axis<<" ";
288 cout <<"dA=("<<minA<<","<<maxA<<") dB=("<<minB<<","<<maxB<<")";
289 cout <<" d="<<d<<" ";
290 //cout <<"minD="<<minIntervalDistance<<" ";
291 cout <<"int="<<result.intersect<<" ";
292 //cout <<"wint="<<result.willIntersect<<" ";
293 //cout <<"velProj="<<velocityProjection<<" ";
297 if (result.intersect )// || result.willIntersect)
299 // Check if the current interval distance is the minimum one. If so
300 // store the interval distance and the current distance. This will
301 // be used to calculate the minimum translation vector
304 if (d < minIntervalDistance) {
305 minIntervalDistance = d;
306 //translationAxis = axis;
307 //cout << "tAxix="<<translationAxis<<endl;
309 //QPointF t = polygonA.Center - polygonB.Center;
310 //QPointF t = polygonA.at(0) - polygonB.at(0);
311 //if (dotProduct(t,translationAxis) < 0)
312 // translationAxis = -translationAxis;
317 // The minimum translation vector
318 // can be used to push the polygons appart.
320 if (result.willIntersect)
321 result.minTranslation =
322 translationAxis * minIntervalDistance;
327 /* The function can be used this way:
328 QPointF polygonATranslation = new QPointF();
333 PolygonCollisionResult r = PolygonCollision(polygonA, polygonB, velocity);
336 // Move the polygon by its velocity, then move
337 // the polygons appart using the Minimum Translation Vector
338 polygonATranslation = velocity + r.minTranslation;
340 // Just move the polygon by its velocity
341 polygonATranslation = velocity;
343 polygonA.Offset(polygonATranslation);