소스 검색

Merge branch 'next' of https://github.com/blackberry-gaming/GamePlay into next-dgough

Darryl Gough 14 년 전
부모
커밋
249b3fc635
2개의 변경된 파일74개의 추가작업 그리고 46개의 파일을 삭제
  1. 42 26
      gameplay/src/PhysicsCharacter.cpp
  2. 32 20
      gameplay/src/PhysicsCharacter.h

+ 42 - 26
gameplay/src/PhysicsCharacter.cpp

@@ -54,9 +54,11 @@ protected:
 };
 };
 
 
 PhysicsCharacter::PhysicsCharacter(Node* node, float radius, float height, const Vector3 center)
 PhysicsCharacter::PhysicsCharacter(Node* node, float radius, float height, const Vector3 center)
-    : _node(node), _forwardVelocity(0.0f), _rightVelocity(0.0f), _fallVelocity(0, 0, 0),
-    _currentVelocity(0,0,0), _colliding(false), _ghostObject(NULL), _collisionShape(NULL),
-    _ignoreTransformChanged(0), _stepHeight(0.2f), _slopeAngle(0.0f), _cosSlopeAngle(0.0f)
+    : _node(node), _motionState(NULL), _moveVelocity(0,0,0), _forwardVelocity(0.0f), _rightVelocity(0.0f),
+    _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)
 {
 {
     setMaxSlopeAngle(45.0f);
     setMaxSlopeAngle(45.0f);
 
 
@@ -82,10 +84,10 @@ PhysicsCharacter::PhysicsCharacter(Node* node, float radius, float height, const
     _ghostObject->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT);
     _ghostObject->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT);
 
 
     btDynamicsWorld* world = Game::getInstance()->getPhysicsController()->_world;
     btDynamicsWorld* world = Game::getInstance()->getPhysicsController()->_world;
-
+    
     // Register the ghost object for collisions with the world.
     // Register the ghost object for collisions with the world.
     // For now specify static flag only, so character does not interact with dynamic objects
     // For now specify static flag only, so character does not interact with dynamic objects
-    world->addCollisionObject(_ghostObject, btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter | btBroadphaseProxy::DefaultFilter);
+    world->addCollisionObject(_ghostObject, btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter | btBroadphaseProxy::CharacterFilter | btBroadphaseProxy::DefaultFilter);
 
 
     // Register ourselves as an action on the physics world so we are called back during physics ticks
     // Register ourselves as an action on the physics world so we are called back during physics ticks
     world->addAction(this);
     world->addAction(this);
@@ -231,37 +233,38 @@ void PhysicsCharacter::play(CharacterAnimation* animation, unsigned int layer)
     }
     }
 }
 }
 
 
-void PhysicsCharacter::setVelocity(const Vector3& velocity, unsigned int flags)
+void PhysicsCharacter::setVelocity(const Vector3& velocity)
 {
 {
     _moveVelocity.setValue(velocity.x, velocity.y, velocity.z);
     _moveVelocity.setValue(velocity.x, velocity.y, velocity.z);
-    _moveFlags = flags;
+}
 
 
-    if (flags & MOVE_ROTATE)
-    {
-        Vector3 dn;
-        velocity.normalize(&dn);
+void PhysicsCharacter::rotate(const Vector3& axis, float angle)
+{
+    _node->rotate(axis, angle);
+}
 
 
-        float angle = std::acos(Vector3::dot(Vector3(0, 0, -1), dn));
-        _node->setRotation(Vector3::unitY(), angle);
-    }
+void PhysicsCharacter::rotate(const Quaternion& rotation)
+{
+    _node->rotate(rotation);
+}
 
 
-    updateCurrentVelocity();
+void PhysicsCharacter::setRotation(const Vector3& axis, float angle)
+{
+    _node->setRotation(axis, angle);
 }
 }
 
 
+void PhysicsCharacter::setRotation(const Quaternion& rotation)
+{
+    _node->setRotation(rotation);
+}
 void PhysicsCharacter::setForwardVelocity(float velocity)
 void PhysicsCharacter::setForwardVelocity(float velocity)
 {
 {
     _forwardVelocity = velocity;
     _forwardVelocity = velocity;
-    _moveFlags = MOVE_TRANSLATE;
-
-    updateCurrentVelocity();
 }
 }
 
 
 void PhysicsCharacter::setRightVelocity(float velocity)
 void PhysicsCharacter::setRightVelocity(float velocity)
 {
 {
     _rightVelocity = velocity;
     _rightVelocity = velocity;
-    _moveFlags = MOVE_TRANSLATE;
-
-    updateCurrentVelocity();
 }
 }
 
 
 void PhysicsCharacter::jump(float height)
 void PhysicsCharacter::jump(float height)
@@ -305,8 +308,15 @@ void PhysicsCharacter::updateCurrentVelocity()
     }
     }
 
 
     // Compute final combined movement vectors
     // Compute final combined movement vectors
-    _normalizedVelocity.normalize();
-    _currentVelocity = _normalizedVelocity * std::sqrt(velocity2);
+    if (_normalizedVelocity.isZero())
+    {
+        _currentVelocity.setZero();
+    }
+    else
+    {
+        _normalizedVelocity.normalize();
+        _currentVelocity = _normalizedVelocity * std::sqrt(velocity2);
+    }
 }
 }
 
 
 void PhysicsCharacter::transformChanged(Transform* transform, long cookie)
 void PhysicsCharacter::transformChanged(Transform* transform, long cookie)
