From: <a-...@us...> - 2009-12-01 09:08:25
|
Revision: 111 http://simspark.svn.sourceforge.net/simspark/?rev=111&view=rev Author: a-held Date: 2009-12-01 09:08:17 +0000 (Tue, 01 Dec 2009) Log Message: ----------- Tackled bridge pattern for RigidBody class Modified Paths: -------------- branches/multiphys/spark/lib/oxygen/physicsserver/int/rigidbodyint.h branches/multiphys/spark/lib/oxygen/physicsserver/ode/oderigidbody.cpp branches/multiphys/spark/lib/oxygen/physicsserver/ode/oderigidbody.h branches/multiphys/spark/lib/oxygen/physicsserver/rigidbody.cpp branches/multiphys/spark/lib/oxygen/physicsserver/rigidbody.h Modified: branches/multiphys/spark/lib/oxygen/physicsserver/int/rigidbodyint.h =================================================================== --- branches/multiphys/spark/lib/oxygen/physicsserver/int/rigidbodyint.h 2009-11-30 08:31:04 UTC (rev 110) +++ branches/multiphys/spark/lib/oxygen/physicsserver/int/rigidbodyint.h 2009-12-01 09:08:17 UTC (rev 111) @@ -25,6 +25,7 @@ #include <oxygen/oxygen_defines.h> #include <oxygen/physicsserver/ode/odebody.h> +#include <iostream> namespace oxygen { @@ -42,6 +43,7 @@ virtual bool IsEnabled() const = 0; virtual void UseGravity(bool f) = 0; virtual bool UsesGravity() const = 0; + virtual void CreateBody(long world) = 0; virtual void SetMass(float mass) = 0; virtual void SetMassParameters(const dMass& mass) = 0; virtual float GetMass() const = 0; @@ -74,28 +76,72 @@ virtual void SetPosition(const salt::Vector3f& pos) = 0; virtual salt::Vector3f GetPosition() const = 0; virtual void DestroyPhysicsObject() = 0; - virtual void SynchronizeParent() const = 0; + virtual salt::Matrix GetSynchronisationMatrix() = 0; virtual void AddMass(const dMass& mass, const salt::Matrix& matrix) = 0; //virtual salt::Vector3f GetMassCenter() const = 0; + virtual void BodySetData(RigidBody* rb) = 0; + virtual RigidBody* BodyGetData(long bodyID) = 0; protected: - virtual void OnLink() = 0; - virtual bool CreateBody() = 0; + /** sets up an ode mass struct representing a box of the given + size and total_mass + */ virtual void PrepareBoxTotal(dMass& mass, float total_mass, const salt::Vector3f& size) const = 0; + + /** sets up an ode mass struct representing a box of the given + density and size + */ virtual void PrepareBox(dMass& mass, float density, const salt::Vector3f& size) const = 0; + + /** sets up an ode mass struct representing a sphere of the given + density and radius + */ virtual void PrepareSphere(dMass& mass, float density, float radius) const = 0; + + /** sets up an ode mass struct representing a sphere of the given + radius and total_mass + */ virtual void PrepareSphereTotal(dMass& mass, float total_mass, float radius) const = 0; + + /** sets up an ode mass struct representing a flat-ended cylinder + of the given parameters and density, with the center of mass + at (0,0,0) relative to the body. The radius of the cylinder is + radius. The length of the cylinder is length. The cylinder's + long axis is oriented along the body's z axis. + */ virtual void PrepareCylinder(dMass& mass, float density, float radius, float length) const = 0; + + /** sets up an ode mass struct representing a flat-ended cylinder + of the given parameters and total mass, with the center of + mass at (0,0,0) relative to the body. The radius of the + cylinder is radius. The length of the cylinder is length. The + cylinder's long axis is oriented along the body's z axis. + */ virtual void PrepareCylinderTotal(dMass& mass, float total_mass, float radius, float length) const = 0; + + /* sets up an ode mass struct representing a capsule of + the given parameters and density, with the center of mass at + (0,0,0) relative to the body. The radius of the capsule (and + the spherical cap) is radius. The length of the capsule (not + counting the spherical cap) is length. The capsule's long axis + is oriented along the body's z axis. + */ virtual void PrepareCapsule(dMass& mass, float density, float radius, float length) const = 0; + + /* sets up an ode mass struct representing a capsule of + the given parameters and total mass, with the center of mass at + (0,0,0) relative to the body. The radius of the capsule (and + the spherical cap) is radius. The length of the capsule (not + counting the spherical cap) is length. The capsule's long axis + is oriented along the body's z axis. + */ virtual void PrepareCapsuleTotal(dMass& mass, float total_mass, float radius, float length) const = 0; private: virtual void PrePhysicsUpdateInternal(float deltaTime) = 0; - virtual void PostPhysicsUpdateInternal() = 0; -protected: - dBodyID mODEBody; +public: + long mBodyID; salt::Vector3f mMassTrans; bool mMassTransformed; }; Modified: branches/multiphys/spark/lib/oxygen/physicsserver/ode/oderigidbody.cpp =================================================================== --- branches/multiphys/spark/lib/oxygen/physicsserver/ode/oderigidbody.cpp 2009-11-30 08:31:04 UTC (rev 110) +++ branches/multiphys/spark/lib/oxygen/physicsserver/ode/oderigidbody.cpp 2009-12-01 09:08:17 UTC (rev 111) @@ -32,8 +32,8 @@ using namespace salt; using namespace std; -ODERigidBody::ODERigidBody() : RigidBodyInt(){ - +ODERigidBody::ODERigidBody() : RigidBodyInt(), mMassTrans(0,0,0), mMassTransformed(false){ + mBodyID = 0; } ODERigidBody::~ODERigidBody(){ @@ -42,22 +42,22 @@ dBodyID ODERigidBody::GetODEBody() const { - return mODEBody; + return (dBodyID)mBodyID; } void ODERigidBody::Enable() { - dBodyEnable(mODEBody); + dBodyEnable((dBodyID)mBodyID); } void ODERigidBody::Disable() { - dBodyDisable(mODEBody); + dBodyDisable((dBodyID)mBodyID); } bool ODERigidBody::IsEnabled() const { - return (dBodyIsEnabled(mODEBody) != 0); + return (dBodyIsEnabled((dBodyID)mBodyID) != 0); } void ODERigidBody::UseGravity(bool f) @@ -65,99 +65,105 @@ if (f == true) { // body is affected by gravity - dBodySetGravityMode(mODEBody, 1); + dBodySetGravityMode((dBodyID)mBodyID, 1); } else { // body is not affected by gravity - dBodySetGravityMode(mODEBody, 0); + dBodySetGravityMode((dBodyID)mBodyID, 0); } } bool ODERigidBody::UsesGravity() const { - return (dBodyGetGravityMode(mODEBody) != 0); + return (dBodyGetGravityMode((dBodyID)mBodyID) != 0); } -bool ODERigidBody::CreateBody() +void ODERigidBody::CreateBody(long world) { - if (mODEBody != 0) + if (mBodyID != 0) { - return true; + return; } - dWorldID world = GetWorldID(); - if (world == 0) - { - return false; - } - // create the managed body - mODEBody = dBodyCreate(world); - if (mODEBody == 0) - { - GetLog()->Error() - << "(Body) ERROR: could not create new ODE body\n"; - return false; - } - - return true; + mBodyID = (long) dBodyCreate( (dWorldID) world); } void ODERigidBody::DestroyPhysicsObject() { - if (mODEBody == 0) + if (mBodyID == 0) { return; } - dBodyDestroy(mODEBody); - mODEBody = 0; + dBodyDestroy((dBodyID)mBodyID); + mBodyID = 0; } -void ODERigidBody::OnLink() +void ODERigidBody::BodySetData(RigidBody* rb) { - ODEPhysicsObject::OnLink(); - - if (! CreateBody()) - { - return; - } - - // let the body, take on the world space position of the parent - dBodySetData(mODEBody, this); - - shared_ptr<BaseNode> baseNode = shared_static_cast<BaseNode> - (GetParent().lock()); - - const Matrix& mat = baseNode->GetWorldTransform(); - SetRotation(mat); - SetPosition(mat.Pos()); + dBodySetData((dBodyID)mBodyID, rb); } void ODERigidBody::SetMass(float mass) { dMass ODEMass; - dBodyGetMass(mODEBody, &ODEMass); + dBodyGetMass((dBodyID)mBodyID, &ODEMass); dMassAdjust(&ODEMass, mass); - dBodySetMass(mODEBody, &ODEMass); + dBodySetMass((dBodyID)mBodyID, &ODEMass); } float ODERigidBody::GetMass() const { dMass m; - dBodyGetMass(mODEBody, &m); + dBodyGetMass((dBodyID)mBodyID, &m); return m.mass; } +void ODERigidBody::AddMass(const dMass& mass, const Matrix& matrix) +{ + dMass transMass(mass); + + dMatrix3 rot; + ConvertRotationMatrix(matrix, rot); + dMassRotate(&transMass,rot); + + const Vector3f& trans(matrix.Pos()); + dMassTranslate(&transMass,trans[0],trans[1],trans[2]); + + dMassTranslate(&transMass,mMassTrans[0],mMassTrans[1],mMassTrans[2]); + + dMass bodyMass; + dBodyGetMass((dBodyID)mBodyID, &bodyMass); + dMassAdd(&bodyMass, &transMass); + + /** ODE currently requires that the center mass is always in the + origin of the body + */ + Vector3f trans2(bodyMass.c[0], bodyMass.c[1], bodyMass.c[2]); + + dMassTranslate(&bodyMass, -trans2[0], -trans2[1], -trans2[2]); + bodyMass.c[0] = bodyMass.c[1] = bodyMass.c[2] = 0.0f; + dBodySetMass((dBodyID)mBodyID, (const dMass*)&bodyMass); + + // Move body so mass is at right position again + SetPosition(GetPosition() + trans2); + + // Keep track of total translation of mass + mMassTrans = mMassTrans - trans2; + + mMassTransformed = true; +} + void ODERigidBody::GetMassParameters(dMass& mass) const { - dBodyGetMass(mODEBody, &mass); + dBodyGetMass((dBodyID)mBodyID, &mass); } void ODERigidBody::SetMassParameters(const dMass& mass) { - dBodySetMass(mODEBody, &mass); + dBodySetMass((dBodyID)mBodyID, &mass); } void ODERigidBody::PrepareSphere(dMass& mass, float density, float radius) const @@ -169,7 +175,7 @@ { dMass ODEMass; PrepareSphere(ODEMass, density, radius); - dBodySetMass(mODEBody, &ODEMass); + dBodySetMass((dBodyID)mBodyID, &ODEMass); } void ODERigidBody::AddSphere(float density, float radius, const Matrix& matrix) @@ -188,7 +194,7 @@ { dMass ODEMass; PrepareSphereTotal(ODEMass, total_mass, radius); - dBodySetMass(mODEBody, &ODEMass); + dBodySetMass((dBodyID)mBodyID, &ODEMass); } void ODERigidBody::AddSphereTotal(float total_mass, float radius, const Matrix& matrix) @@ -207,7 +213,7 @@ { dMass ODEMass; PrepareBox(ODEMass, density, size); - dBodySetMass(mODEBody, &ODEMass); + dBodySetMass((dBodyID)mBodyID, &ODEMass); } void ODERigidBody::AddBox(float density, const Vector3f& size, const Matrix& matrix) @@ -226,7 +232,7 @@ { dMass ODEMass; PrepareBoxTotal(ODEMass, total_mass, size); - dBodySetMass(mODEBody, &ODEMass); + dBodySetMass((dBodyID)mBodyID, &ODEMass); } void ODERigidBody::AddBoxTotal(float total_mass, const Vector3f& size, const Matrix& matrix) @@ -236,41 +242,6 @@ AddMass(ODEMass, matrix); } -void ODERigidBody::AddMass(const dMass& mass, const Matrix& matrix) -{ - dMass transMass(mass); - - dMatrix3 rot; - ConvertRotationMatrix(matrix, rot); - dMassRotate(&transMass,rot); - - const Vector3f& trans(matrix.Pos()); - dMassTranslate(&transMass,trans[0],trans[1],trans[2]); - - dMassTranslate(&transMass,mMassTrans[0],mMassTrans[1],mMassTrans[2]); - - dMass bodyMass; - dBodyGetMass(mODEBody, &bodyMass); - dMassAdd(&bodyMass, &transMass); - - /** ODE currently requires that the center mass is always in the - origin of the body - */ - Vector3f trans2(bodyMass.c[0], bodyMass.c[1], bodyMass.c[2]); - - dMassTranslate(&bodyMass, -trans2[0], -trans2[1], -trans2[2]); - bodyMass.c[0] = bodyMass.c[1] = bodyMass.c[2] = 0.0f; - dBodySetMass(mODEBody, (const dMass*)&bodyMass); - - // Move body so mass is at right position again - SetPosition(GetPosition() + trans2); - - // Keep track of total translation of mass - mMassTrans = mMassTrans - trans2; - - mMassTransformed = true; -} - void ODERigidBody::PrepareCylinder (dMass& mass, float density, float radius, float length) const { // direction: (1=x, 2=y, 3=z) @@ -283,7 +254,7 @@ { dMass ODEMass; PrepareCylinder(ODEMass, density, radius, length); - dBodySetMass(mODEBody, &ODEMass); + dBodySetMass((dBodyID)mBodyID, &ODEMass); } void ODERigidBody::AddCylinder (float density, float radius, float length, const Matrix& matrix) @@ -305,7 +276,7 @@ { dMass ODEMass; PrepareCylinderTotal(ODEMass, total_mass, radius, length); - dBodySetMass(mODEBody, &ODEMass); + dBodySetMass((dBodyID)mBodyID, &ODEMass); } void ODERigidBody::AddCylinderTotal(float total_mass, float radius, float length, const Matrix& matrix) @@ -327,7 +298,7 @@ { dMass ODEMass; PrepareCapsule(ODEMass, density, radius, length); - dBodySetMass(mODEBody, &ODEMass); + dBodySetMass((dBodyID)mBodyID, &ODEMass); } void ODERigidBody::AddCapsule (float density, float radius, float length, const Matrix& matrix) @@ -349,7 +320,7 @@ { dMass ODEMass; PrepareCapsuleTotal(ODEMass, total_mass, radius, length); - dBodySetMass(mODEBody, &ODEMass); + dBodySetMass((dBodyID)mBodyID, &ODEMass); } void ODERigidBody::AddCapsuleTotal(float total_mass, float radius, float length, const salt::Matrix& matrix) @@ -361,25 +332,25 @@ Vector3f ODERigidBody::GetVelocity() const { - const dReal* vel = dBodyGetLinearVel(mODEBody); + const dReal* vel = dBodyGetLinearVel((dBodyID)mBodyID); return Vector3f(vel[0], vel[1], vel[2]); } void ODERigidBody::SetVelocity(const Vector3f& vel) { - dBodySetLinearVel(mODEBody, vel[0], vel[1], vel[2]); + dBodySetLinearVel((dBodyID)mBodyID, vel[0], vel[1], vel[2]); } void ODERigidBody::SetRotation(const Matrix& rot) { dMatrix3 m; ConvertRotationMatrix(rot,m); - dBodySetRotation(mODEBody,m); + dBodySetRotation((dBodyID)mBodyID,m); } salt::Matrix ODERigidBody::GetRotation() const { - const dReal* m = dBodyGetRotation(mODEBody); + const dReal* m = dBodyGetRotation((dBodyID)mBodyID); salt::Matrix rot; ConvertRotationMatrix(m,rot); return rot; @@ -387,23 +358,19 @@ Vector3f ODERigidBody::GetAngularVelocity() const { - const dReal* vel = dBodyGetAngularVel(mODEBody); + const dReal* vel = dBodyGetAngularVel((dBodyID)mBodyID); return Vector3f(vel[0], vel[1], vel[2]); } void ODERigidBody::SetAngularVelocity(const Vector3f& vel) { - dBodySetAngularVel(mODEBody, vel[0], vel[1], vel[2]); + dBodySetAngularVel((dBodyID)mBodyID, vel[0], vel[1], vel[2]); } -void ODERigidBody::SynchronizeParent() const +salt::Matrix ODERigidBody::GetSynchronisationMatrix() { - const dReal* pos = dBodyGetPosition(mODEBody); - const dReal* rot = dBodyGetRotation(mODEBody); - - shared_ptr<BaseNode> baseNode = shared_static_cast<BaseNode> - (GetParent().lock()); - + const dReal* pos = dBodyGetPosition((dBodyID)mBodyID); + const dReal* rot = dBodyGetRotation((dBodyID)mBodyID); Matrix mat; mat.m[0] = rot[0]; @@ -423,7 +390,7 @@ mat.m[14] = pos[2]; mat.m[15] = 1; - baseNode->SetWorldTransform(mat); + return mat; } void ODERigidBody::PrePhysicsUpdateInternal(float /*deltaTime*/) @@ -465,68 +432,39 @@ } } - -void ODERigidBody::PostPhysicsUpdateInternal() +RigidBody* ODERigidBody::BodyGetData(long bodyID) { - SynchronizeParent(); -} - -shared_ptr<RigidBody> ODERigidBody::GetBody(dBodyID id) -{ - if (id == 0) - { - return shared_ptr<RigidBody>(); - } - RigidBody* bodyPtr = - static_cast<RigidBody*>(dBodyGetData(id)); + static_cast<RigidBody*>(dBodyGetData( (dBodyID) bodyID)); - if (bodyPtr == 0) - { - // we cannot use the logserver here - cerr << "ERROR: (ODERigidBody) no body found for dBodyID " - << id << "\n"; - return shared_ptr<RigidBody>(); - } - - shared_ptr<RigidBody> body = shared_static_cast<RigidBody> - (bodyPtr->GetSelf().lock()); - - if (body.get() == 0) - { - // we cannot use the logserver here - cerr << "ERROR: (ODERigidBody) got no shared_ptr for dBodyID " - << id << "\n"; - } - - return body; + return bodyPtr; } void ODERigidBody::AddForce(const Vector3f& force) { - dBodyAddForce(mODEBody, force.x(), force.y(), force.z()); + dBodyAddForce((dBodyID)mBodyID, force.x(), force.y(), force.z()); } void ODERigidBody::AddTorque(const Vector3f& torque) { - dBodyAddTorque(mODEBody, torque.x(), torque.y(), torque.z()); + dBodyAddTorque((dBodyID)mBodyID, torque.x(), torque.y(), torque.z()); } void ODERigidBody::SetPosition(const Vector3f& pos) { - dBodySetPosition(mODEBody, pos.x(), pos.y(), pos.z()); + dBodySetPosition((dBodyID)mBodyID, pos.x(), pos.y(), pos.z()); // the parent node will be updated in the next physics cycle } Vector3f ODERigidBody::GetPosition() const { - const dReal* pos = dBodyGetPosition(mODEBody); + const dReal* pos = dBodyGetPosition((dBodyID)mBodyID); return Vector3f(pos[0], pos[1], pos[2]); } void ODERigidBody::TranslateMass(const Vector3f& v) { dMass m; - dBodyGetMass(mODEBody, &m); + dBodyGetMass((dBodyID)mBodyID, &m); dMassTranslate(&m,v[0],v[1],v[2]); } Modified: branches/multiphys/spark/lib/oxygen/physicsserver/ode/oderigidbody.h =================================================================== --- branches/multiphys/spark/lib/oxygen/physicsserver/ode/oderigidbody.h 2009-11-30 08:31:04 UTC (rev 110) +++ branches/multiphys/spark/lib/oxygen/physicsserver/ode/oderigidbody.h 2009-12-01 09:08:17 UTC (rev 111) @@ -43,6 +43,7 @@ void SetMassParameters(const dMass& mass); float GetMass() const; void GetMassParameters(dMass& mass) const; + void AddMass(const dMass& mass, const salt::Matrix& matrix); void SetSphere(float density, float radius); void AddSphere(float density, float radius, const salt::Matrix& matrix); void SetSphereTotal(float total_mass, float radius); @@ -71,14 +72,13 @@ void SetPosition(const salt::Vector3f& pos); salt::Vector3f GetPosition() const; void DestroyPhysicsObject(); - void SynchronizeParent() const; - void AddMass(const dMass& mass, const salt::Matrix& matrix); + salt::Matrix GetSynchronisationMatrix(); //salt::Vector3f GetMassCenter() const; - boost::shared_ptr<RigidBody> GetBody(dBodyID id); //static in rigidbody.h + void BodySetData(RigidBody* rb); + RigidBody* BodyGetData(long bodyID); protected: - void OnLink(); - bool CreateBody(); + void CreateBody(long world); void PrepareBoxTotal(dMass& mass, float total_mass, const salt::Vector3f& size) const; void PrepareBox(dMass& mass, float density, const salt::Vector3f& size) const; void PrepareSphere(dMass& mass, float density, float radius) const; @@ -90,10 +90,8 @@ private: void PrePhysicsUpdateInternal(float deltaTime); - void PostPhysicsUpdateInternal(); protected: - dBodyID mODEBody; salt::Vector3f mMassTrans; bool mMassTransformed; }; Modified: branches/multiphys/spark/lib/oxygen/physicsserver/rigidbody.cpp =================================================================== --- branches/multiphys/spark/lib/oxygen/physicsserver/rigidbody.cpp 2009-11-30 08:31:04 UTC (rev 110) +++ branches/multiphys/spark/lib/oxygen/physicsserver/rigidbody.cpp 2009-12-01 09:08:17 UTC (rev 111) @@ -41,59 +41,54 @@ dBodyID RigidBody::GetODEBody() const { - return mODEBody; + return (dBodyID) mRigidBodyImp->mBodyID; } void RigidBody::Enable() { - dBodyEnable(mODEBody); + mRigidBodyImp->Enable(); } void RigidBody::Disable() { - dBodyDisable(mODEBody); + mRigidBodyImp->Disable(); } bool RigidBody::IsEnabled() const { - return (dBodyIsEnabled(mODEBody) != 0); + return mRigidBodyImp->IsEnabled(); } void RigidBody::UseGravity(bool f) { - if (f == true) - { - // body is affected by gravity - dBodySetGravityMode(mODEBody, 1); - } - else - { - // body is not affected by gravity - dBodySetGravityMode(mODEBody, 0); - } + mRigidBodyImp->UseGravity(f); } bool RigidBody::UsesGravity() const { - return (dBodyGetGravityMode(mODEBody) != 0); + return mRigidBodyImp->UsesGravity(); } bool RigidBody::CreateBody() { - if (mODEBody != 0) + long bodyID = mRigidBodyImp->mBodyID; + + if (bodyID != 0) { return true; } - dWorldID world = GetWorldID(); + long world = (long) GetWorldID(); if (world == 0) { return false; } - - // create the managed body - mODEBody = dBodyCreate(world); - if (mODEBody == 0) + + mRigidBodyImp->CreateBody(world); + + mODEBody = (dBodyID) mRigidBodyImp->mBodyID; + + if (mRigidBodyImp->mBodyID == 0) { GetLog()->Error() << "(Body) ERROR: could not create new ODE body\n"; @@ -105,13 +100,7 @@ void RigidBody::DestroyPhysicsObject() { - if (mODEBody == 0) - { - return; - } - - dBodyDestroy(mODEBody); - mODEBody = 0; + mRigidBodyImp->DestroyPhysicsObject(); } void RigidBody::OnLink() @@ -124,31 +113,31 @@ } // let the body, take on the world space position of the parent - dBodySetData(mODEBody, this); + mRigidBodyImp->BodySetData(this); shared_ptr<BaseNode> baseNode = shared_static_cast<BaseNode> (GetParent().lock()); const Matrix& mat = baseNode->GetWorldTransform(); - SetRotation(mat); - SetPosition(mat.Pos()); + mRigidBodyImp->SetRotation(mat); + mRigidBodyImp->SetPosition(mat.Pos()); } void RigidBody::SetMass(float mass) { - dMass ODEMass; - dBodyGetMass(mODEBody, &ODEMass); - dMassAdjust(&ODEMass, mass); - dBodySetMass(mODEBody, &ODEMass); + mRigidBodyImp->SetMass(mass); } float RigidBody::GetMass() const { - dMass m; - dBodyGetMass(mODEBody, &m); - return m.mass; + return mRigidBodyImp->GetMass(); } +void RigidBody::AddMass(const dMass& mass, const Matrix& matrix) +{ + mRigidBodyImp->AddMass(mass, matrix); +} + void RigidBody::GetMassParameters(dMass& mass) const { dBodyGetMass(mODEBody, &mass); @@ -159,268 +148,122 @@ dBodySetMass(mODEBody, &mass); } -void RigidBody::PrepareSphere(dMass& mass, float density, float radius) const -{ - dMassSetSphere(&mass, density, radius); -} - void RigidBody::SetSphere(float density, float radius) { - dMass ODEMass; - PrepareSphere(ODEMass, density, radius); - dBodySetMass(mODEBody, &ODEMass); + mRigidBodyImp->SetSphere(density, radius); } void RigidBody::AddSphere(float density, float radius, const Matrix& matrix) { - dMass ODEMass; - PrepareSphere(ODEMass, density, radius); - AddMass(ODEMass, matrix); + mRigidBodyImp->AddSphere(density, radius, matrix); } -void RigidBody::PrepareSphereTotal(dMass& mass, float total_mass, float radius) const -{ - dMassSetSphereTotal(&mass, total_mass, radius); -} - void RigidBody::SetSphereTotal(float total_mass, float radius) { - dMass ODEMass; - PrepareSphereTotal(ODEMass, total_mass, radius); - dBodySetMass(mODEBody, &ODEMass); + mRigidBodyImp->SetSphereTotal(total_mass, radius); } void RigidBody::AddSphereTotal(float total_mass, float radius, const Matrix& matrix) { - dMass ODEMass; - PrepareSphereTotal(ODEMass, total_mass, radius); - AddMass(ODEMass, matrix); + mRigidBodyImp->AddSphereTotal(total_mass, radius, matrix); } -void RigidBody::PrepareBox(dMass& mass, float density, const Vector3f& size) const -{ - dMassSetBox(&mass, density, size[0], size[1], size[2]); -} - void RigidBody::SetBox(float density, const Vector3f& size) { - dMass ODEMass; - PrepareBox(ODEMass, density, size); - dBodySetMass(mODEBody, &ODEMass); + mRigidBodyImp->SetBox(density, size); } void RigidBody::AddBox(float density, const Vector3f& size, const Matrix& matrix) { - dMass ODEMass; - PrepareBox(ODEMass, density, size); - AddMass(ODEMass, matrix); + mRigidBodyImp->AddBox(density, size, matrix); } -void RigidBody::PrepareBoxTotal(dMass& mass, float total_mass, const Vector3f& size) const -{ - dMassSetBoxTotal(&mass, total_mass, size[0], size[1], size[2]); -} - void RigidBody::SetBoxTotal(float total_mass, const Vector3f& size) { - dMass ODEMass; - PrepareBoxTotal(ODEMass, total_mass, size); - dBodySetMass(mODEBody, &ODEMass); + mRigidBodyImp->SetBoxTotal(total_mass, size); } void RigidBody::AddBoxTotal(float total_mass, const Vector3f& size, const Matrix& matrix) { - dMass ODEMass; - PrepareBoxTotal(ODEMass, total_mass, size); - AddMass(ODEMass, matrix); + mRigidBodyImp->AddBoxTotal(total_mass, size, matrix); } -void RigidBody::AddMass(const dMass& mass, const Matrix& matrix) -{ - dMass transMass(mass); - - dMatrix3 rot; - ConvertRotationMatrix(matrix, rot); - dMassRotate(&transMass,rot); - - const Vector3f& trans(matrix.Pos()); - dMassTranslate(&transMass,trans[0],trans[1],trans[2]); - - dMassTranslate(&transMass,mMassTrans[0],mMassTrans[1],mMassTrans[2]); - - dMass bodyMass; - dBodyGetMass(mODEBody, &bodyMass); - dMassAdd(&bodyMass, &transMass); - - /** ODE currently requires that the center mass is always in the - origin of the body - */ - Vector3f trans2(bodyMass.c[0], bodyMass.c[1], bodyMass.c[2]); - - dMassTranslate(&bodyMass, -trans2[0], -trans2[1], -trans2[2]); - bodyMass.c[0] = bodyMass.c[1] = bodyMass.c[2] = 0.0f; - dBodySetMass(mODEBody, (const dMass*)&bodyMass); - - // Move body so mass is at right position again - SetPosition(GetPosition() + trans2); - - // Keep track of total translation of mass - mMassTrans = mMassTrans - trans2; - - mMassTransformed = true; -} - -void RigidBody::PrepareCylinder (dMass& mass, float density, float radius, float length) const -{ - // direction: (1=x, 2=y, 3=z) - int direction = 3; - - dMassSetCylinder (&mass, density, direction, radius, length); -} - void RigidBody::SetCylinder (float density, float radius, float length) { - dMass ODEMass; - PrepareCylinder(ODEMass, density, radius, length); - dBodySetMass(mODEBody, &ODEMass); + mRigidBodyImp->SetCylinder(density, radius, length); } void RigidBody::AddCylinder (float density, float radius, float length, const Matrix& matrix) { - dMass ODEMass; - PrepareCylinder(ODEMass, density, radius, length); - AddMass(ODEMass, matrix); + mRigidBodyImp->AddCylinder(density, radius, length, matrix); } -void RigidBody::PrepareCylinderTotal(dMass& mass, float total_mass, float radius, float length) const -{ - // direction: (1=x, 2=y, 3=z) - int direction = 3; - - dMassSetCylinderTotal(&mass, total_mass, direction, radius, length); -} - void RigidBody::SetCylinderTotal(float total_mass, float radius, float length) { - dMass ODEMass; - PrepareCylinderTotal(ODEMass, total_mass, radius, length); - dBodySetMass(mODEBody, &ODEMass); + mRigidBodyImp->SetCylinderTotal(total_mass, radius, length); } void RigidBody::AddCylinderTotal(float total_mass, float radius, float length, const Matrix& matrix) { - dMass ODEMass; - PrepareCylinderTotal(ODEMass, total_mass, radius, length); - AddMass(ODEMass, matrix); + mRigidBodyImp->AddCylinderTotal(total_mass, radius, length, matrix); } -void RigidBody::PrepareCappedCylinder (dMass& mass, float density, float radius, float length) const -{ - // direction: (1=x, 2=y, 3=z) - int direction = 3; - - dMassSetCapsule (&mass, density, direction, radius, length); -} - void RigidBody::SetCappedCylinder (float density, float radius, float length) { - dMass ODEMass; - PrepareCappedCylinder(ODEMass, density, radius, length); - dBodySetMass(mODEBody, &ODEMass); + mRigidBodyImp->SetCapsule(density, radius, length); } void RigidBody::AddCappedCylinder (float density, float radius, float length, const Matrix& matrix) { - dMass ODEMass; - PrepareCappedCylinder(ODEMass, density, radius, length); - AddMass(ODEMass, matrix); + mRigidBodyImp->AddCapsule(density, radius, length, matrix); } -void RigidBody::PrepareCappedCylinderTotal(dMass& mass, float total_mass, float radius, float length) const -{ - // direction: (1=x, 2=y, 3=z) - int direction = 3; - - dMassSetCapsuleTotal(&mass, total_mass, direction, radius, length); -} - void RigidBody::SetCappedCylinderTotal(float total_mass, float radius, float length) { - dMass ODEMass; - PrepareCappedCylinderTotal(ODEMass, total_mass, radius, length); - dBodySetMass(mODEBody, &ODEMass); + mRigidBodyImp->SetCapsuleTotal(total_mass, radius, length); } void RigidBody::AddCappedCylinderTotal(float total_mass, float radius, float length, const salt::Matrix& matrix) { - dMass ODEMass; - PrepareCappedCylinderTotal(ODEMass, total_mass, radius, length); - AddMass(ODEMass, matrix); + mRigidBodyImp->AddCapsuleTotal(total_mass, radius, length, matrix); } Vector3f RigidBody::GetVelocity() const { - const dReal* vel = dBodyGetLinearVel(mODEBody); - return Vector3f(vel[0], vel[1], vel[2]); + return mRigidBodyImp->GetVelocity(); } void RigidBody::SetVelocity(const Vector3f& vel) { - dBodySetLinearVel(mODEBody, vel[0], vel[1], vel[2]); + mRigidBodyImp->SetVelocity(vel); } void RigidBody::SetRotation(const Matrix& rot) { - dMatrix3 m; - ConvertRotationMatrix(rot,m); - dBodySetRotation(mODEBody,m); + mRigidBodyImp->SetRotation(rot); } salt::Matrix RigidBody::GetRotation() const { - const dReal* m = dBodyGetRotation(mODEBody); - salt::Matrix rot; - ConvertRotationMatrix(m,rot); - return rot; + return mRigidBodyImp->GetRotation(); } Vector3f RigidBody::GetAngularVelocity() const { - const dReal* vel = dBodyGetAngularVel(mODEBody); - return Vector3f(vel[0], vel[1], vel[2]); + return mRigidBodyImp->GetAngularVelocity(); } void RigidBody::SetAngularVelocity(const Vector3f& vel) { - dBodySetAngularVel(mODEBody, vel[0], vel[1], vel[2]); + mRigidBodyImp->SetAngularVelocity(vel); } void RigidBody::SynchronizeParent() const { - const dReal* pos = dBodyGetPosition(mODEBody); - const dReal* rot = dBodyGetRotation(mODEBody); - shared_ptr<BaseNode> baseNode = shared_static_cast<BaseNode> (GetParent().lock()); - - Matrix mat; - mat.m[0] = rot[0]; - mat.m[1] = rot[4]; - mat.m[2] = rot[8]; - mat.m[3] = 0; - mat.m[4] = rot[1]; - mat.m[5] = rot[5]; - mat.m[6] = rot[9]; - mat.m[7] = 0; - mat.m[8] = rot[2]; - mat.m[9] = rot[6]; - mat.m[10] = rot[10]; - mat.m[11] = 0; - mat.m[12] = pos[0]; - mat.m[13] = pos[1]; - mat.m[14] = pos[2]; - mat.m[15] = 1; + Matrix mat = mRigidBodyImp->GetSynchronisationMatrix(); baseNode->SetWorldTransform(mat); } @@ -428,7 +271,7 @@ void RigidBody::PrePhysicsUpdateInternal(float /*deltaTime*/) { // Check whether mass/body has been translated - if (mMassTransformed) + if (mRigidBodyImp->mMassTransformed) { weak_ptr<Node> parent = GetParent(); @@ -459,8 +302,8 @@ transform->SetWorldTransform(worldTransform); } - mMassTrans = Vector3f(0,0,0); - mMassTransformed = false; + mRigidBodyImp->mMassTrans = Vector3f(0,0,0); + mRigidBodyImp->mMassTransformed = false; } } @@ -472,18 +315,19 @@ shared_ptr<RigidBody> RigidBody::GetBody(dBodyID id) { - if (id == 0) + long bodyID = (long) id; + if (bodyID == 0) { return shared_ptr<RigidBody>(); } RigidBody* bodyPtr = - static_cast<RigidBody*>(dBodyGetData(id)); + static_cast<RigidBody*>(dBodyGetData( (dBodyID) bodyID)); if (bodyPtr == 0) { // we cannot use the logserver here - cerr << "ERROR: (RigidBody) no body found for dBodyID " + cerr << "ERROR: (RigidBody) no body found for BodyID " << id << "\n"; return shared_ptr<RigidBody>(); } @@ -503,29 +347,25 @@ void RigidBody::AddForce(const Vector3f& force) { - dBodyAddForce(mODEBody, force.x(), force.y(), force.z()); + mRigidBodyImp->AddForce(force); } void RigidBody::AddTorque(const Vector3f& torque) { - dBodyAddTorque(mODEBody, torque.x(), torque.y(), torque.z()); + mRigidBodyImp->AddTorque(torque); } void RigidBody::SetPosition(const Vector3f& pos) { - dBodySetPosition(mODEBody, pos.x(), pos.y(), pos.z()); - // the parent node will be updated in the next physics cycle + mRigidBodyImp->SetPosition(pos); } Vector3f RigidBody::GetPosition() const { - const dReal* pos = dBodyGetPosition(mODEBody); - return Vector3f(pos[0], pos[1], pos[2]); + return mRigidBodyImp->GetPosition(); } void RigidBody::TranslateMass(const Vector3f& v) { - dMass m; - dBodyGetMass(mODEBody, &m); - dMassTranslate(&m,v[0],v[1],v[2]); + mRigidBodyImp->TranslateMass(v); } Modified: branches/multiphys/spark/lib/oxygen/physicsserver/rigidbody.h =================================================================== --- branches/multiphys/spark/lib/oxygen/physicsserver/rigidbody.h 2009-11-30 08:31:04 UTC (rev 110) +++ branches/multiphys/spark/lib/oxygen/physicsserver/rigidbody.h 2009-12-01 09:08:17 UTC (rev 111) @@ -85,6 +85,11 @@ /** returns the ODE mass parameters of this body */ void GetMassParameters(dMass& mass) const; + + /** adds the given ode mass to this body. The given matrix is + applied to the mass center + */ + void AddMass(const dMass& mass, const salt::Matrix& matrix); /** sets the mass parameters to represent a sphere of the given radius and density, with the center of mass at (0,0,0) @@ -235,11 +240,6 @@ */ void SynchronizeParent() const; - /** adds the given ode mass to this body. The given matrix is - applied to the mass center - */ - void AddMass(const dMass& mass, const salt::Matrix& matrix); - salt::Vector3f GetMassCenter() const; protected: @@ -251,60 +251,6 @@ /** create the managed ODE body; returns true on success */ bool CreateBody(); - /** sets up an ode mass struct representing a box of the given - size and total_mass - */ - void PrepareBoxTotal(dMass& mass, float total_mass, const salt::Vector3f& size) const; - - /** sets up an ode mass struct representing a box of the given - density and size - */ - void PrepareBox(dMass& mass, float density, const salt::Vector3f& size) const; - - /** sets up an ode mass struct representing a sphere of the given - density and radius - */ - void PrepareSphere(dMass& mass, float density, float radius) const; - - /** sets up an ode mass struct representing a sphere of the given - radius and total_mass - */ - void PrepareSphereTotal(dMass& mass, float total_mass, float radius) const; - - /** sets up an ode mass struct representing a flat-ended cylinder - of the given parameters and density, with the center of mass - at (0,0,0) relative to the body. The radius of the cylinder is - radius. The length of the cylinder is length. The cylinder's - long axis is oriented along the body's z axis. - */ - void PrepareCylinder (dMass& mass, float density, float radius, float length) const; - - /** sets up an ode mass struct representing a flat-ended cylinder - of the given parameters and total mass, with the center of - mass at (0,0,0) relative to the body. The radius of the - cylinder is radius. The length of the cylinder is length. The - cylinder's long axis is oriented along the body's z axis. - */ - void PrepareCylinderTotal(dMass& mass, float total_mass, float radius, float length) const; - - /* sets up an ode mass struct representing a capped cylinder of - the given parameters and density, with the center of mass at - (0,0,0) relative to the body. The radius of the cylinder (and - the spherical cap) is radius. The length of the cylinder (not - counting the spherical cap) is length. The cylinder's long axis - is oriented along the body's z axis. - */ - void PrepareCappedCylinder (dMass& mass, float density, float radius, float length) const; - - /* sets up an ode mass struct representing a capped cylinder of - the given parameters and total mass, with the center of mass at - (0,0,0) relative to the body. The radius of the cylinder (and - the spherical cap) is radius. The length of the cylinder (not - counting the spherical cap) is length. The cylinder's long axis - is oriented along the body's z axis. - */ - void PrepareCappedCylinderTotal(dMass& mass, float total_mass, float radius, float length) const; - private: /** updates internal state before physics calculation */ virtual void PrePhysicsUpdateInternal(float deltaTime); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |