00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00039
00040
00041
00042 CollisionSolid *CollisionSphere::
00043 make_copy() {
00044 return new CollisionSphere(*this);
00045 }
00046
00047
00048
00049
00050
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
00059
00060
00061
00062 void CollisionSphere::
00063 xform(const LMatrix4f &mat) {
00064 _center = _center * mat;
00065
00066
00067
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
00077
00078
00079
00080
00081
00082
00083 LPoint3f CollisionSphere::
00084 get_collision_origin() const {
00085 return get_center();
00086 }
00087
00088
00089
00090
00091
00092
00093 void CollisionSphere::
00094 output(ostream &out) const {
00095 out << "csphere, c (" << _center << "), r " << _radius;
00096 }
00097
00098
00099
00100
00101
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
00116
00117
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
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
00156
00157
00158
00159 new_entry->set_from_depth(into_depth);
00160
00161 return new_entry;
00162 }
00163
00164
00165
00166
00167
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
00180 return NULL;
00181 }
00182
00183 if (t2 < 0.0) {
00184
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
00208
00209
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
00223 return NULL;
00224 }
00225
00226 if (t2 < 0.0 || t1 > 1.0) {
00227
00228
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
00242
00243 into_intersection_point = from_a;
00244 } else {
00245
00246
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
00256
00257
00258
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
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290 bool CollisionSphere::
00291 intersects_line(double &t1, double &t2,
00292 const LPoint3f &from, const LVector3f &delta) const {
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
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
00336 t1 = t2 = -B /(2.0*A);
00337 return true;
00338
00339 } else if (radical < 0.0) {
00340
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
00354
00355
00356
00357 void CollisionSphere::
00358 register_with_read_factory() {
00359 BamReader::get_factory()->register_factory(get_class_type(), make_CollisionSphere);
00360 }
00361
00362
00363
00364
00365
00366
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
00377
00378
00379
00380 TypedWritable *CollisionSphere::
00381 make_CollisionSphere(const FactoryParams ¶ms) {
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
00393
00394
00395
00396
00397
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 }