Przeglądaj źródła

be more robust with new collision poly representation

David Rose 22 lat temu
rodzic
commit
cfcd70ad32

+ 29 - 1
panda/src/collide/collisionPolygon.I

@@ -125,10 +125,38 @@ to_2d(const LVecBase3f &point3d) const {
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 INLINE void CollisionPolygon::
 INLINE void CollisionPolygon::
 calc_to_3d_mat(LMatrix4f &to_3d_mat) const {
 calc_to_3d_mat(LMatrix4f &to_3d_mat) const {
-  look_at(to_3d_mat, get_plane().get_normal());
+  // We have to be explicit about the coordinate system--we
+  // specifically mean CS_zup_right, because that points the forward
+  // vector down the Y axis and moves the coords inthe (X, 0, Z).  We
+  // want this effect regardless of the user's coordinate system of
+  // choice.
+
+  // The up vector, on the other hand, is completely arbitrary.
+
+  look_at(to_3d_mat, get_plane().get_normal(), 
+          LVector3f(0.0f, 0.0f, 1.0f), CS_zup_right);
   to_3d_mat.set_row(3, get_plane().get_point());
   to_3d_mat.set_row(3, get_plane().get_point());
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: CollisionPolygon::rederive_to_3d_mat
+//       Access: Private
+//  Description: Fills the indicated matrix with the appropriate
+//               rotation transform to move points from the 2-d plane
+//               into the 3-d (X, 0, Z) plane.
+//
+//               This is essentially similar to calc_to_3d_mat, except
+//               that the matrix is rederived from whatever is stored
+//               in _to_2d_mat, guaranteeing that it will match
+//               whatever algorithm produced that one, even if it was
+//               produced on a different machine with different
+//               numerical precision.
+////////////////////////////////////////////////////////////////////
+INLINE void CollisionPolygon::
+rederive_to_3d_mat(LMatrix4f &to_3d_mat) const {
+  to_3d_mat.invert_from(_to_2d_mat);
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: CollisionPolygon::to_3d
 //     Function: CollisionPolygon::to_3d
 //       Access: Private, Static
 //       Access: Private, Static

+ 5 - 5
panda/src/collide/collisionPolygon.cxx

@@ -227,7 +227,7 @@ xform(const LMatrix4f &mat) {
 
 
   if (!_points.empty()) {
   if (!_points.empty()) {
     LMatrix4f to_3d_mat;
     LMatrix4f to_3d_mat;
-    calc_to_3d_mat(to_3d_mat);
+    rederive_to_3d_mat(to_3d_mat);
 
 
     pvector<LPoint3f> verts;
     pvector<LPoint3f> verts;
     Points::const_iterator pi;
     Points::const_iterator pi;
@@ -254,7 +254,7 @@ xform(const LMatrix4f &mat) {
 LPoint3f CollisionPolygon::
 LPoint3f CollisionPolygon::
 get_collision_origin() const {
 get_collision_origin() const {
   LMatrix4f to_3d_mat;
   LMatrix4f to_3d_mat;
-  calc_to_3d_mat(to_3d_mat);
+  rederive_to_3d_mat(to_3d_mat);
 
 
   LPoint2f median = _points[0]._p;
   LPoint2f median = _points[0]._p;
   for (int n = 1; n < (int)_points.size(); n++) {
   for (int n = 1; n < (int)_points.size(); n++) {
@@ -339,7 +339,7 @@ write(ostream &out, int indent_level) const {
   }
   }
 
 
   LMatrix4f to_3d_mat;
   LMatrix4f to_3d_mat;
-  calc_to_3d_mat(to_3d_mat);
+  rederive_to_3d_mat(to_3d_mat);
   out << "In 3-d space:\n";
   out << "In 3-d space:\n";
   PTA_Vertexf verts;
   PTA_Vertexf verts;
   for (pi = _points.begin(); pi != _points.end(); ++pi) {
   for (pi = _points.begin(); pi != _points.end(); ++pi) {
@@ -368,7 +368,7 @@ recompute_bound() {
   // Now actually compute the bounding volume by putting it around all
   // Now actually compute the bounding volume by putting it around all
   // of our vertices.
   // of our vertices.
   LMatrix4f to_3d_mat;
   LMatrix4f to_3d_mat;
-  calc_to_3d_mat(to_3d_mat);
+  rederive_to_3d_mat(to_3d_mat);
   pvector<LPoint3f> vertices;
   pvector<LPoint3f> vertices;
   Points::const_iterator pi;
   Points::const_iterator pi;
   for (pi = _points.begin(); pi != _points.end(); ++pi) {
   for (pi = _points.begin(); pi != _points.end(); ++pi) {
@@ -737,7 +737,7 @@ draw_polygon(GeomNode *geom_node, const CollisionPolygon::Points &points) const
   }
   }
 
 
   LMatrix4f to_3d_mat;
   LMatrix4f to_3d_mat;
-  calc_to_3d_mat(to_3d_mat);
+  rederive_to_3d_mat(to_3d_mat);
   PTA_Vertexf verts;
   PTA_Vertexf verts;
   Points::const_iterator pi;
   Points::const_iterator pi;
   for (pi = points.begin(); pi != points.end(); ++pi) {
   for (pi = points.begin(); pi != points.end(); ++pi) {

+ 1 - 0
panda/src/collide/collisionPolygon.h

@@ -100,6 +100,7 @@ private:
   void setup_points(const LPoint3f *begin, const LPoint3f *end);
   void setup_points(const LPoint3f *begin, const LPoint3f *end);
   INLINE LPoint2f to_2d(const LVecBase3f &point3d) const;
   INLINE LPoint2f to_2d(const LVecBase3f &point3d) const;
   INLINE void calc_to_3d_mat(LMatrix4f &to_3d_mat) const;
   INLINE void calc_to_3d_mat(LMatrix4f &to_3d_mat) const;
+  INLINE void rederive_to_3d_mat(LMatrix4f &to_3d_mat) const;
   INLINE static LPoint3f to_3d(const LVecBase2f &point2d, const LMatrix4f &to_3d_mat);
   INLINE static LPoint3f to_3d(const LVecBase2f &point2d, const LMatrix4f &to_3d_mat);
   LPoint3f legacy_to_3d(const LVecBase2f &point2d, int axis) const;
   LPoint3f legacy_to_3d(const LVecBase2f &point2d, int axis) const;