From 9d8f7fe83445cd8f802b7a6f270859a72d2f4d4b Mon Sep 17 00:00:00 2001 From: David Rose Date: Wed, 5 Jan 2005 19:48:08 +0000 Subject: [PATCH] add CollisionInvSphere --- panda/src/collide/Sources.pp | 3 + panda/src/collide/collide_composite1.cxx | 2 +- panda/src/collide/collide_composite2.cxx | 1 + panda/src/collide/collisionInvSphere.I | 61 ++++ panda/src/collide/collisionInvSphere.cxx | 385 +++++++++++++++++++++++ panda/src/collide/collisionInvSphere.h | 96 ++++++ panda/src/collide/collisionSphere.I | 2 +- panda/src/collide/collisionSphere.cxx | 100 +++--- panda/src/collide/collisionSphere.h | 5 +- panda/src/collide/config_collide.cxx | 5 +- 10 files changed, 618 insertions(+), 42 deletions(-) create mode 100644 panda/src/collide/collisionInvSphere.I create mode 100644 panda/src/collide/collisionInvSphere.cxx create mode 100644 panda/src/collide/collisionInvSphere.h diff --git a/panda/src/collide/Sources.pp b/panda/src/collide/Sources.pp index a25489470c..e01948e57b 100644 --- a/panda/src/collide/Sources.pp +++ b/panda/src/collide/Sources.pp @@ -18,6 +18,7 @@ collisionHandlerPhysical.I collisionHandlerPhysical.h \ collisionHandlerPusher.I collisionHandlerPusher.h \ collisionHandlerQueue.h \ + collisionInvSphere.I collisionInvSphere.h \ collisionLine.I collisionLine.h \ collisionLevelState.I collisionLevelState.h \ collisionNode.I collisionNode.h \ @@ -43,6 +44,7 @@ collisionHandlerPusher.cxx \ collisionHandlerQueue.cxx \ collisionLevelState.cxx \ + collisionInvSphere.cxx \ collisionLine.cxx \ collisionNode.cxx \ collisionPlane.cxx \ @@ -66,6 +68,7 @@ collisionHandlerPhysical.I collisionHandlerPhysical.h \ collisionHandlerPusher.I collisionHandlerPusher.h \ collisionHandlerQueue.h \ + collisionInvSphere.I collisionInvSphere.h \ collisionLevelState.I collisionLevelState.h \ collisionLine.I collisionLine.h \ collisionNode.I collisionNode.h \ diff --git a/panda/src/collide/collide_composite1.cxx b/panda/src/collide/collide_composite1.cxx index fd99c24d78..9eccde38f3 100644 --- a/panda/src/collide/collide_composite1.cxx +++ b/panda/src/collide/collide_composite1.cxx @@ -7,7 +7,7 @@ #include "collisionHandlerPhysical.cxx" #include "collisionHandlerPusher.cxx" #include "collisionHandlerQueue.cxx" +#include "collisionInvSphere.cxx" #include "collisionLevelState.cxx" #include "collisionLine.cxx" -#include "collisionNode.cxx" diff --git a/panda/src/collide/collide_composite2.cxx b/panda/src/collide/collide_composite2.cxx index 36de6589ce..1abb72b904 100644 --- a/panda/src/collide/collide_composite2.cxx +++ b/panda/src/collide/collide_composite2.cxx @@ -1,3 +1,4 @@ +#include "collisionNode.cxx" #include "collisionPlane.cxx" #include "collisionPolygon.cxx" #include "collisionRay.cxx" diff --git a/panda/src/collide/collisionInvSphere.I b/panda/src/collide/collisionInvSphere.I new file mode 100644 index 0000000000..10083c7d5a --- /dev/null +++ b/panda/src/collide/collisionInvSphere.I @@ -0,0 +1,61 @@ +// Filename: collisionInvSphere.I +// Created by: drose (05Jan05) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: CollisionInvSphere::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE CollisionInvSphere:: +CollisionInvSphere(const LPoint3f ¢er, float radius) : + CollisionSphere(center, radius) +{ +} + +//////////////////////////////////////////////////////////////////// +// Function: CollisionInvSphere::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE CollisionInvSphere:: +CollisionInvSphere(float cx, float cy, float cz, float radius) : + CollisionSphere(cx, cy, cz, radius) +{ +} + +//////////////////////////////////////////////////////////////////// +// Function: CollisionInvSphere::Default constructor +// Access: Protected +// Description: Creates an invalid sphere. Only used when reading +// from a bam file. +//////////////////////////////////////////////////////////////////// +INLINE CollisionInvSphere:: +CollisionInvSphere() { +} + +//////////////////////////////////////////////////////////////////// +// Function: CollisionInvSphere::Copy Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE CollisionInvSphere:: +CollisionInvSphere(const CollisionInvSphere ©) : + CollisionSphere(copy) +{ +} diff --git a/panda/src/collide/collisionInvSphere.cxx b/panda/src/collide/collisionInvSphere.cxx new file mode 100644 index 0000000000..5c24217bff --- /dev/null +++ b/panda/src/collide/collisionInvSphere.cxx @@ -0,0 +1,385 @@ +// Filename: collisionInvSphere.cxx +// Created by: drose (05Jan05) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#include "collisionInvSphere.h" +#include "collisionSphere.h" +#include "collisionLine.h" +#include "collisionRay.h" +#include "collisionSegment.h" +#include "collisionHandler.h" +#include "collisionEntry.h" +#include "config_collide.h" + +#include "omniBoundingVolume.h" +#include "datagram.h" +#include "datagramIterator.h" +#include "bamReader.h" +#include "bamWriter.h" +#include "geomTristrip.h" +#include "nearly_zero.h" + +TypeHandle CollisionInvSphere::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: CollisionInvSphere::make_copy +// Access: Public, Virtual +// Description: +//////////////////////////////////////////////////////////////////// +CollisionSolid *CollisionInvSphere:: +make_copy() { + return new CollisionInvSphere(*this); +} + +//////////////////////////////////////////////////////////////////// +// Function: CollisionInvSphere::test_intersection +// Access: Public, Virtual +// Description: +//////////////////////////////////////////////////////////////////// +PT(CollisionEntry) CollisionInvSphere:: +test_intersection(const CollisionEntry &) const { + report_undefined_from_intersection(get_type()); + return NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function: CollisionInvSphere::output +// Access: Public, Virtual +// Description: +//////////////////////////////////////////////////////////////////// +void CollisionInvSphere:: +output(ostream &out) const { + out << "invsphere, c (" << get_center() << "), r " << get_radius(); +} + +//////////////////////////////////////////////////////////////////// +// Function: CollisionInvSphere::recompute_bound +// Access: Protected, Virtual +// Description: +//////////////////////////////////////////////////////////////////// +BoundingVolume *CollisionInvSphere:: +recompute_bound() { + BoundedObject::recompute_bound(); + // An inverse sphere always has an infinite bounding volume, since + // everything outside the sphere is solid matter. + return set_bound_ptr(new OmniBoundingVolume()); +} + +//////////////////////////////////////////////////////////////////// +// Function: CollisionInvSphere::test_intersection_from_sphere +// Access: Public, Virtual +// Description: +//////////////////////////////////////////////////////////////////// +PT(CollisionEntry) CollisionInvSphere:: +test_intersection_from_sphere(const CollisionEntry &entry) const { + const CollisionSphere *sphere; + DCAST_INTO_R(sphere, entry.get_from(), 0); + + const LMatrix4f &wrt_mat = entry.get_wrt_mat(); + + LPoint3f from_center = sphere->get_center() * wrt_mat; + LVector3f from_radius_v = + LVector3f(sphere->get_radius(), 0.0f, 0.0f) * wrt_mat; + float from_radius = length(from_radius_v); + + LPoint3f into_center = get_center(); + float into_radius = get_radius(); + + LVector3f vec = from_center - into_center; + float dist2 = dot(vec, vec); + if (dist2 < (into_radius - from_radius) * (into_radius - from_radius)) { + // No intersection--the sphere is within the hollow. + return NULL; + } + + if (collide_cat.is_debug()) { + collide_cat.debug() + << "intersection detected from " << entry.get_from_node_path() + << " into " << entry.get_into_node_path() << "\n"; + } + PT(CollisionEntry) new_entry = new CollisionEntry(entry); + + LVector3f surface_normal; + float vec_length = vec.length(); + if (IS_NEARLY_ZERO(vec_length)) { + // If we don't have a collision normal (e.g. the centers are + // exactly coincident), then make up an arbitrary normal--any one + // is as good as any other. + surface_normal.set(1.0, 0.0, 0.0); + } else { + surface_normal = -vec / vec_length; + } + + LVector3f normal = (has_effective_normal() && sphere->get_respect_effective_normal()) ? get_effective_normal() : surface_normal; + + new_entry->set_surface_normal(normal); + new_entry->set_surface_point(into_center - surface_normal * into_radius); + new_entry->set_interior_point(from_center - surface_normal * from_radius); + + return new_entry; +} + +//////////////////////////////////////////////////////////////////// +// Function: CollisionInvSphere::test_intersection_from_line +// Access: Public, Virtual +// Description: +//////////////////////////////////////////////////////////////////// +PT(CollisionEntry) CollisionInvSphere:: +test_intersection_from_line(const CollisionEntry &entry) const { + const CollisionLine *line; + DCAST_INTO_R(line, entry.get_from(), 0); + + const LMatrix4f &wrt_mat = entry.get_wrt_mat(); + + LPoint3f from_origin = line->get_origin() * wrt_mat; + LVector3f from_direction = line->get_direction() * wrt_mat; + + double t1, t2; + if (!intersects_line(t1, t2, from_origin, from_direction)) { + // The line is in the middle of space, and therefore intersects + // the sphere. + t1 = t2 = 0.0; + } + + if (collide_cat.is_debug()) { + collide_cat.debug() + << "intersection detected from " << entry.get_from_node_path() + << " into " << entry.get_into_node_path() << "\n"; + } + PT(CollisionEntry) new_entry = new CollisionEntry(entry); + + LPoint3f into_intersection_point = from_origin + t2 * from_direction; + new_entry->set_surface_point(into_intersection_point); + + if (has_effective_normal() && line->get_respect_effective_normal()) { + new_entry->set_surface_normal(get_effective_normal()); + } else { + LVector3f normal = into_intersection_point - get_center(); + normal.normalize(); + new_entry->set_surface_normal(-normal); + } + + return new_entry; +} + +//////////////////////////////////////////////////////////////////// +// Function: CollisionInvSphere::test_intersection_from_ray +// Access: Public, Virtual +// Description: +//////////////////////////////////////////////////////////////////// +PT(CollisionEntry) CollisionInvSphere:: +test_intersection_from_ray(const CollisionEntry &entry) const { + const CollisionRay *ray; + DCAST_INTO_R(ray, entry.get_from(), 0); + + const LMatrix4f &wrt_mat = entry.get_wrt_mat(); + + LPoint3f from_origin = ray->get_origin() * wrt_mat; + LVector3f from_direction = ray->get_direction() * wrt_mat; + + double t1, t2; + if (!intersects_line(t1, t2, from_origin, from_direction)) { + // The ray is in the middle of space, and therefore intersects + // the sphere. + t1 = t2 = 0.0; + } + + t2 = max(t2, 0.0); + + if (collide_cat.is_debug()) { + collide_cat.debug() + << "intersection detected from " << entry.get_from_node_path() + << " into " << entry.get_into_node_path() << "\n"; + } + PT(CollisionEntry) new_entry = new CollisionEntry(entry); + + LPoint3f into_intersection_point; + into_intersection_point = from_origin + t2 * from_direction; + new_entry->set_surface_point(into_intersection_point); + + if (has_effective_normal() && ray->get_respect_effective_normal()) { + new_entry->set_surface_normal(get_effective_normal()); + } else { + LVector3f normal = into_intersection_point - get_center(); + normal.normalize(); + new_entry->set_surface_normal(-normal); + } + + return new_entry; +} + +//////////////////////////////////////////////////////////////////// +// Function: CollisionInvSphere::test_intersection_from_segment +// Access: Public, Virtual +// Description: +//////////////////////////////////////////////////////////////////// +PT(CollisionEntry) CollisionInvSphere:: +test_intersection_from_segment(const CollisionEntry &entry) const { + const CollisionSegment *segment; + DCAST_INTO_R(segment, entry.get_from(), 0); + + const LMatrix4f &wrt_mat = entry.get_wrt_mat(); + + LPoint3f from_a = segment->get_point_a() * wrt_mat; + LPoint3f from_b = segment->get_point_b() * wrt_mat; + LVector3f from_direction = from_b - from_a; + + double t1, t2; + if (!intersects_line(t1, t2, from_a, from_direction)) { + // The segment is in the middle of space, and therefore intersects + // the sphere. + t1 = t2 = 0.0; + } + + double t; + if (t2 <= 0.0) { + // The segment is completely below the shell. + t = 0.0; + + } else if (t1 >= 1.0) { + // The segment is completely above the shell. + t = 1.0; + + } else if (t2 <= 1.0) { + // The bottom edge of the segment intersects the shell. + t = min(t2, 1.0); + + } else if (t1 >= 0.0) { + // The top edge of the segment intersects the shell. + t = max(t1, 0.0); + + } else { + // Neither edge of the segment intersects the shell. It follows + // that both intersection points are within the hollow center of + // the sphere; therefore, there is no intersection. + return NULL; + } + + if (collide_cat.is_debug()) { + collide_cat.debug() + << "intersection detected from " << entry.get_from_node_path() + << " into " << entry.get_into_node_path() << "\n"; + } + PT(CollisionEntry) new_entry = new CollisionEntry(entry); + + LPoint3f into_intersection_point = from_a + t * from_direction; + new_entry->set_surface_point(into_intersection_point); + + if (has_effective_normal() && segment->get_respect_effective_normal()) { + new_entry->set_surface_normal(get_effective_normal()); + } else { + LVector3f normal = into_intersection_point - get_center(); + normal.normalize(); + new_entry->set_surface_normal(-normal); + } + + return new_entry; +} + +//////////////////////////////////////////////////////////////////// +// Function: CollisionInvSphere::fill_viz_geom +// Access: Protected, Virtual +// Description: Fills the _viz_geom GeomNode up with Geoms suitable +// for rendering this solid. +//////////////////////////////////////////////////////////////////// +void CollisionInvSphere:: +fill_viz_geom() { + if (collide_cat.is_debug()) { + collide_cat.debug() + << "Recomputing viz for " << *this << "\n"; + } + + static const int num_slices = 8; + static const int num_stacks = 8; + + GeomTristrip *sphere = new GeomTristrip; + PTA_Vertexf verts; + PTA_int lengths; + verts.reserve((num_stacks * 2) * num_slices); + lengths.reserve(num_slices); + + for (int sl = 0; sl < num_slices; sl++) { + float longitude0 = (float)sl / (float)num_slices; + float longitude1 = (float)(sl + 1) / (float)num_slices; + verts.push_back(compute_point(0.0, longitude0)); + for (int st = 1; st < num_stacks; st++) { + float latitude = (float)st / (float)num_stacks; + verts.push_back(compute_point(latitude, longitude1)); + verts.push_back(compute_point(latitude, longitude0)); + } + verts.push_back(compute_point(1.0, longitude0)); + + lengths.push_back(num_stacks * 2); + } + + sphere->set_coords(verts); + sphere->set_lengths(lengths); + sphere->set_num_prims(num_slices); + + _viz_geom->add_geom(sphere, get_solid_viz_state()); +} + +//////////////////////////////////////////////////////////////////// +// Function: CollisionInvSphere::register_with_read_factory +// Access: Public, Static +// Description: Factory method to generate a CollisionInvSphere object +//////////////////////////////////////////////////////////////////// +void CollisionInvSphere:: +register_with_read_factory() { + BamReader::get_factory()->register_factory(get_class_type(), make_CollisionInvSphere); +} + +//////////////////////////////////////////////////////////////////// +// Function: CollisionInvSphere::write_datagram +// Access: Public +// Description: Function to write the important information in +// the particular object to a Datagram +//////////////////////////////////////////////////////////////////// +void CollisionInvSphere:: +write_datagram(BamWriter *manager, Datagram &me) { + CollisionSphere::write_datagram(manager, me); +} + +//////////////////////////////////////////////////////////////////// +// Function: CollisionInvSphere::make_CollisionInvSphere +// Access: Protected +// Description: Factory method to generate a CollisionInvSphere object +//////////////////////////////////////////////////////////////////// +TypedWritable *CollisionInvSphere:: +make_CollisionInvSphere(const FactoryParams ¶ms) { + CollisionInvSphere *me = new CollisionInvSphere; + DatagramIterator scan; + BamReader *manager; + + parse_params(params, scan, manager); + me->fillin(scan, manager); + return me; +} + +//////////////////////////////////////////////////////////////////// +// Function: CollisionInvSphere::fillin +// Access: Protected +// Description: Function that reads out of the datagram (or asks +// manager to read) all of the data that is needed to +// re-create this object and stores it in the appropiate +// place +//////////////////////////////////////////////////////////////////// +void CollisionInvSphere:: +fillin(DatagramIterator& scan, BamReader* manager) { + CollisionSphere::fillin(scan, manager); +} + diff --git a/panda/src/collide/collisionInvSphere.h b/panda/src/collide/collisionInvSphere.h new file mode 100644 index 0000000000..7032c9536e --- /dev/null +++ b/panda/src/collide/collisionInvSphere.h @@ -0,0 +1,96 @@ +// Filename: collisionInvSphere.h +// Created by: drose (05Jan05) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#ifndef COLLISIONINVSPHERE_H +#define COLLISIONINVSPHERE_H + +#include "pandabase.h" + +#include "collisionSphere.h" + +/////////////////////////////////////////////////////////////////// +// Class : CollisionInvSphere +// Description : An inverted sphere: this is a sphere whose collision +// surface is the inside surface of the sphere. +// Everything outside the sphere is solid matter; +// everything inside is empty space. Useful for +// constraining objects to remain within a spherical +// perimeter. +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDA CollisionInvSphere : public CollisionSphere { +PUBLISHED: + INLINE CollisionInvSphere(const LPoint3f ¢er, float radius); + INLINE CollisionInvSphere(float cx, float cy, float cz, float radius); + +protected: + INLINE CollisionInvSphere(); + +public: + INLINE CollisionInvSphere(const CollisionInvSphere ©); + virtual CollisionSolid *make_copy(); + + virtual PT(CollisionEntry) + test_intersection(const CollisionEntry &entry) const; + + virtual void output(ostream &out) const; + +protected: + virtual BoundingVolume *recompute_bound(); + + virtual PT(CollisionEntry) + test_intersection_from_sphere(const CollisionEntry &entry) const; + virtual PT(CollisionEntry) + test_intersection_from_line(const CollisionEntry &entry) const; + virtual PT(CollisionEntry) + test_intersection_from_ray(const CollisionEntry &entry) const; + virtual PT(CollisionEntry) + test_intersection_from_segment(const CollisionEntry &entry) const; + + virtual void fill_viz_geom(); + +public: + static void register_with_read_factory(); + virtual void write_datagram(BamWriter *manager, Datagram &me); + +protected: + static TypedWritable *make_CollisionInvSphere(const FactoryParams ¶ms); + void fillin(DatagramIterator &scan, BamReader *manager); + +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + CollisionSphere::init_type(); + register_type(_type_handle, "CollisionInvSphere", + CollisionSphere::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() {init_type(); return get_class_type();} + +private: + static TypeHandle _type_handle; +}; + +#include "collisionInvSphere.I" + +#endif + + diff --git a/panda/src/collide/collisionSphere.I b/panda/src/collide/collisionSphere.I index 7ef093f129..68d0909866 100644 --- a/panda/src/collide/collisionSphere.I +++ b/panda/src/collide/collisionSphere.I @@ -41,7 +41,7 @@ CollisionSphere(float cx, float cy, float cz, float radius) : //////////////////////////////////////////////////////////////////// // Function: CollisionSphere::Default constructor -// Access: Private +// Access: Protected // Description: Creates an invalid sphere. Only used when reading // from a bam file. //////////////////////////////////////////////////////////////////// diff --git a/panda/src/collide/collisionSphere.cxx b/panda/src/collide/collisionSphere.cxx index 8646cc4cf7..1cbd927e41 100644 --- a/panda/src/collide/collisionSphere.cxx +++ b/panda/src/collide/collisionSphere.cxx @@ -30,8 +30,10 @@ #include "datagramIterator.h" #include "bamReader.h" #include "bamWriter.h" -#include "geomSphere.h" +#include "geomTristrip.h" #include "nearly_zero.h" +#include "cmath.h" +#include "mathNumbers.h" TypeHandle CollisionSphere::_type_handle; @@ -92,7 +94,7 @@ get_collision_origin() const { //////////////////////////////////////////////////////////////////// void CollisionSphere:: output(ostream &out) const { - out << "csphere, c (" << _center << "), r " << _radius; + out << "sphere, c (" << get_center() << "), r " << get_radius(); } //////////////////////////////////////////////////////////////////// @@ -128,8 +130,8 @@ test_intersection_from_sphere(const CollisionEntry &entry) const { LVector3f(sphere->get_radius(), 0.0f, 0.0f) * wrt_mat; float from_radius = length(from_radius_v); - LPoint3f into_center = _center; - float into_radius = _radius; + LPoint3f into_center = get_center(); + float into_radius = get_radius(); LVector3f vec = from_center - into_center; float dist2 = dot(vec, vec); @@ -140,8 +142,8 @@ test_intersection_from_sphere(const CollisionEntry &entry) const { if (collide_cat.is_debug()) { collide_cat.debug() - << "intersection detected from " << entry.get_from_node_path() << " into " - << entry.get_into_node_path() << "\n"; + << "intersection detected from " << entry.get_from_node_path() + << " into " << entry.get_into_node_path() << "\n"; } PT(CollisionEntry) new_entry = new CollisionEntry(entry); @@ -199,7 +201,7 @@ test_intersection_from_line(const CollisionEntry &entry) const { if (has_effective_normal() && line->get_respect_effective_normal()) { new_entry->set_surface_normal(get_effective_normal()); } else { - LVector3f normal = into_intersection_point - _center; + LVector3f normal = into_intersection_point - get_center(); normal.normalize(); new_entry->set_surface_normal(normal); } @@ -233,6 +235,8 @@ test_intersection_from_ray(const CollisionEntry &entry) const { return NULL; } + t1 = max(t1, 0.0); + if (collide_cat.is_debug()) { collide_cat.debug() << "intersection detected from " << entry.get_from_node_path() @@ -240,22 +244,13 @@ test_intersection_from_ray(const CollisionEntry &entry) const { } PT(CollisionEntry) new_entry = new CollisionEntry(entry); - LPoint3f into_intersection_point; - if (t1 < 0.0) { - // Point a is within the sphere. The first intersection point is - // point a itself. - into_intersection_point = from_origin; - } else { - // Point a is outside the sphere. The first intersection point is - // at t1. - into_intersection_point = from_origin + t1 * from_direction; - } + LPoint3f into_intersection_point = from_origin + t1 * from_direction; new_entry->set_surface_point(into_intersection_point); if (has_effective_normal() && ray->get_respect_effective_normal()) { new_entry->set_surface_normal(get_effective_normal()); } else { - LVector3f normal = into_intersection_point - _center; + LVector3f normal = into_intersection_point - get_center(); normal.normalize(); new_entry->set_surface_normal(normal); } @@ -291,6 +286,8 @@ test_intersection_from_segment(const CollisionEntry &entry) const { return NULL; } + t1 = max(t1, 0.0); + if (collide_cat.is_debug()) { collide_cat.debug() << "intersection detected from " << entry.get_from_node_path() @@ -298,22 +295,13 @@ test_intersection_from_segment(const CollisionEntry &entry) const { } PT(CollisionEntry) new_entry = new CollisionEntry(entry); - LPoint3f into_intersection_point; - if (t1 < 0.0) { - // Point a is within the sphere. The first intersection point is - // point a itself. - into_intersection_point = from_a; - } else { - // Point a is outside the sphere, and point b is either inside the - // sphere or beyond it. The first intersection point is at t1. - into_intersection_point = from_a + t1 * from_direction; - } + LPoint3f into_intersection_point = from_a + t1 * from_direction; new_entry->set_surface_point(into_intersection_point); if (has_effective_normal() && segment->get_respect_effective_normal()) { new_entry->set_surface_normal(get_effective_normal()); } else { - LVector3f normal = into_intersection_point - _center; + LVector3f normal = into_intersection_point - get_center(); normal.normalize(); new_entry->set_surface_normal(normal); } @@ -334,19 +322,39 @@ fill_viz_geom() { << "Recomputing viz for " << *this << "\n"; } - GeomSphere *sphere = new GeomSphere; + static const int num_slices = 8; + static const int num_stacks = 8; + + GeomTristrip *sphere = new GeomTristrip; PTA_Vertexf verts; - verts.push_back(_center); - verts.push_back(_center + LVector3f(_radius, 0.0f, 0.0f)); + PTA_int lengths; + verts.reserve((num_stacks * 2) * num_slices); + lengths.reserve(num_slices); + + for (int sl = 0; sl < num_slices; sl++) { + float longitude0 = (float)sl / (float)num_slices; + float longitude1 = (float)(sl + 1) / (float)num_slices; + verts.push_back(compute_point(0.0, longitude0)); + for (int st = 1; st < num_stacks; st++) { + float latitude = (float)st / (float)num_stacks; + verts.push_back(compute_point(latitude, longitude0)); + verts.push_back(compute_point(latitude, longitude1)); + } + verts.push_back(compute_point(1.0, longitude0)); + + lengths.push_back(num_stacks * 2); + } + sphere->set_coords(verts); - sphere->set_num_prims(1); + sphere->set_lengths(lengths); + sphere->set_num_prims(num_slices); _viz_geom->add_geom(sphere, get_solid_viz_state()); } //////////////////////////////////////////////////////////////////// // Function: CollisionSphere::intersects_line -// Access: Private +// Access: Protected // Description: Determine the point(s) of intersection of a parametric // line with the sphere. The line is infinite in both // directions, and passes through "from" and from+delta. @@ -394,10 +402,10 @@ intersects_line(double &t1, double &t2, nassertr(A != 0.0, false); - LVector3f fc = from - _center; + LVector3f fc = from - get_center(); double B = 2.0f* dot(delta, fc); double fc_d2 = dot(fc, fc); - double C = fc_d2 - _radius * _radius; + double C = fc_d2 - get_radius() * get_radius(); double radical = B*B - 4.0*A*C; @@ -419,6 +427,26 @@ intersects_line(double &t1, double &t2, return true; } +//////////////////////////////////////////////////////////////////// +// Function: CollisionSphere::compute_point +// Access: Protected +// Description: Returns a point on the surface of the sphere. +// latitude and longitude range from 0.0 to 1.0. This +// is used by fill_viz_geom() to create a visible +// representation of the sphere. +//////////////////////////////////////////////////////////////////// +Vertexf CollisionSphere:: +compute_point(float latitude, float longitude) const { + float s1, c1; + csincos(latitude * MathNumbers::pi_f, &s1, &c1); + + float s2, c2; + csincos(longitude * 2.0f * MathNumbers::pi_f, &s2, &c2); + + Vertexf p(s1 * c2, s1 * s2, c1); + return p * get_radius() + get_center(); +} + //////////////////////////////////////////////////////////////////// // Function: CollisionSphere::register_with_read_factory // Access: Public, Static diff --git a/panda/src/collide/collisionSphere.h b/panda/src/collide/collisionSphere.h index af9450169c..99f9fb9783 100644 --- a/panda/src/collide/collisionSphere.h +++ b/panda/src/collide/collisionSphere.h @@ -32,7 +32,7 @@ PUBLISHED: INLINE CollisionSphere(const LPoint3f ¢er, float radius); INLINE CollisionSphere(float cx, float cy, float cz, float radius); -private: +protected: INLINE CollisionSphere(); public: @@ -69,9 +69,10 @@ protected: virtual void fill_viz_geom(); -private: +protected: bool intersects_line(double &t1, double &t2, const LPoint3f &from, const LVector3f &delta) const; + Vertexf compute_point(float latitude, float longitude) const; private: LPoint3f _center; diff --git a/panda/src/collide/config_collide.cxx b/panda/src/collide/config_collide.cxx index ed9ae41df9..7eeb3de03b 100644 --- a/panda/src/collide/config_collide.cxx +++ b/panda/src/collide/config_collide.cxx @@ -25,6 +25,7 @@ #include "collisionHandlerPhysical.h" #include "collisionHandlerPusher.h" #include "collisionHandlerQueue.h" +#include "collisionInvSphere.h" #include "collisionLine.h" #include "collisionNode.h" #include "collisionPlane.h" @@ -86,6 +87,7 @@ init_libcollide() { CollisionHandlerPhysical::init_type(); CollisionHandlerPusher::init_type(); CollisionHandlerQueue::init_type(); + CollisionInvSphere::init_type(); CollisionLine::init_type(); CollisionNode::init_type(); CollisionPlane::init_type(); @@ -101,8 +103,7 @@ init_libcollide() { CollisionVisualizer::init_type(); #endif - // Registration of writeable object's creation - // functions with BamReader's factory + CollisionInvSphere::register_with_read_factory(); CollisionLine::register_with_read_factory(); CollisionNode::register_with_read_factory(); CollisionPlane::register_with_read_factory();