Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/collide/collisionSphere.cxx

Go to the documentation of this file.
00001 // Filename: collisionSphere.cxx
00002 // Created by:  drose (24Apr00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 
00020 #include "collisionSphere.h"
00021 #include "collisionRay.h"
00022 #include "collisionSegment.h"
00023 #include "collisionHandler.h"
00024 #include "collisionEntry.h"
00025 #include "config_collide.h"
00026 
00027 #include "boundingSphere.h"
00028 #include "datagram.h"
00029 #include "datagramIterator.h"
00030 #include "bamReader.h"
00031 #include "bamWriter.h"
00032 #include "geomSphere.h"
00033 #include "nearly_zero.h"
00034 
00035 TypeHandle CollisionSphere::_type_handle;
00036 
00037 ////////////////////////////////////////////////////////////////////
00038 //     Function: CollisionSphere::make_copy
00039 //       Access: Public, Virtual
00040 //  Description:
00041 ////////////////////////////////////////////////////////////////////
00042 CollisionSolid *CollisionSphere::
00043 make_copy() {
00044   return new CollisionSphere(*this);
00045 }
00046 
00047 ////////////////////////////////////////////////////////////////////
00048 //     Function: CollisionSphere::test_intersection
00049 //       Access: Public, Virtual
00050 //  Description:
00051 ////////////////////////////////////////////////////////////////////
00052 PT(CollisionEntry) CollisionSphere::
00053 test_intersection(const CollisionEntry &entry) const {
00054   return entry.get_into()->test_intersection_from_sphere(entry);
00055 }
00056 
00057 ////////////////////////////////////////////////////////////////////
00058 //     Function: CollisionSphere::xform
00059 //       Access: Public, Virtual
00060 //  Description: Transforms the solid by the indicated matrix.
00061 ////////////////////////////////////////////////////////////////////
00062 void CollisionSphere::
00063 xform(const LMatrix4f &mat) {
00064   _center = _center * mat;
00065 
00066   // This is a little cheesy and fails miserably in the presence of a
00067   // non-proportionate scale.
00068   LVector3f radius_v = LVector3f(_radius, 0.0f, 0.0f) * mat;
00069   _radius = length(radius_v);
00070 
00071   mark_viz_stale();
00072   mark_bound_stale();
00073 }
00074 
00075 ////////////////////////////////////////////////////////////////////
00076 //     Function: CollisionSphere::get_collision_origin
00077 //       Access: Public, Virtual
00078 //  Description: Returns the point in space deemed to be the "origin"
00079 //               of the solid for collision purposes.  The closest
00080 //               intersection point to this origin point is considered
00081 //               to be the most significant.
00082 ////////////////////////////////////////////////////////////////////
00083 LPoint3f CollisionSphere::
00084 get_collision_origin() const {
00085   return get_center();
00086 }
00087 
00088 ////////////////////////////////////////////////////////////////////
00089 //     Function: CollisionSphere::output
00090 //       Access: Public, Virtual
00091 //  Description:
00092 ////////////////////////////////////////////////////////////////////
00093 void CollisionSphere::
00094 output(ostream &out) const {
00095   out << "csphere, c (" << _center << "), r " << _radius;
00096 }
00097 
00098 ////////////////////////////////////////////////////////////////////
00099 //     Function: CollisionSphere::recompute_bound
00100 //       Access: Protected, Virtual
00101 //  Description:
00102 ////////////////////////////////////////////////////////////////////
00103 BoundingVolume *CollisionSphere::
00104 recompute_bound() {
00105   BoundingVolume *bound = BoundedObject::recompute_bound();
00106   nassertr(bound != (BoundingVolume*)0L, bound);
00107   nassertr(!_center.is_nan() && !cnan(_radius), bound);
00108   BoundingSphere sphere(_center, _radius);
00109   bound->extend_by(&sphere);
00110 
00111   return bound;
00112 }
00113 
00114 ////////////////////////////////////////////////////////////////////
00115 //     Function: CollisionSphere::test_intersection_from_sphere
00116 //       Access: Public, Virtual
00117 //  Description:
00118 ////////////////////////////////////////////////////////////////////
00119 PT(CollisionEntry) CollisionSphere::
00120 test_intersection_from_sphere(const CollisionEntry &entry) const {
00121   const CollisionSphere *sphere;
00122   DCAST_INTO_R(sphere, entry.get_from(), 0);
00123 
00124   LPoint3f from_center = sphere->get_center() * entry.get_wrt_space();
00125   LVector3f from_radius_v =
00126     LVector3f(sphere->get_radius(), 0.0f, 0.0f) * entry.get_wrt_space();
00127   float from_radius = length(from_radius_v);
00128 
00129   LPoint3f into_center = _center;
00130   float into_radius = _radius;
00131 
00132   LVector3f vec = from_center - into_center;
00133   float dist2 = dot(vec, vec);
00134   if (dist2 > (into_radius + from_radius) * (into_radius + from_radius)) {
00135     // No intersection.
00136     return NULL;
00137   }
00138 
00139   if (collide_cat.is_debug()) {
00140     collide_cat.debug()
00141       << "intersection detected from " << *entry.get_from_node() << " into "
00142       << entry.get_into_node_path() << "\n";
00143   }
00144   PT(CollisionEntry) new_entry = new CollisionEntry(entry);
00145 
00146   float dist = sqrtf(dist2);
00147   LVector3f into_normal = normalize(vec);
00148   LPoint3f into_intersection_point = into_normal * (dist - from_radius);
00149   float into_depth = into_radius + from_radius - dist;
00150 
00151   new_entry->set_into_surface_normal(into_normal);
00152   new_entry->set_into_intersection_point(into_intersection_point);
00153   new_entry->set_into_depth(into_depth);
00154 
00155   // This is a bit of a hack.  This result is incorrect if there is a
00156   // scale between the colliding object and this sphere.  We need to
00157   // account for that scale, but to account for it correctly
00158   // (especially including non-uniform scales) is rather complicated.
00159   new_entry->set_from_depth(into_depth);
00160 
00161   return new_entry;
00162 }
00163 
00164 ////////////////////////////////////////////////////////////////////
00165 //     Function: CollisionSphere::test_intersection_from_ray
00166 //       Access: Public, Virtual
00167 //  Description:
00168 ////////////////////////////////////////////////////////////////////
00169 PT(CollisionEntry) CollisionSphere::
00170 test_intersection_from_ray(const CollisionEntry &entry) const {
00171   const CollisionRay *ray;
00172   DCAST_INTO_R(ray, entry.get_from(), 0);
00173 
00174   LPoint3f from_origin = ray->get_origin() * entry.get_wrt_space();
00175   LVector3f from_direction = ray->get_direction() * entry.get_wrt_space();
00176 
00177   double t1, t2;
00178   if (!intersects_line(t1, t2, from_origin, from_direction)) {
00179     // No intersection.
00180     return NULL;
00181   }
00182 
00183   if (t2 < 0.0) {
00184     // Both intersection points are before the start of the ray.
00185     return NULL;
00186   }
00187 
00188   if (collide_cat.is_debug()) {
00189     collide_cat.debug()
00190       << "intersection detected from " << *entry.get_from_node() << " into "
00191       << entry.get_into_node_path() << "\n";
00192   }
00193   PT(CollisionEntry) new_entry = new CollisionEntry(entry);
00194 
00195   LPoint3f into_intersection_point;
00196   if (t1 < 0.0) {
00197     into_intersection_point = from_origin + t2 * from_direction;
00198   } else {
00199     into_intersection_point = from_origin + t1 * from_direction;
00200   }
00201   new_entry->set_into_intersection_point(into_intersection_point);
00202 
00203   return new_entry;
00204 }
00205 
00206 ////////////////////////////////////////////////////////////////////
00207 //     Function: CollisionSphere::test_intersection_from_segment
00208 //       Access: Public, Virtual
00209 //  Description:
00210 ////////////////////////////////////////////////////////////////////
00211 PT(CollisionEntry) CollisionSphere::
00212 test_intersection_from_segment(const CollisionEntry &entry) const {
00213   const CollisionSegment *segment;
00214   DCAST_INTO_R(segment, entry.get_from(), 0);
00215 
00216   LPoint3f from_a = segment->get_point_a() * entry.get_wrt_space();
00217   LPoint3f from_b = segment->get_point_b() * entry.get_wrt_space();
00218   LVector3f from_direction = from_b - from_a;
00219 
00220   double t1, t2;
00221   if (!intersects_line(t1, t2, from_a, from_direction)) {
00222     // No intersection.
00223     return NULL;
00224   }
00225 
00226   if (t2 < 0.0 || t1 > 1.0) {
00227     // Both intersection points are before the start of the segment or
00228     // after the end of the segment.
00229     return NULL;
00230   }
00231 
00232   if (collide_cat.is_debug()) {
00233     collide_cat.debug()
00234       << "intersection detected from " << *entry.get_from_node() << " into "
00235       << entry.get_into_node_path() << "\n";
00236   }
00237   PT(CollisionEntry) new_entry = new CollisionEntry(entry);
00238 
00239   LPoint3f into_intersection_point;
00240   if (t1 < 0.0) {
00241     // Point a is within the sphere.  The first intersection point is
00242     // point a itself.
00243     into_intersection_point = from_a;
00244   } else {
00245     // Point a is outside the sphere, and point b is either inside the
00246     // sphere or beyond it.  The first intersection point is at t1.
00247     into_intersection_point = from_a + t1 * from_direction;
00248   }
00249   new_entry->set_into_intersection_point(into_intersection_point);
00250 
00251   return new_entry;
00252 }
00253 
00254 ////////////////////////////////////////////////////////////////////
00255 //     Function: CollisionSphere::fill_viz_geom
00256 //       Access: Protected, Virtual
00257 //  Description: Fills the _viz_geom GeomNode up with Geoms suitable
00258 //               for rendering this solid.
00259 ////////////////////////////////////////////////////////////////////
00260 void CollisionSphere::
00261 fill_viz_geom() {
00262   if (collide_cat.is_debug()) {
00263     collide_cat.debug()
00264       << "Recomputing viz for " << *this << "\n";
00265   }
00266 
00267   GeomSphere *sphere = new GeomSphere;
00268   PTA_Vertexf verts;
00269   verts.push_back(_center);
00270   verts.push_back(_center + LVector3f(_radius, 0.0f, 0.0f));
00271   sphere->set_coords(verts);
00272   sphere->set_num_prims(1);
00273 
00274   _viz_geom->add_geom(sphere, get_solid_viz_state());
00275 }
00276 
00277 ////////////////////////////////////////////////////////////////////
00278 //     Function: CollisionSphere::intersects_line
00279 //       Access: Protected
00280 //  Description: Determine the point(s) of intersect of a parametric
00281 //               line with the sphere.  The line is infinite in both
00282 //               directions, and passes through "from" and from+delta.
00283 //               If the line does not intersect the sphere, the
00284 //               function returns false, and t1 and t2 are undefined.
00285 //               If it does intersect the sphere, it returns true, and
00286 //               t1 and t2 are set to the points along the equation
00287 //               from+t*delta that correspond to the two points of
00288 //               intersection.
00289 ////////////////////////////////////////////////////////////////////
00290 bool CollisionSphere::
00291 intersects_line(double &t1, double &t2,
00292                 const LPoint3f &from, const LVector3f &delta) const {
00293   // Solve the equation for the intersection of a line with a sphere
00294   // using the quadratic equation.
00295 
00296   // A line segment from f to f+d is defined as all P such that
00297   // P = f + td for 0 <= t <= 1.
00298 
00299   // A sphere with radius r about point c is defined as all P such
00300   // that r^2 = (P - c)^2.
00301 
00302   // Subsituting P in the above we have:
00303 
00304   // r^2 = (f + td - c)^2 =
00305   // (f^2 + ftd - fc + ftd + t^2d^2 - tdc - fc - tdc + c^2) =
00306   // t^2(d^2) + t(fd + fd - dc - dc) + (f^2 - fc - fc + c^2) =
00307   // t^2(d^2) + t(2d(f - c)) + (f - c)^2
00308 
00309   // Thus, the equation is quadratic in t, and we have
00310   // at^2 + bt + c = 0
00311 
00312   // Where  a = d^2
00313   //        b = 2d(f - c)
00314   //        c = (f - c)^2 - r^2
00315 
00316   // Solving for t using the quadratic equation gives us the point of
00317   // intersection along the line segment.  Actually, there are two
00318   // solutions (since it is quadratic): one for the front of the
00319   // sphere, and one for the back.  In the case where the line is
00320   // tangent to the sphere, there is only one solution (and the
00321   // radical is zero).
00322 
00323   double A = dot(delta, delta);
00324 
00325   nassertr(A != 0.0, false);
00326 
00327   LVector3f fc = from - _center;
00328   double B = 2.0f* dot(delta, fc);
00329   double fc_d2 = dot(fc, fc);
00330   double C = fc_d2 - _radius * _radius;
00331 
00332   double radical = B*B - 4.0*A*C;
00333 
00334   if (IS_NEARLY_ZERO(radical)) {
00335     // Tangent.
00336     t1 = t2 = -B /(2.0*A);
00337     return true;
00338 
00339   } else if (radical < 0.0) {
00340     // No real roots: no intersection with the line.
00341     return false;
00342   }
00343 
00344   double reciprocal_2A = 1.0/(2.0*A);
00345   double sqrt_radical = sqrtf(radical);
00346   t1 = ( -B - sqrt_radical ) * reciprocal_2A;
00347   t2 = ( -B + sqrt_radical ) * reciprocal_2A;
00348 
00349   return true;
00350 }
00351 
00352 ////////////////////////////////////////////////////////////////////
00353 //     Function: CollisionSphere::register_with_read_factory
00354 //       Access: Public, Static
00355 //  Description: Factory method to generate a CollisionSphere object
00356 ////////////////////////////////////////////////////////////////////
00357 void CollisionSphere::
00358 register_with_read_factory() {
00359   BamReader::get_factory()->register_factory(get_class_type(), make_CollisionSphere);
00360 }
00361 
00362 ////////////////////////////////////////////////////////////////////
00363 //     Function: CollisionSphere::write_datagram
00364 //       Access: Public
00365 //  Description: Function to write the important information in
00366 //               the particular object to a Datagram
00367 ////////////////////////////////////////////////////////////////////
00368 void CollisionSphere::
00369 write_datagram(BamWriter *manager, Datagram &me) {
00370   CollisionSolid::write_datagram(manager, me);
00371   _center.write_datagram(me);
00372   me.add_float32(_radius);
00373 }
00374 
00375 ////////////////////////////////////////////////////////////////////
00376 //     Function: CollisionSphere::make_CollisionSphere
00377 //       Access: Protected
00378 //  Description: Factory method to generate a CollisionSphere object
00379 ////////////////////////////////////////////////////////////////////
00380 TypedWritable *CollisionSphere::
00381 make_CollisionSphere(const FactoryParams &params) {
00382   CollisionSphere *me = new CollisionSphere;
00383   DatagramIterator scan;
00384   BamReader *manager;
00385 
00386   parse_params(params, scan, manager);
00387   me->fillin(scan, manager);
00388   return me;
00389 }
00390 
00391 ////////////////////////////////////////////////////////////////////
00392 //     Function: CollisionSphere::fillin
00393 //       Access: Protected
00394 //  Description: Function that reads out of the datagram (or asks
00395 //               manager to read) all of the data that is needed to
00396 //               re-create this object and stores it in the appropiate
00397 //               place
00398 ////////////////////////////////////////////////////////////////////
00399 void CollisionSphere::
00400 fillin(DatagramIterator& scan, BamReader* manager) {
00401   CollisionSolid::fillin(scan, manager);
00402   _center.read_datagram(scan);
00403   _radius = scan.get_float32();
00404 }

Generated on Fri May 2 00:35:45 2003 for Panda by doxygen1.3