From 3ebb00fd685b9a291d824f6161b466fe6865436c Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Tue, 24 Nov 2015 07:03:02 +0100 Subject: [PATCH] Modifications in testbed for setScaling() method on objects --- src/collision/ProxyShape.h | 18 +++++++++++++++++- src/collision/shapes/CapsuleShape.h | 2 +- testbed/common/Capsule.cpp | 6 +++--- testbed/common/Dumbbell.cpp | 23 +++++++++++++++++++---- testbed/common/Dumbbell.h | 2 +- testbed/src/Scene.h | 2 +- 6 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/collision/ProxyShape.h b/src/collision/ProxyShape.h index 22dfa2e6..405e20e4 100644 --- a/src/collision/ProxyShape.h +++ b/src/collision/ProxyShape.h @@ -55,7 +55,7 @@ class ProxyShape { CollisionShape* mCollisionShape; /// Local-space to parent body-space transform (does not change over time) - const Transform mLocalToBodyTransform; + Transform mLocalToBodyTransform; /// Mass (in kilogramms) of the corresponding collision shape decimal mMass; @@ -122,6 +122,9 @@ class ProxyShape { /// Return the local to parent body transform const Transform& getLocalToBodyTransform() const; + /// Set the local to parent body transform + void setLocalToBodyTransform(const Transform& transform); + /// Return the local to world transform const Transform getLocalToWorldTransform() const; @@ -227,6 +230,17 @@ inline const Transform& ProxyShape::getLocalToBodyTransform() const { return mLocalToBodyTransform; } +// Set the local to parent body transform +inline void ProxyShape::setLocalToBodyTransform(const Transform& transform) { + + mLocalToBodyTransform = transform; + + mBody->setIsSleeping(false); + + // Notify the body that the proxy shape has to be updated in the broad-phase + mBody->updateProxyShapeInBroadPhase(this, true); +} + // Return the local to world transform /** * @return The transformation that transforms the local-space of the collision @@ -316,6 +330,8 @@ inline void ProxyShape::setLocalScaling(const Vector3& scaling) { // Set the local scaling of the collision shape mCollisionShape->setLocalScaling(scaling); + mBody->setIsSleeping(false); + // Notify the body that the proxy shape has to be updated in the broad-phase mBody->updateProxyShapeInBroadPhase(this, true); } diff --git a/src/collision/shapes/CapsuleShape.h b/src/collision/shapes/CapsuleShape.h index fb2270a8..efc38633 100644 --- a/src/collision/shapes/CapsuleShape.h +++ b/src/collision/shapes/CapsuleShape.h @@ -128,7 +128,7 @@ inline decimal CapsuleShape::getHeight() const { return mHalfHeight + mHalfHeight; } -/// Set the scaling vector of the collision shape +// Set the scaling vector of the collision shape inline void CapsuleShape::setLocalScaling(const Vector3& scaling) { mHalfHeight = (mHalfHeight / mScaling.y) * scaling.y; diff --git a/testbed/common/Capsule.cpp b/testbed/common/Capsule.cpp index 08e4d22c..dafdf784 100644 --- a/testbed/common/Capsule.cpp +++ b/testbed/common/Capsule.cpp @@ -47,7 +47,7 @@ Capsule::Capsule(float radius, float height, const openglframework::Vector3& pos // Compute the scaling matrix mScalingMatrix = openglframework::Matrix4(mRadius, 0, 0, 0, - 0, (mHeight + 2.0f * mRadius) / 3.0f, 0,0, + 0, (mHeight + 2.0f * mRadius) / 3, 0,0, 0, 0, mRadius, 0, 0, 0, 0, 1.0f); @@ -290,7 +290,7 @@ void Capsule::setScaling(const openglframework::Vector3& scaling) { // Scale the graphics object mScalingMatrix = openglframework::Matrix4(mRadius * scaling.x, 0, 0, 0, - 0, (mHeight + 2.0f * mRadius) / 3.0f * scaling.y, 0,0, - 0, 0, mRadius * scaling.z, 0, + 0, (mHeight * scaling.y + 2.0f * mRadius * scaling.x) / 3, 0,0, + 0, 0, mRadius * scaling.x, 0, 0, 0, 0, 1.0f); } diff --git a/testbed/common/Dumbbell.cpp b/testbed/common/Dumbbell.cpp index 4b6e863d..d7a232aa 100644 --- a/testbed/common/Dumbbell.cpp +++ b/testbed/common/Dumbbell.cpp @@ -47,6 +47,8 @@ Dumbbell::Dumbbell(const openglframework::Vector3 &position, // Identity scaling matrix mScalingMatrix.setToIdentity(); + mDistanceBetweenSphere = 8.0f; + // Initialize the position where the sphere will be rendered translateWorld(position); @@ -74,10 +76,10 @@ Dumbbell::Dumbbell(const openglframework::Vector3 &position, mPreviousTransform = transformBody; // Initial transform of the first sphere collision shape of the dumbbell (in local-space) - rp3d::Transform transformSphereShape1(rp3d::Vector3(0, 4.0, 0), rp3d::Quaternion::identity()); + rp3d::Transform transformSphereShape1(rp3d::Vector3(0, mDistanceBetweenSphere / 2.0f, 0), rp3d::Quaternion::identity()); // Initial transform of the second sphere collision shape of the dumbell (in local-space) - rp3d::Transform transformSphereShape2(rp3d::Vector3(0, -4.0, 0), rp3d::Quaternion::identity()); + rp3d::Transform transformSphereShape2(rp3d::Vector3(0, -mDistanceBetweenSphere / 2.0f, 0), rp3d::Quaternion::identity()); // Initial transform of the cylinder collision shape of the dumbell (in local-space) rp3d::Transform transformCylinderShape(rp3d::Vector3(0, 0, 0), rp3d::Quaternion::identity()); @@ -116,6 +118,8 @@ Dumbbell::Dumbbell(const openglframework::Vector3 &position, // Identity scaling matrix mScalingMatrix.setToIdentity(); + mDistanceBetweenSphere = 8.0f; + // Initialize the position where the sphere will be rendered translateWorld(position); @@ -139,10 +143,10 @@ Dumbbell::Dumbbell(const openglframework::Vector3 &position, rp3d::Transform transformBody(initPosition, initOrientation); // Initial transform of the first sphere collision shape of the dumbbell (in local-space) - rp3d::Transform transformSphereShape1(rp3d::Vector3(0, 4.0, 0), rp3d::Quaternion::identity()); + rp3d::Transform transformSphereShape1(rp3d::Vector3(0, mDistanceBetweenSphere / 2.0f, 0), rp3d::Quaternion::identity()); // Initial transform of the second sphere collision shape of the dumbell (in local-space) - rp3d::Transform transformSphereShape2(rp3d::Vector3(0, -4.0, 0), rp3d::Quaternion::identity()); + rp3d::Transform transformSphereShape2(rp3d::Vector3(0, -mDistanceBetweenSphere / 2.0f, 0), rp3d::Quaternion::identity()); // Initial transform of the cylinder collision shape of the dumbell (in local-space) rp3d::Transform transformCylinderShape(rp3d::Vector3(0, 0, 0), rp3d::Quaternion::identity()); @@ -325,6 +329,17 @@ void Dumbbell::setScaling(const openglframework::Vector3& scaling) { mProxyShapeSphere1->setLocalScaling(newScaling); mProxyShapeSphere2->setLocalScaling(newScaling); + mDistanceBetweenSphere = (mDistanceBetweenSphere / mScalingMatrix.getValue(1, 1)) * scaling.y; + + // Initial transform of the first sphere collision shape of the dumbbell (in local-space) + rp3d::Transform transformSphereShape1(rp3d::Vector3(0, mDistanceBetweenSphere / 2.0f, 0), rp3d::Quaternion::identity()); + + // Initial transform of the second sphere collision shape of the dumbell (in local-space) + rp3d::Transform transformSphereShape2(rp3d::Vector3(0, -mDistanceBetweenSphere / 2.0f, 0), rp3d::Quaternion::identity()); + + mProxyShapeSphere1->setLocalToBodyTransform(transformSphereShape1); + mProxyShapeSphere2->setLocalToBodyTransform(transformSphereShape2); + // Scale the graphics object mScalingMatrix = openglframework::Matrix4(scaling.x, 0, 0, 0, 0, scaling.y, 0, 0, diff --git a/testbed/common/Dumbbell.h b/testbed/common/Dumbbell.h index 742cf1c2..b2d211b1 100644 --- a/testbed/common/Dumbbell.h +++ b/testbed/common/Dumbbell.h @@ -39,7 +39,7 @@ class Dumbbell : public openglframework::Mesh, public PhysicsObject { // -------------------- Attributes -------------------- // /// Radius of the spheres - float mRadius; + float mDistanceBetweenSphere; /// Collision shapes rp3d::CylinderShape* mCylinderShape; diff --git a/testbed/src/Scene.h b/testbed/src/Scene.h index 2091caac..c995d056 100644 --- a/testbed/src/Scene.h +++ b/testbed/src/Scene.h @@ -47,7 +47,7 @@ struct EngineSettings { public: - float elapsedTime; // Elapsed time (in seconds) + long double elapsedTime; // Elapsed time (in seconds) float timeStep; // Current time step (in seconds) int nbVelocitySolverIterations; // Nb of velocity solver iterations int nbPositionSolverIterations; // Nb of position solver iterations