24TypeHandle CollisionHandlerGravity::_type_handle;
29CollisionHandlerGravity::
30CollisionHandlerGravity() {
33 _airborne_height = 0.0f;
34 _impact_velocity = 0.0f;
36 _current_velocity = 0.0f;
37 _max_velocity = 400.0f;
38 _contact_normal = LVector3::zero();
45CollisionHandlerGravity::
46~CollisionHandlerGravity() {
81#define OLD_COLLISION_HANDLER_GRAVITY 0
82#if OLD_COLLISION_HANDLER_GRAVITY
83PN_stdfloat CollisionHandlerGravity::
84set_highest_collision(
const NodePath &target_node_path,
const NodePath &from_node_path,
const Entries &entries) {
87 PN_stdfloat max_height = 0.0f;
90 Entries::const_iterator ei;
91 for (ei = entries.begin(); ei != entries.end(); ++ei) {
93 nassertr(entry !=
nullptr, 0.0f);
98 if (collide_cat.is_debug()) {
100 <<
"Intersection point detected at " << point <<
"\n";
103 PN_stdfloat height = point[2];
104 if (!got_max || height > max_height) {
114 cout<<
"\ncolliding with:\n";
115 for (Colliding::const_iterator i = _current_colliding.begin(); i != _current_colliding.end(); ++i) {
116 (**i).write(cout, 2);
118 cout<<
"\nhighest:\n";
119 highest->write(cout, 2);
126 _current_colliding.clear();
134PN_stdfloat CollisionHandlerGravity::
135set_highest_collision(
const NodePath &target_node_path,
const NodePath &from_node_path,
const Entries &entries) {
140 bool got_max =
false;
141 bool got_min =
false;
142 PN_stdfloat max_height = 0.0f;
143 PN_stdfloat min_height = 0.0f;
149 Entries::const_iterator ei;
150 for (ei = entries.begin(); ei != entries.end(); ++ei) {
152 nassertr(entry !=
nullptr, 0.0f);
157 if (collide_cat.is_debug()) {
159 <<
"Intersection point detected at " << point <<
"\n";
161 PN_stdfloat height = point[2];
162 if(height < _offset + _reach) {
163 valid_entries.push_back(entry);
164 if (!got_max || height > max_height) {
170 if (!got_min || height < min_height) {
177 if (!got_max && got_min) {
181 max_height = min_height;
183 valid_entries.push_back(lowest);
188 cout<<
"\ncolliding with:\n";
189 for (Colliding::const_iterator i = _current_colliding.begin(); i != _current_colliding.end(); ++i) {
190 (**i).write(cout, 2);
192 cout<<
"\nhighest:\n";
193 highest->write(cout, 2);
199 _current_colliding.clear();
206 for (vi = valid_entries.begin(); vi != valid_entries.end(); ++vi) {
214 if (highest->
get_into()->is_of_type(CollisionPlane::get_class_type())) {
244bool CollisionHandlerGravity::
248 FromEntries::const_iterator fi;
249 for (fi = _from_entries.begin(); fi != _from_entries.end(); ++fi) {
250 const NodePath &from_node_path = (*fi).first;
251 const Entries &entries = (*fi).second;
253 Colliders::iterator ci;
254 ci = _colliders.
find(from_node_path);
255 if (ci == _colliders.end()) {
259 << get_type() <<
" doesn't know about "
260 << from_node_path <<
", disabling.\n";
263 ColliderDef &def = (*ci).second;
264 PN_stdfloat max_height = set_highest_collision(def._target, from_node_path, entries);
267 #if OLD_COLLISION_HANDLER_GRAVITY
268 PN_stdfloat adjust = max_height + _offset;
270 PN_stdfloat adjust = max_height + _offset;
272 if (_current_velocity > 0.0f || !IS_THRESHOLD_ZERO(adjust, 0.001)) {
273 if (collide_cat.is_debug()) {
275 <<
"Adjusting height by " << adjust <<
"\n";
278 if (_current_velocity > 0.0f || adjust) {
284 PN_stdfloat gravity_adjust = _current_velocity * dt + 0.5 * -_gravity * dt * dt;
289 adjust += std::max((PN_stdfloat)0.0, gravity_adjust);
292 adjust = std::max(adjust, gravity_adjust);
294 _current_velocity -= _gravity * dt;
296 _airborne_height = -(max_height + _offset) + adjust;
297 assert(_airborne_height >= -0.001f);
300 if (_airborne_height < 0.001f && _current_velocity < 0.001f) {
302 _impact_velocity = _current_velocity;
304 _current_velocity = _airborne_height = 0.0f;
305 }
else if (_legacy_mode) {
307 _current_colliding.clear();
311 LVecBase3 pos = trans->get_pos();
313 def._target.set_transform(trans->set_pos(pos));
314 def.updated_transform();
316 apply_linear_force(def, LVector3(0.0f, 0.0f, adjust));
319 _current_velocity = _airborne_height = 0.0f;
320 if (collide_cat.is_spam()) {
322 <<
"Leaving height unchanged.\n";
334void CollisionHandlerGravity::
335apply_linear_force(ColliderDef &def,
const LVector3 &force) {
get_dt
Returns the elapsed time for the previous frame: the number of seconds elapsed between the last two c...
static ClockObject * get_global_clock()
Returns a pointer to the global ClockObject.
Defines a single collision event.
get_into
Returns the CollisionSolid pointer for the particular solid was collided into.
LPoint3 get_surface_point(const NodePath &space) const
Returns the point, on the surface of the "into" object, at which a collision is detected.
get_from_node_path
Returns the NodePath that represents the CollisionNode that contains the CollisionSolid that triggere...
bool has_surface_point() const
Returns true if the surface point has been specified, false otherwise.
LVector3 get_surface_normal(const NodePath &space) const
Returns the surface normal of the "into" object at the point at which a collision is detected.
get_into_node_path
Returns the NodePath that represents the specific CollisionNode or GeomNode instance that was collide...
void read_datagram(DatagramIterator &source)
Restores the object state from the given datagram, previously obtained using __getstate__.
void write_datagram(Datagram &destination) const
Serializes this object, to implement pickle support.
void write_datagram(Datagram &destination) const
Serializes this object, to implement pickle support.
void read_datagram(DatagramIterator &source)
Restores the object state from the given datagram, previously obtained using __getstate__.
virtual void add_entry(CollisionEntry *entry)
Called between a begin_group() .
A class to retrieve the individual data elements previously stored in a Datagram.
PN_float64 get_float64()
Extracts a 64-bit floating-point number.
bool get_bool()
Extracts a boolean value.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
void add_bool(bool value)
Adds a boolean value to the datagram.
void add_float64(PN_float64 value)
Adds a 64-bit floating-point number to the datagram.
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
NodePath find(const std::string &path) const
Searches for a node below the referenced node that matches the indicated string.
TypeHandle is the identifier used to differentiate C++ class types.
This is our own Panda specialization on the default STL vector.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.