Procházet zdrojové kódy

Fixed bug with PhysicsController::rayTest where it was using incorrect ray vectors.
Removed the PhysicsRigidBody::Parameters::gravity member variable. Overriding gravity for a rigid body must now be done after creating the rigid body through the PhysicsRigidBody::setGravity() method.
Added a couple convenience methods to Node for retrieving world space basis vectors.

Steve Grenier před 13 roky
rodič
revize
4ca0d43d5c

+ 14 - 0
gameplay/src/Node.cpp

@@ -547,6 +547,20 @@ Vector3 Node::getForwardVectorView() const
     return vector;
 }
 
+Vector3 Node::getRightVectorWorld() const
+{
+    Vector3 vector;
+    getWorldMatrix().getRightVector(&vector);
+    return vector;
+}
+
+Vector3 Node::getUpVectorWorld() const
+{
+    Vector3 vector;
+    getWorldMatrix().getUpVector(&vector);
+    return vector;
+}
+
 Vector3 Node::getActiveCameraTranslationWorld() const
 {
     Scene* scene = getScene();

+ 24 - 0
gameplay/src/Node.h

@@ -341,14 +341,38 @@ public:
 
     /**
      * Returns the forward vector of the Node in world space.
+     *
+     * @return The forward vector in world space.
      */
     Vector3 getForwardVectorWorld() const;
 
     /**
      *  Returns the forward vector of the Node in view space.
+     *
+     * @param normalize True to return the vector normalized, false (default) otherwise.
+     *
+     * @return The forwward vector in view space.
      */
     Vector3 getForwardVectorView() const;
 
+    /**
+     * Returns the right vector of the Node in world space.
+     *
+     * @param normalize True to return the vector normalized, false (default) otherwise.
+     *
+     * @return The right vector in world space.
+     */
+    Vector3 getRightVectorWorld() const;
+
+    /**
+     * Returns the up vector of the Node in world space.
+     *
+     * @param normalize True to return the vector normalized, false (default) otherwise.
+     *
+     * @return The up vector in world space.
+     */
+    Vector3 getUpVectorWorld() const;
+
     /**
      * Returns the translation vector of the currently active camera for this node's scene.
      *

+ 5 - 2
gameplay/src/PhysicsController.cpp

@@ -158,8 +158,11 @@ bool PhysicsController::rayTest(const Ray& ray, float distance, PhysicsControlle
 {
     GP_ASSERT(_world);
 
-    btCollisionWorld::ClosestRayResultCallback callback(BV(ray.getOrigin()), BV(distance * ray.getDirection()));
-    _world->rayTest(BV(ray.getOrigin()), BV(distance * ray.getDirection()), callback);
+    btVector3 rayFromWorld(BV(ray.getOrigin()));
+    btVector3 rayToWorld(rayFromWorld + BV(ray.getDirection() * distance));
+
+    btCollisionWorld::ClosestRayResultCallback callback(rayFromWorld, rayToWorld);
+    _world->rayTest(rayFromWorld, rayToWorld, callback);
     if (callback.hasHit())
     {
         if (result)

+ 9 - 2
gameplay/src/PhysicsRigidBody.cpp

@@ -44,7 +44,6 @@ PhysicsRigidBody::PhysicsRigidBody(Node* node, const PhysicsCollisionShape::Defi
     // Set other initially defined properties.
     setKinematic(parameters.kinematic);
     setAnisotropicFriction(parameters.anisotropicFriction);
-    setGravity(parameters.gravity);
 
     // Add ourself to the physics world.
     Game::getInstance()->getPhysicsController()->addCollisionObject(this);
@@ -185,6 +184,7 @@ PhysicsRigidBody* PhysicsRigidBody::create(Node* node, Properties* properties)
 
     // Set the rigid body parameters to their defaults.
     Parameters parameters;
+    Vector3* gravity = NULL;
 
     // Load the defined rigid body parameters.
     properties->rewind();
@@ -221,7 +221,8 @@ PhysicsRigidBody* PhysicsRigidBody::create(Node* node, Properties* properties)
         }
         else if (strcmp(name, "gravity") == 0)
         {
-            properties->getVector3(NULL, &parameters.gravity);
+            gravity = new Vector3();
+            properties->getVector3(NULL, gravity);
         }
         else
         {
@@ -233,6 +234,12 @@ PhysicsRigidBody* PhysicsRigidBody::create(Node* node, Properties* properties)
     PhysicsRigidBody* body = new PhysicsRigidBody(node, *shape, parameters);
     SAFE_DELETE(shape);
 
+    if (gravity)
+    {
+        body->setGravity(*gravity);
+        SAFE_DELETE(gravity);
+    }
+
     return body;
 }
 

+ 9 - 5
gameplay/src/PhysicsRigidBody.h

@@ -72,18 +72,22 @@ public:
         Vector3 anisotropicFriction;
 
         /**
-         * The gravity acceleration factor for the rigid body.
+         * Constructor.
          */
-        Vector3 gravity;
+        Parameters() : mass(0.0f), friction(0.5f), restitution(0.0f),
+            linearDamping(0.0f), angularDamping(0.0f),
+            kinematic(false), anisotropicFriction(Vector3::one())
+        {
+        }
 
         /**
          * Constructor.
          */
-        Parameters(float mass = 0.0f, float friction = 0.5f, float resititution = 0.0f,
+        Parameters(float mass, float friction = 0.5f, float resititution = 0.0f,
             float linearDamping = 0.0f, float angularDamping = 0.0f, bool kinematic = false,
-            const Vector3& anisotropicFriction = Vector3::one(), const Vector3& gravity = Vector3::zero())
+            const Vector3& anisotropicFriction = Vector3::one())
             : mass(mass), friction(friction), restitution(restitution), linearDamping(linearDamping), angularDamping(angularDamping),
-              kinematic(kinematic), anisotropicFriction(anisotropicFriction), gravity(gravity)
+              kinematic(kinematic), anisotropicFriction(anisotropicFriction)
         {
         }
     };