Panda3D
 All Classes Functions Variables Enumerations
collisionLevelStateBase.cxx
1 // Filename: collisionLevelStateBase.cxx
2 // Created by: drose (16Mar02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "collisionLevelStateBase.h"
16 #include "collisionSolid.h"
17 #include "collisionNode.h"
18 #include "config_collide.h"
19 #include "dcast.h"
20 
21 PStatCollector CollisionLevelStateBase::_node_volume_pcollector("Collision Volumes:PandaNode");
22 
23 TypeHandle CollisionLevelStateBase::_type_handle;
24 
25 ////////////////////////////////////////////////////////////////////
26 // Function: CollisionLevelStateBase::clear
27 // Access: Public
28 // Description:
29 ////////////////////////////////////////////////////////////////////
30 void CollisionLevelStateBase::
31 clear() {
32  _colliders.clear();
33  _local_bounds.clear();
34  _parent_bounds.clear();
35 }
36 
37 ////////////////////////////////////////////////////////////////////
38 // Function: CollisionLevelStateBase::reserve
39 // Access: Public
40 // Description: Indicates an intention to add the indicated number of
41 // colliders to the level state.
42 ////////////////////////////////////////////////////////////////////
44 reserve(int num_colliders) {
45  _colliders.reserve(num_colliders);
46  _local_bounds.reserve(num_colliders);
47 }
48 
49 ////////////////////////////////////////////////////////////////////
50 // Function: CollisionLevelStateBase::prepare_collider
51 // Access: Public
52 // Description: Adds the indicated Collider to the set of Colliders
53 // in the current level state.
54 ////////////////////////////////////////////////////////////////////
56 prepare_collider(const ColliderDef &def, const NodePath &root) {
57  _colliders.push_back(def);
58 
59  const CollisionSolid *collider = def._collider;
60  CPT(BoundingVolume) bv = collider->get_bounds();
61  if (!bv->is_of_type(GeometricBoundingVolume::get_class_type())) {
62  _local_bounds.push_back((GeometricBoundingVolume *)NULL);
63  } else {
64  // We can use a plain pointer, rather than a PT() here, because we
65  // know we are going to save the volume in the vector, below.
67  DCAST_INTO_V(gbv, bv->make_copy());
68 
69  // TODO: we need to make this logic work in the new relative
70  // world. The bounding volume should be extended by the object's
71  // motion relative to each object it is considering a collision
72  // with. That makes things complicated!
73  if (bv->as_bounding_sphere()) {
74  LPoint3 pos_delta = def._node_path.get_pos_delta(root);
75 
76  //LVector3 cap(pos_delta);
77  //if(cap.length()>fluid_cap_amount) {
78  // pos_delta=LPoint3(cap/cap.length())*fluid_cap_amount;
79  //}
80  if (pos_delta != LVector3::zero()) {
81  // If the node has a delta, we have to include the starting
82  // position in the volume as well. We only do this for bounding
83  // spheres, since (a) other kinds of volumes may not extend so
84  // well, and (b) we've only implemented fluid-motion detection
85  // for CollisionSpheres anyway.
86  LMatrix4 inv_trans = LMatrix4::translate_mat(-pos_delta);
87  PT(GeometricBoundingVolume) gbv_prev;
88  gbv_prev = DCAST(GeometricBoundingVolume, bv->make_copy());
89 
90  gbv_prev->xform(inv_trans);
91  gbv->extend_by(gbv_prev);
92  }
93  }
94 
95  CPT(TransformState) rel_transform = def._node_path.get_transform(root.get_parent());
96  gbv->xform(rel_transform->get_mat());
97  _local_bounds.push_back(gbv);
98  }
99 
100  _parent_bounds = _local_bounds;
101 }
static LMatrix4f translate_mat(const LVecBase3f &trans)
Returns a matrix that applies the indicated translation.
Definition: lmatrix.h:2397
NodePath get_parent(Thread *current_thread=Thread::get_current_thread()) const
Returns the NodePath to the parent of the referenced node: that is, this NodePath, shortened by one node.
Definition: nodePath.I:460
The abstract base class for all things that can collide with other things in the world, and all the things they can collide with (except geometry).
const TransformState * get_transform(Thread *current_thread=Thread::get_current_thread()) const
Returns the complete transform object set on this node.
Definition: nodePath.cxx:925
static const LVector3f & zero()
Returns a zero-length vector.
Definition: lvector3.h:269
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
A lightweight class that represents a single element that may be timed and/or counted via stats...
This is another abstract class, for a general class of bounding volumes that actually enclose points ...
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
void reserve(int num_colliders)
Indicates an intention to add the indicated number of colliders to the level state.
void prepare_collider(const ColliderDef &def, const NodePath &root)
Adds the indicated Collider to the set of Colliders in the current level state.
LVector3 get_pos_delta() const
Returns the delta vector from this node's position in the previous frame (according to set_prev_trans...
Definition: nodePath.cxx:1198
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165
bool extend_by(const GeometricBoundingVolume *vol)
Increases the size of the volume to include the given volume.