26 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);
42 PhysicsCollisionHandler::
43 ~PhysicsCollisionHandler() {
49 void PhysicsCollisionHandler::
50 apply_friction(ColliderDef &def, LVector3& vel,
const LVector3& force,
52 if (vel!=LVector3::zero()) {
53 PN_stdfloat friction_coefficient=0.0f;
55 if (vel.length()<_almost_stationary_speed) {
56 physics_debug(
" static friction");
57 friction_coefficient=_static_friction_coef;
59 physics_debug(
" dynamic friction");
60 friction_coefficient=_dynamic_friction_coef;
63 physics_debug(
" vel pre friction "<<vel<<
" len "<<vel.length());
64 PN_stdfloat friction=friction_coefficient*angle;
65 physics_debug(
" friction "<<friction);
66 if (friction<0.0f || friction>1.0f) {
67 cerr<<
"\n\nfriction error "<<friction<<endl;
72 vel *= (1.0f-friction) * dt * dt;
76 physics_debug(
" vel post friction "<<vel<<
" len "<<vel.length());
83 void PhysicsCollisionHandler::
84 apply_net_shove(ColliderDef &def,
const LVector3& net_shove,
85 const LVector3 &force) {
86 CollisionHandlerPusher::apply_net_shove(def, net_shove, force);
87 if (force == LVector3::zero()) {
90 if (def._target.is_empty()) {
94 DCAST_INTO_V(actor, def._target.node());
95 LVector3 vel=actor->get_physics_object()->
get_velocity();
96 if (vel == LVector3::zero()) {
99 physics_debug(
"apply_linear_force() {");
100 physics_debug(
" vel "<<vel<<
" len "<<vel.length());
101 physics_debug(
" net_shove "<<net_shove<<
" len "<<net_shove.length());
102 physics_debug(
" force "<<force<<
" len "<<force.length());
103 LVector3 old_vel=vel;
107 LVector3 adjustment=force;
109 " adjustment set "<<adjustment<<
" len "<<adjustment.length());
116 adjustment=adjustment*actor->get_physics_object()->
get_lcs();
118 " adjustment lcs "<<adjustment<<
" len "<<adjustment.length());
120 adjustment.normalize();
122 " adjustment nrm "<<adjustment<<
" len "<<adjustment.length());
124 PN_stdfloat adjustmentLength=-(adjustment.dot(vel));
125 physics_debug(
" adjustmentLength "<<adjustmentLength);
126 PN_stdfloat angle=-normalize(old_vel).dot(normalize(force));
127 physics_debug(
" angle "<<angle);
130 physics_debug(
" positive contact");
132 cerr<<
"vel "<<vel<<endl;
133 cerr<<
"net_shove "<<net_shove<<endl;
134 cerr<<
"force "<<force<<endl;
135 actor->get_physics_object()->
add_impact(force, -vel);
137 adjustment*=adjustmentLength;
139 " adjustment mul "<<adjustment<<
" len "<<adjustment.length());
144 physics_debug(
" vel+adj "<<vel<<
" len "<<vel.length());
146 apply_friction(def, vel, force, angle);
148 }
else if (adjustmentLength==0.0f) {
149 physics_debug(
" brushing contact");
151 physics_debug(
" negative contact");
155 if (IS_THRESHOLD_EQUAL(vel.length(), old_vel.length(), 0.0001f)) {
158 " vel is about the same length: "
159 <<vel.length()<<
" ~ "<<old_vel.length());
160 }
else if (vel.length() > old_vel.length()) {
163 " vel got larger "<<vel.length()<<
" > "<<old_vel.length());
167 " vel got smaller "<<vel.length()<<
" < "<<old_vel.length());
169 if (vel.length() > 10.0f) {
172 physics_debug(
" vel.length() > 10.0f "<<vel.length());
176 physics_debug(
" force "<<force<<
" len "<<force.length());
177 physics_debug(
" vel "<<vel<<
" len "<<vel.length());
179 actor->set_contact_vector(adjustment);
186 void PhysicsCollisionHandler::
187 apply_linear_force(ColliderDef &def,
const LVector3 &force) {
194 bool PhysicsCollisionHandler::
195 validate_target(
const NodePath &target) {
196 if (!CollisionHandlerPhysical::validate_target(target)) {
199 nassertr_always(target.
node()->
is_of_type(ActorNode::get_class_type()),
false);