15 #include "physicsCollisionHandler.h" 16 #include "collisionNode.h" 17 #include "collisionEntry.h" 18 #include "collisionPolygon.h" 19 #include "config_collide.h" 20 #include "config_physics.h" 21 #include "actorNode.h" 24 TypeHandle PhysicsCollisionHandler::_type_handle;
31 PhysicsCollisionHandler::
32 PhysicsCollisionHandler() {
33 _almost_stationary_speed = 0.1f;
34 _static_friction_coef=0.9f;
35 _dynamic_friction_coef=0.5f;
36 set_horizontal(
false);
44 PhysicsCollisionHandler::
45 ~PhysicsCollisionHandler() {
54 void PhysicsCollisionHandler::
58 PN_stdfloat friction_coefficient=0.0f;
60 if (vel.
length()<_almost_stationary_speed) {
61 physics_debug(
" static friction");
62 friction_coefficient=_static_friction_coef;
64 physics_debug(
" dynamic friction");
65 friction_coefficient=_dynamic_friction_coef;
68 physics_debug(
" vel pre friction "<<vel<<
" len "<<vel.
length());
69 PN_stdfloat friction=friction_coefficient*angle;
70 physics_debug(
" friction "<<friction);
71 if (friction<0.0f || friction>1.0f) {
72 cerr<<
"\n\nfriction error "<<friction<<endl;
77 vel *= (1.0f-friction) * dt * dt;
81 physics_debug(
" vel post friction "<<vel<<
" len "<<vel.
length());
90 void PhysicsCollisionHandler::
91 apply_net_shove(ColliderDef &def,
const LVector3& net_shove,
93 CollisionHandlerPusher::apply_net_shove(def, net_shove, force);
97 if (def._target.is_empty()) {
101 DCAST_INTO_V(actor, def._target.node());
106 physics_debug(
"apply_linear_force() {");
107 physics_debug(
" vel "<<vel<<
" len "<<vel.length());
108 physics_debug(
" net_shove "<<net_shove<<
" len "<<net_shove.
length());
109 physics_debug(
" force "<<force<<
" len "<<force.
length());
116 " adjustment set "<<adjustment<<
" len "<<adjustment.
length());
124 adjustment=adjustment*actor->get_physics_object()->
get_lcs();
126 " adjustment lcs "<<adjustment<<
" len "<<adjustment.length());
128 adjustment.normalize();
130 " adjustment nrm "<<adjustment<<
" len "<<adjustment.length());
132 PN_stdfloat adjustmentLength=-(adjustment.dot(vel));
133 physics_debug(
" adjustmentLength "<<adjustmentLength);
134 PN_stdfloat angle=-normalize(old_vel).dot(normalize(force));
135 physics_debug(
" angle "<<angle);
138 physics_debug(
" positive contact");
140 cerr<<
"vel "<<vel<<endl;
141 cerr<<
"net_shove "<<net_shove<<endl;
142 cerr<<
"force "<<force<<endl;
143 actor->get_physics_object()->
add_impact(force, -vel);
145 adjustment*=adjustmentLength;
147 " adjustment mul "<<adjustment<<
" len "<<adjustment.length());
152 physics_debug(
" vel+adj "<<vel<<
" len "<<vel.length());
154 apply_friction(def, vel, force, angle);
156 }
else if (adjustmentLength==0.0f) {
157 physics_debug(
" brushing contact");
159 physics_debug(
" negative contact");
163 if (IS_THRESHOLD_EQUAL(vel.length(), old_vel.length(), 0.0001f)) {
166 " vel is about the same length: " 167 <<vel.length()<<
" ~ "<<old_vel.length());
168 }
else if (vel.length() > old_vel.length()) {
171 " vel got larger "<<vel.length()<<
" > "<<old_vel.length());
175 " vel got smaller "<<vel.length()<<
" < "<<old_vel.length());
177 if (vel.length() > 10.0f) {
180 physics_debug(
" vel.length() > 10.0f "<<vel.length());
184 physics_debug(
" force "<<force<<
" len "<<force.
length());
185 physics_debug(
" vel "<<vel<<
" len "<<vel.length());
187 actor->set_contact_vector(adjustment);
196 void PhysicsCollisionHandler::
197 apply_linear_force(ColliderDef &def,
const LVector3 &force) {
207 bool PhysicsCollisionHandler::
208 validate_target(
const NodePath &target) {
209 if (!CollisionHandlerPhysical::validate_target(target)) {
212 nassertr_always(target.
node()->
is_of_type(ActorNode::get_class_type()),
false);
static ClockObject * get_global_clock()
Returns a pointer to the global ClockObject.
static const LVector3f & zero()
Returns a zero-length vector.
void set_velocity(const LVector3 &vel)
Vector velocity assignment.
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
LVector3 get_velocity() const
Velocity Query per second.
float length() const
Returns the length of the vector, by the Pythagorean theorem.
PandaNode * node() const
Returns the referenced node of the path.
bool is_of_type(TypeHandle handle) const
Returns true if the current object is or derives from the indicated type.
TypeHandle is the identifier used to differentiate C++ class types.
Like a physical node, but with a little more.
virtual void add_impact(const LPoint3 &offset_from_center_of_mass, const LVector3 &impulse)
Adds an impulse and/or torque (i.e.
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
virtual LMatrix4 get_lcs() const
returns a transform matrix to this object's local coordinate system.
double get_dt(Thread *current_thread=Thread::get_current_thread()) const
Returns the elapsed time for the previous frame: the number of seconds elapsed between the last two c...