@@ -382,8 +392,6 @@ void PhysicsCharacter::stepUp(btCollisionWorld* collisionWorld, btScalar time)
 
 
 void PhysicsCharacter::stepForwardAndStrafe(btCollisionWorld* collisionWorld, float time)
 void PhysicsCharacter::stepForwardAndStrafe(btCollisionWorld* collisionWorld, float time)
 {
 {
-    btVector3 targetPosition = _currentPosition;
-
     // Process currently playing movements+animations and determine final move location
     // Process currently playing movements+animations and determine final move location
     float animationMoveSpeed = 0.0f;
     float animationMoveSpeed = 0.0f;
     unsigned int animationCount = 0;
     unsigned int animationCount = 0;
@@ -415,6 +423,14 @@ void PhysicsCharacter::stepForwardAndStrafe(btCollisionWorld* collisionWorld, fl
         ++animationCount;
         ++animationCount;
     }
     }
 
 
+    updateCurrentVelocity();
+
+    if (_currentVelocity.isZero())
+    {
+        // No velocity, so we aren't moving
+        return;
+    }
+
     // Calculate final velocity
     // Calculate final velocity
     btVector3 velocity(_currentVelocity);
     btVector3 velocity(_currentVelocity);
     if (animationCount > 0)
     if (animationCount > 0)
@@ -424,7 +440,7 @@ void PhysicsCharacter::stepForwardAndStrafe(btCollisionWorld* collisionWorld, fl
     velocity *= time; // since velocity is in meters per second
     velocity *= time; // since velocity is in meters per second
 
 
     // Translate the target position by the velocity vector (already scaled by t)
     // Translate the target position by the velocity vector (already scaled by t)
-    targetPosition += velocity;
+    btVector3 targetPosition = _currentPosition + velocity;
 
 
     // Check for collisions by performing a bullet convex sweep test
     // Check for collisions by performing a bullet convex sweep test
     btTransform start, end;
     btTransform start, end;

+ 32 - 20
gameplay/src/PhysicsCharacter.h

@@ -53,22 +53,6 @@ public:
          ANIMATION_REPEAT
          ANIMATION_REPEAT
     };
     };
 
 
-    /**
-     * Flags controlling how a character is moved.
-     */
-    enum MoveFlags
-    {
-        /**
-         * Translates the character.
-         */
-        MOVE_TRANSLATE = 1,
-
-        /**
-         * Rotates the character.
-         */
-        MOVE_ROTATE = 2
-    };
-
     /**
     /**
      * Returns the character node for this PhysicsCharacter.
      * Returns the character node for this PhysicsCharacter.
      *
      *
@@ -170,7 +154,7 @@ public:
      * normalized (unit-length) velocity vector results in the character speed being
      * normalized (unit-length) velocity vector results in the character speed being
      * controled entirely by the current animation's velocity; whereas the speed of
      * controled entirely by the current animation's velocity; whereas the speed of
      * the character can be augmented by modifying the magnitude of the velocity vector.
      * the character can be augmented by modifying the magnitude of the velocity vector.
-     *
+
      * Note that a zero velocity vector and/or a zero animation move speed will
      * Note that a zero velocity vector and/or a zero animation move speed will
      * result in no character movement (the character will be stationary). A zero
      * result in no character movement (the character will be stationary). A zero
      * velocity vector should be used when playing an animation that targets the
      * velocity vector should be used when playing an animation that targets the
@@ -178,9 +162,38 @@ public:
      * animations will overwrite any transformations on the character's node.
      * animations will overwrite any transformations on the character's node.
      *
      *
      * @param velocity Movement velocity.
      * @param velocity Movement velocity.
-     * @param flags Optional bitwise combination of MoveFlags constants.
      */
      */
-    void setVelocity(const Vector3& velocity, unsigned int flags = MOVE_TRANSLATE | MOVE_ROTATE);
+    void setVelocity(const Vector3& velocity);
+
+    /**
+     * Rotates the character.
+     *
+     * @param axis Axis of rotation.
+     * @param angle Angle in radians.
+     */
+    void rotate(const Vector3& axis, float angle);
+
+    /**
+     * Rotates the character.
+     *
+     * @param rotation Quaternion representing the rotation to apply.
+     */
+    void rotate(const Quaternion& rotation);
+
+    /**
+     * Sets the rotation of the character.
+     *
+     * @param axis Axis of rotation.
+     * @param angle Angle in radians.
+     */
+    void setRotation(const Vector3& axis, float angle);
+
+    /**
+     * Sets the rotation of the character.
+     *
+     * @param rotation Quaternion representing the new rotation.
+     */
+    void setRotation(const Quaternion& rotation);
 
 
     /**
     /**
      * Moves the character forward with the given velocity vector.
      * Moves the character forward with the given velocity vector.
@@ -285,7 +298,6 @@ private:
     btVector3 _fallVelocity;
     btVector3 _fallVelocity;
     btVector3 _currentVelocity;
     btVector3 _currentVelocity;
     btVector3 _normalizedVelocity;
     btVector3 _normalizedVelocity;
-    unsigned int _moveFlags;
     bool _colliding;
     bool _colliding;
     btVector3 _collisionNormal;
     btVector3 _collisionNormal;
     btVector3 _currentPosition;
     btVector3 _currentPosition;