Ver código fonte

Merge pull request #182 from blackberry-gaming/next-sgrenier

Next sgrenier
Steve Grenier 14 anos atrás
pai
commit
a0c3ed128c
2 arquivos alterados com 65 adições e 19 exclusões
  1. 41 19
      gameplay/src/PhysicsCharacter.cpp
  2. 24 0
      gameplay/src/PhysicsCharacter.h

+ 41 - 19
gameplay/src/PhysicsCharacter.cpp

@@ -68,7 +68,7 @@ PhysicsCharacter::PhysicsCharacter(Node* node, float radius, float height, const
     _fallVelocity(0, 0, 0), _currentVelocity(0,0,0), _normalizedVelocity(0,0,0),
     _colliding(false), _collisionNormal(0,0,0), _currentPosition(0,0,0),
     _ghostObject(NULL), _collisionShape(NULL), _ignoreTransformChanged(0),
-    _stepHeight(0.2f), _slopeAngle(0.0f), _cosSlopeAngle(0.0f)
+    _stepHeight(0.2f), _slopeAngle(0.0f), _cosSlopeAngle(0.0f), _physicsEnabled(true)
 {
     setMaxSlopeAngle(45.0f);
 
@@ -129,6 +129,16 @@ btCollisionObject* PhysicsCharacter::getCollisionObject() const
     return _ghostObject;
 }
 
+bool PhysicsCharacter::isPhysicsEnabled() const
+{
+    return _physicsEnabled;
+}
+
+void PhysicsCharacter::setPhysicsEnabled(bool enabled)
+{
+    _physicsEnabled = enabled;
+}
+
 btCollisionShape* PhysicsCharacter::getCollisionShape() const
 {
     return _collisionShape;
@@ -380,35 +390,40 @@ void PhysicsCharacter::updateAction(btCollisionWorld* collisionWorld, btScalar d
     // the following steps (movement) start from a clean slate, where the character
     // is not colliding with anything. Also, this step handles collision between
     // dynamic objects (i.e. objects that moved and now intersect the character).
-    _colliding = fixCollision(collisionWorld);
-    /*_colliding = false;
-    int stepCount = 0;
-	while (fixCollision(collisionWorld))
-	{
-        _colliding = true;
-
-        // After a small number of attempts to fix a collision/penetration, give up.
-        // This hanldes the case where we are deeply penetrating some object and attempting
-        // to step out of it (by COLLISION_REPAIR_INCREMENT units) does not fix the collision.
-        if (++stepCount > COLLISION_REPAIR_MAX_ITERATIONS)
-		{
-            WARN_VARG("Character '%s' could not recover from collision.", _node->getId());
-			break;
-		}
-	}*/
+    if (_physicsEnabled)
+    {
+        _colliding = fixCollision(collisionWorld);
+        /*_colliding = false;
+        int stepCount = 0;
+	    while (fixCollision(collisionWorld))
+	    {
+            _colliding = true;
+
+            // After a small number of attempts to fix a collision/penetration, give up.
+            // This hanldes the case where we are deeply penetrating some object and attempting
+            // to step out of it (by COLLISION_REPAIR_INCREMENT units) does not fix the collision.
+            if (++stepCount > COLLISION_REPAIR_MAX_ITERATIONS)
+		    {
+                WARN_VARG("Character '%s' could not recover from collision.", _node->getId());
+			    break;
+		    }
+	    }*/
+    }
 
     // Update current and target world positions
     btTransform transform = _ghostObject->getWorldTransform();
     _currentPosition = transform.getOrigin();
 
     // Process movement in the up direction
-    stepUp(collisionWorld, deltaTimeStep);
+    if (_physicsEnabled)
+        stepUp(collisionWorld, deltaTimeStep);
 
     // Process horizontal movement
     stepForwardAndStrafe(collisionWorld, deltaTimeStep);
 
     // Process movement in the down direction
-    stepDown(collisionWorld, deltaTimeStep);
+    if (_physicsEnabled)
+        stepDown(collisionWorld, deltaTimeStep);
 
     // Set new position
     transform.setOrigin(_currentPosition);
@@ -486,6 +501,13 @@ void PhysicsCharacter::stepForwardAndStrafe(btCollisionWorld* collisionWorld, fl
     // Translate the target position by the velocity vector (already scaled by t)
     btVector3 targetPosition = _currentPosition + velocity;
 
+    // If physics is disabled, simply update current position without checking collisions
+    if (!_physicsEnabled)
+    {
+        _currentPosition = targetPosition;
+        return;
+    }
+
     // Check for collisions by performing a bullet convex sweep test
     btTransform start, end;
 	start.setIdentity();

+ 24 - 0
gameplay/src/PhysicsCharacter.h

@@ -65,6 +65,29 @@ public:
      */
     Node* getNode() const;
 
+    /**
+     * Returns whether physics simulation is enabled for the physics character.
+     *
+     * @return true if physics simulation is enabled, false otherwise.
+     *
+     * @see setPhysicsEnabled(bool)
+     */
+    bool isPhysicsEnabled() const;
+
+    /**
+     * Enables or disables phyiscs simulation for the character.
+     *
+     * When physics simulation is enabled (default), the physics character automatically
+     * responds to collisions in the physics world. For example, the character will
+     * automatically slide along walls, step up stairs, react to gravity, etc.
+     *
+     * When disabled, the character will not have any physics applied on it and will
+     * therefore be allowed to walk through walls, ceiling, floors, other objects, etc.
+     *
+     * @param enabled true to enable physics simulation, false otherwise.
+     */
+    void setPhysicsEnabled(bool enabled);
+
     /**
      * Returns the maximum step height for the character.
      *
@@ -331,6 +354,7 @@ private:
     float _stepHeight;
     float _slopeAngle;
     float _cosSlopeAngle;
+    bool _physicsEnabled;
 };
 
 }