From cfcd70ad32e2b51a80f95cf10aa747a0f4ebef22 Mon Sep 17 00:00:00 2001 From: David Rose Date: Thu, 4 Dec 2003 18:13:33 +0000 Subject: [PATCH] be more robust with new collision poly representation --- panda/src/collide/collisionPolygon.I | 30 +++++++++++++++++++++++++- panda/src/collide/collisionPolygon.cxx | 10 ++++----- panda/src/collide/collisionPolygon.h | 1 + 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/panda/src/collide/collisionPolygon.I b/panda/src/collide/collisionPolygon.I index 1e39e5d14a..0332b0fa60 100644 --- a/panda/src/collide/collisionPolygon.I +++ b/panda/src/collide/collisionPolygon.I @@ -125,10 +125,38 @@ to_2d(const LVecBase3f &point3d) const { //////////////////////////////////////////////////////////////////// INLINE void CollisionPolygon:: 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()); } +//////////////////////////////////////////////////////////////////// +// 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 // Access: Private, Static diff --git a/panda/src/collide/collisionPolygon.cxx b/panda/src/collide/collisionPolygon.cxx index c8416a5f9c..f3c9bab1c8 100644 --- a/panda/src/collide/collisionPolygon.cxx +++ b/panda/src/collide/collisionPolygon.cxx @@ -227,7 +227,7 @@ xform(const LMatrix4f &mat) { if (!_points.empty()) { LMatrix4f to_3d_mat; - calc_to_3d_mat(to_3d_mat); + rederive_to_3d_mat(to_3d_mat); pvector verts; Points::const_iterator pi; @@ -254,7 +254,7 @@ xform(const LMatrix4f &mat) { LPoint3f CollisionPolygon:: get_collision_origin() const { LMatrix4f to_3d_mat; - calc_to_3d_mat(to_3d_mat); + rederive_to_3d_mat(to_3d_mat); LPoint2f median = _points[0]._p; for (int n = 1; n < (int)_points.size(); n++) { @@ -339,7 +339,7 @@ write(ostream &out, int indent_level) const { } LMatrix4f to_3d_mat; - calc_to_3d_mat(to_3d_mat); + rederive_to_3d_mat(to_3d_mat); out << "In 3-d space:\n"; PTA_Vertexf verts; 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 // of our vertices. LMatrix4f to_3d_mat; - calc_to_3d_mat(to_3d_mat); + rederive_to_3d_mat(to_3d_mat); pvector vertices; Points::const_iterator 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; - calc_to_3d_mat(to_3d_mat); + rederive_to_3d_mat(to_3d_mat); PTA_Vertexf verts; Points::const_iterator pi; for (pi = points.begin(); pi != points.end(); ++pi) { diff --git a/panda/src/collide/collisionPolygon.h b/panda/src/collide/collisionPolygon.h index ca698d64f0..0902315af2 100644 --- a/panda/src/collide/collisionPolygon.h +++ b/panda/src/collide/collisionPolygon.h @@ -100,6 +100,7 @@ private: void setup_points(const LPoint3f *begin, const LPoint3f *end); INLINE LPoint2f to_2d(const LVecBase3f &point3d) 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); LPoint3f legacy_to_3d(const LVecBase2f &point2d, int axis) const;