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 qreal distance (const QPointF &p, const QPointF &q)
53 return sqrt (p.x()*q.x() + p.y()*q.y());
56 Vector::Vector ():QPointF ()
60 Vector::Vector (const QPointF &p):QPointF (p)
64 Vector::Vector (qreal x, qreal y):QPointF (x,y)
68 //! Check if length is 0
77 void Vector::normalize ()
79 if (isNull() ) return;
80 qreal l=sqrt ( x()*x() + y()*y() );
85 //! Dot product of two vectors
86 qreal Vector::dotProduct (const QPointF &b)
88 return x()*b.x() + y()*b.y();
92 void Vector::scale (const qreal &f)
98 void Vector::invert ()
104 QPointF Vector::toQPointF ()
106 return QPointF (x(),y());
109 /*! Calculate the projection of a polygon on an axis
110 and returns it as a [min, max] interval */
111 ConvexPolygon::ConvexPolygon ()
115 ConvexPolygon::ConvexPolygon (QPolygonF p):QPolygonF (p)
119 void ConvexPolygon::calcCentroid()
121 // Calculate area and centroid
122 // http://en.wikipedia.org/wiki/Centroid
128 for (int i=0;i<size()-1;i++)
130 p=at(i).x() * at(i+1).y() - at(i+1).x() * at(i).y();
132 cx+=(at(i).x()+at(i+1).x()) * p;
133 cy+=(at(i).y()+at(i+1).y()) * p;
136 // area is negative if vertices ordered counterclockwise
137 // (in mirrored graphicsview!)
140 _centroid.setX (cx/p);
141 _centroid.setY (cy/p);
144 QPointF ConvexPolygon::centroid() const
149 qreal ConvexPolygon::weight() const
154 std::string ConvexPolygon::toStdString()
157 for (int i=0;i<size();++i)
159 s+=QString("(%1,%2)").arg(at(i).x()).arg(at(i).y());
160 if (i<size()-1) s+=",";
163 return s.toStdString();
166 Vector ConvexPolygon::at(const int &i) const
168 return Vector (QPolygonF::at(i).x(),QPolygonF::at(i).y());
171 void ConvexPolygon::translate ( const Vector & offset )
172 { translate (offset.x(),offset.y());}
174 void ConvexPolygon::translate ( qreal dx, qreal dy )
176 QPolygonF::translate (dx,dy);
177 _centroid=_centroid+QPointF (dx,dy);
180 void projectPolygon(Vector axis, ConvexPolygon polygon, qreal &min, qreal &max)
182 // To project a point on an axis use the dot product
184 //cout << "Projecting on "<< axis<<endl;
185 qreal d = axis.dotProduct(polygon.at(0));
188 for (int i = 0; i < polygon.size(); i++)
190 d= polygon.at(i).dotProduct (axis);
195 // cout << "p="<<polygon.at(i)<<" d="<<d<<" (min, max)=("<<min<<","<<max<<")\n";
199 // Calculate the signed distance between [minA, maxA] and [minB, maxB]
200 // The distance will be negative if the intervals overlap
202 qreal intervalDistance(qreal minA, qreal maxA, qreal minB, qreal maxB) {
211 Check if polygon A is going to collide with polygon B.
212 The last parameter is the *relative* velocity
213 of the polygons (i.e. velocityA - velocityB)
216 PolygonCollisionResult polygonCollision(ConvexPolygon polygonA,
217 ConvexPolygon polygonB, Vector velocity)
219 PolygonCollisionResult result;
220 result.intersect = true;
221 result.willIntersect = true;
223 int edgeCountA = polygonA.size();
224 int edgeCountB = polygonB.size();
225 qreal minIntervalDistance = 1000000000;
226 QPointF translationAxis;
231 for (int k=0; k<edgeCountA;k++)
232 cout <<polygonA.at(k);
234 for (int k=0; k<edgeCountB;k++)
235 cout <<polygonB.at(k);
239 // Loop through all the edges of both polygons
240 for (int i=0;i<edgeCountA + edgeCountB;i++)
244 // Loop through polygon A
247 polygonA.at(i+1).x()-polygonA.at(i).x(),
248 polygonA.at(i+1).y()-polygonA.at(i).y());
251 polygonA.at(0).x()-polygonA.at(i).x(),
252 polygonA.at(0).y()-polygonA.at(i).y());
255 // Loop through polygon B
256 if (i < edgeCountA +edgeCountB -1 )
258 polygonB.at(i-edgeCountA+1).x() - polygonB.at(i-edgeCountA).x(),
259 polygonB.at(i-edgeCountA+1).y() - polygonB.at(i-edgeCountA).y());
262 polygonB.at(0).x() - polygonB.at(i-edgeCountA).x(),
263 polygonB.at(0).y() - polygonB.at(i-edgeCountA).y());
266 // ===== 1. Find if the polygons are currently intersecting =====
268 // Find the axis perpendicular to the current edge
270 Vector axis (-edge.y(), edge.x());
273 // Find the projection of the polygon on the current axis
275 qreal minA = 0; qreal minB = 0; qreal maxA = 0; qreal maxB = 0;
276 projectPolygon(axis, polygonA, minA, maxA);
277 projectPolygon(axis, polygonB, minB, maxB);
279 // Check if the polygon projections are currentlty intersecting
281 qreal d = intervalDistance(minA, maxA, minB, maxB);
282 if (d > 0) result.intersect = false;
284 // ===== 2. Now find if the polygons *will* intersect =====
287 // Project the velocity on the current axis
289 qreal velocityProjection = axis.dotProduct(velocity);
291 // Get the projection of polygon A during the movement
293 if (velocityProjection < 0)
294 minA += velocityProjection;
296 maxA += velocityProjection;
298 // Do the same test as above for the new projection
300 // d = intervalDistance(minA, maxA, minB, maxB);
301 //if (d > 0) result.willIntersect = false;
304 cout << "edge="<<edge<<" ";
305 cout <<"axis="<<axis<<" ";
306 cout <<"dA=("<<minA<<","<<maxA<<") dB=("<<minB<<","<<maxB<<")";
307 cout <<" d="<<d<<" ";
308 //cout <<"minD="<<minIntervalDistance<<" ";
309 cout <<"int="<<result.intersect<<" ";
310 //cout <<"wint="<<result.willIntersect<<" ";
311 //cout <<"velProj="<<velocityProjection<<" ";
315 if (result.intersect )// || result.willIntersect)
317 // Check if the current interval distance is the minimum one. If so
318 // store the interval distance and the current distance. This will
319 // be used to calculate the minimum translation vector
322 if (d < minIntervalDistance) {
323 minIntervalDistance = d;
324 //translationAxis = axis;
325 //cout << "tAxix="<<translationAxis<<endl;
327 //QPointF t = polygonA.Center - polygonB.Center;
328 //QPointF t = polygonA.at(0) - polygonB.at(0);
329 //if (dotProduct(t,translationAxis) < 0)
330 // translationAxis = -translationAxis;
335 // The minimum translation vector
336 // can be used to push the polygons appart.
338 if (result.willIntersect)
339 result.minTranslation =
340 translationAxis * minIntervalDistance;
345 /* The function can be used this way:
346 QPointF polygonATranslation = new QPointF();
351 PolygonCollisionResult r = PolygonCollision(polygonA, polygonB, velocity);
354 // Move the polygon by its velocity, then move
355 // the polygons appart using the Minimum Translation Vector
356 polygonATranslation = velocity + r.minTranslation;
358 // Just move the polygon by its velocity
359 polygonATranslation = velocity;
361 polygonA.Offset(polygonATranslation);