Selaa lähdekoodia

Added slider constraint.

Lasse Öörni 13 vuotta sitten
vanhempi
sitoutus
4335295b87
3 muutettua tiedostoa jossa 30 lisäystä ja 3 poistoa
  1. 1 1
      Docs/Reference.dox
  2. 27 1
      Engine/Physics/Constraint.cpp
  3. 2 1
      Engine/Physics/Constraint.h

+ 1 - 1
Docs/Reference.dox

@@ -823,7 +823,7 @@ The other physics components are:
 
 - RigidBody: a physics object instance. Its parameters include mass, linear/angular velocities, friction and restitution.
 - CollisionShape: defines physics collision geometry. The supported shapes are box, sphere, cylinder, capsule, cone, triangle mesh and convex hull.
-- Constraint: connects two RigidBodies together, or one RigidBody to a static point in the world. Currently point and hinge constraints are supported.
+- Constraint: connects two RigidBodies together, or one RigidBody to a static point in the world. Currently point, hinge and slider constraints are supported.
 
 Both a RigidBody and at least one CollisionShape component must exist in a scene node for it to behave physically (a collision shape by itself does nothing.) Several collision shapes may exist in the same node to create compound shapes. An offset position and rotation relative to the node's transform can be specified for each. Triangle mesh and convex hull geometries require specifying a Model resource and the LOD level to use.
 

+ 27 - 1
Engine/Physics/Constraint.cpp

@@ -34,6 +34,7 @@
 
 #include "BulletDynamics/ConstraintSolver/btPoint2PointConstraint.h"
 #include "BulletDynamics/ConstraintSolver/btHingeConstraint.h"
+#include "BulletDynamics/ConstraintSolver/btSliderConstraint.h"
 #include <BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h>
 
 #include "DebugNew.h"
@@ -42,6 +43,7 @@ static const String typeNames[] =
 {
     "Point",
     "Hinge",
+    "Slider",
     ""
 };
 
@@ -195,6 +197,11 @@ void Constraint::SetLowLimit(float limit)
             btHingeConstraint* hingeConstraint = static_cast<btHingeConstraint*>(constraint_);
             hingeConstraint->setLimit(lowLimit_ * M_DEGTORAD, highLimit_ * M_DEGTORAD);
         }
+        if (constraint_ && constraint_->getConstraintType() == SLIDER_CONSTRAINT_TYPE)
+        {
+            btSliderConstraint* sliderConstraint = static_cast<btSliderConstraint*>(constraint_);
+            sliderConstraint->setLowerLinLimit(lowLimit_);
+        }
     }
 }
 
@@ -209,6 +216,11 @@ void Constraint::SetHighLimit(float limit)
             btHingeConstraint* hingeConstraint = static_cast<btHingeConstraint*>(constraint_);
             hingeConstraint->setLimit(lowLimit_ * M_DEGTORAD, highLimit_ * M_DEGTORAD);
         }
+        if (constraint_ && constraint_->getConstraintType() == SLIDER_CONSTRAINT_TYPE)
+        {
+            btSliderConstraint* sliderConstraint = static_cast<btSliderConstraint*>(constraint_);
+            sliderConstraint->setUpperLinLimit(highLimit_);
+        }
     }
 }
 
@@ -295,16 +307,30 @@ void Constraint::CreateConstraint()
         
     case CONSTRAINT_HINGE:
         {
-            btHingeConstraint* hingeConstraint;
             Quaternion ownRotation(Vector3::FORWARD, axis_);
             Quaternion otherRotation(Vector3::FORWARD, otherBodyAxis_);
             btTransform ownFrame(ToBtQuaternion(ownRotation), ToBtVector3(position_ * cachedWorldScale_));
             btTransform otherFrame(ToBtQuaternion(otherRotation), ToBtVector3(otherBodyPosition_));
             
+            btHingeConstraint* hingeConstraint;
             constraint_ = hingeConstraint = new btHingeConstraint(*ownBody, *otherBody, ownFrame, otherFrame);
             hingeConstraint->setLimit(lowLimit_ * M_DEGTORAD, highLimit_ * M_DEGTORAD);
         }
         break;
+        
+    case CONSTRAINT_SLIDER:
+        {
+            Quaternion ownRotation(Vector3::RIGHT, axis_);
+            Quaternion otherRotation(Vector3::RIGHT, otherBodyAxis_);
+            btTransform ownFrame(ToBtQuaternion(ownRotation), ToBtVector3(position_ * cachedWorldScale_));
+            btTransform otherFrame(ToBtQuaternion(otherRotation), ToBtVector3(otherBodyPosition_));
+            
+            btSliderConstraint* sliderConstraint;
+            constraint_ = sliderConstraint = new btSliderConstraint(*ownBody, *otherBody, ownFrame, otherFrame, false);
+            sliderConstraint->setLowerLinLimit(lowLimit_);
+            sliderConstraint->setUpperLinLimit(highLimit_);
+        }
+        break;
     }
     
     constraint_->setUserConstraintPtr(this);

+ 2 - 1
Engine/Physics/Constraint.h

@@ -30,7 +30,8 @@
 enum ConstraintType
 {
     CONSTRAINT_POINT = 0,
-    CONSTRAINT_HINGE
+    CONSTRAINT_HINGE,
+    CONSTRAINT_SLIDER
 };
 
 class PhysicsWorld;