Browse Source

work in progress: add_impact

Dave Schuyler 20 years ago
parent
commit
b2a0448de9
1 changed files with 15 additions and 8 deletions
  1. 15 8
      panda/src/physics/physicsObject.cxx

+ 15 - 8
panda/src/physics/physicsObject.cxx

@@ -124,19 +124,26 @@ add_local_impact(const LPoint3f &offset_from_center_of_mass,
 //               offset and force are in global (or parent) coordinates.
 //               offset and force are in global (or parent) coordinates.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void PhysicsObject::
 void PhysicsObject::
-add_impact(const LPoint3f &offset_from_center_of_mass,
+add_impact(const LPoint3f &offset,
     const LVector3f &force) {
     const LVector3f &force) {
-  nassertv(!offset_from_center_of_mass.is_nan());
+  nassertv(!offset.is_nan());
   nassertv(!force.is_nan());
   nassertv(!force.is_nan());
-  LVector3f a = -offset_from_center_of_mass;
+  LVector3f a = offset;
   LVector3f b = force;
   LVector3f b = force;
   a.normalize();
   a.normalize();
   b.normalize();
   b.normalize();
-  float theta = a.dot(b);
-  LRotationf torque;
-  torque.set_from_axis_angle((1.0f - theta) * a.length(), b.cross(a).normalize());
-  LVector3f impulse = theta * force;
-  add_torque(torque);
+  a = a.cross(b);
+  float angle = a.length();
+  if (angle) {
+    LRotationf torque;
+    float spin = force.length()*0.1f; // todo: this should account for
+                                      // impact distance and mass.
+    a.normalize();
+    assert(IS_THRESHOLD_EQUAL(a.length(), 1.0f, 0.001f));
+    torque.set_from_axis_angle(spin, a);
+    add_torque(torque);
+  }
+  LVector3f impulse = (1.0f - angle) * force;
   add_impulse(impulse);
   add_impulse(impulse);
 }
 }