22 m_triggerController = 0;
39 m_scale = Ogre::Vector3::UNIT_SCALE;
54 m_body = m_triggerController->GetBody();
63 NewtonBodySetUserData(
m_body,
this);
64 NewtonBodySetDestructorCallback(
m_body, newtonDestructor);
65 NewtonBodySetTransformCallback(
m_body, newtonTransformCallback);
84 NewtonBodySetUserData(
m_body, NULL);
85 NewtonBodySetDestructorCallback(
m_body, NULL);
86 NewtonBodySetForceAndTorqueCallback(
m_body, NULL);
87 NewtonBodySetTransformCallback(
m_body, NULL);
88 if (m_triggerController)
91 m_triggerController = 0;
103 void _CDECL Body::newtonDestructor(
const NewtonBody* body)
110 NewtonBodySetDestructorCallback(body, NULL);
112 NewtonBodySetUserData(body, NULL);
114 NewtonBodySetForceAndTorqueCallback(body, NULL);
115 NewtonBodySetTransformCallback(body, NULL);
129 void _CDECL Body::newtonTransformCallback(
const NewtonBody* body,
const dFloat* matrix,
int threadIndex)
139 me->
m_curPosit = Ogre::Vector3(Ogre::Real(matrix[12]), Ogre::Real(matrix[13]), Ogre::Real(matrix[14]));
143 NewtonBodyGetRotation(body, &rot.m_x);
156 void Body::newtonTriggerEnterCallback(NewtonBody*
const visitor)
165 void Body::newtonTriggerInsideCallback(NewtonBody*
const visitor)
175 void Body::newtonTriggerExitCallback(NewtonBody*
const visitor)
186 void _CDECL Body::newtonForceTorqueCallback(
const NewtonBody* body, dFloat timeStep,
int threadIndex)
196 void Body::standardForceCallback(
OgreNewt::Body* me, dFloat timestep,
int threadIndex)
202 Ogre::Vector3 inertia;
205 Ogre::Vector3 gravityAcceleration(0.0f, -9.8f, 0.0f);
206 Ogre::Vector3 gravityForce = gravityAcceleration * mass;
214 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(
"# [" + Ogre::StringConverter::toString(++nth) +
"] force " + Ogre::StringConverter::toString(it->first) +
" at " + Ogre::StringConverter::toString(it->second) +
" to " + me->
getOgreNode()->getName());
227 node->_update(
false,
true);
264 NewtonBodySetMatrix(
m_body, &matrix[0]);
285 NewtonBodySetMatrix(
m_body, &matrix[0]);
308 NewtonBodySetMatrix(
m_body, &matrix[0]);
319 NewtonBodySetMassMatrix(
m_body, (
float)mass, (
float)inertia.x, (
float)inertia.y, (
float)inertia.z);
328 NewtonBodySetMassProperties(
m_body, (
float)mass, NewtonBodyGetCollision(
m_body));
344 NewtonBodySetForceAndTorqueCallback(
m_body, newtonForceTorqueCallback);
389 NewtonBodySetMassProperties(
m_body, (
float)0.0f, NewtonBodyGetCollision(
m_body));
407 void Body::setScale(
const Ogre::Vector3& scale,
const Ogre::Quaternion& orient, Ogre::Vector3& pos)
414 NewtonCollision* collision = NewtonBodyGetCollision(
m_body);
419 NewtonCollisionSetMatrix(collision, matrix);
467 Ogre::AxisAlignedBox ret;
470 NewtonBodyGetAABB(
m_body, &min.m_x, &max.m_x);
475 dmin.x = Ogre::Real(min.m_x);
476 dmin.y = Ogre::Real(min.m_y);
477 dmin.z = Ogre::Real(min.m_z);
479 dmax.x = Ogre::Real(max.m_x);
480 dmax.y = Ogre::Real(max.m_y);
481 dmax.z = Ogre::Real(max.m_z);
483 ret.setExtents(dmin, dmax);
491 NewtonBodyGetMassMatrix(
m_body, &fMass, &fInertia.m_x, &fInertia.m_y, &fInertia.m_z);
493 mass = Ogre::Real(fMass);
494 inertia.x = Ogre::Real(fInertia.m_x);
495 inertia.y = Ogre::Real(fInertia.m_y);
496 inertia.z = Ogre::Real(fInertia.m_z);
503 NewtonBodyGetMassMatrix(
m_body, &fMass, &fInertia.m_x, &fInertia.m_y, &fInertia.m_z);
505 return Ogre::Real(fMass);
512 Ogre::Vector3 inertia;
513 NewtonBodyGetMassMatrix(
m_body, &fMass, &fInertia.m_x, &fInertia.m_y, &fInertia.m_z);
515 inertia.x = Ogre::Real(fInertia.m_x);
516 inertia.y = Ogre::Real(fInertia.m_y);
517 inertia.z = Ogre::Real(fInertia.m_z);
525 NewtonBodyGetInvMass(
m_body, &fMass, &fInertia.m_x, &fInertia.m_y, &fInertia.m_z);
527 mass = Ogre::Real(fMass);
528 inertia.x = Ogre::Real(fInertia.m_x);
529 inertia.y = Ogre::Real(fInertia.m_y);
530 inertia.z = Ogre::Real(fInertia.m_z);
536 Ogre::Vector3 ret = Ogre::Vector3::ZERO;
538 NewtonBodyGetOmega(
m_body, &fVec.m_x);
540 ret.x = Ogre::Real(fVec.m_x);
542 ret.y = Ogre::Real(fVec.m_y);
544 ret.z = Ogre::Real(fVec.m_z);
551 Ogre::Vector3 ret = Ogre::Vector3::ZERO;
553 NewtonBodyGetVelocity(
m_body, &fVec.m_x);
555 ret.x = Ogre::Real(fVec.m_x);
557 ret.y = Ogre::Real(fVec.m_y);
559 ret.z = Ogre::Real(fVec.m_z);
566 Ogre::Vector3 ret = Ogre::Vector3::ZERO;
568 NewtonBodyGetForce(
m_body, &fVec.m_x);
570 ret.x = Ogre::Real(fVec.m_x);
572 ret.y = Ogre::Real(fVec.m_y);
574 ret.z = Ogre::Real(fVec.m_z);
581 Ogre::Vector3 ret = Ogre::Vector3::ZERO;
583 NewtonBodyGetTorque(
m_body, &fVec.m_x);
585 ret.x = Ogre::Real(fVec.m_x);
587 ret.y = Ogre::Real(fVec.m_y);
589 ret.z = Ogre::Real(fVec.m_z);
596 Ogre::Vector3 ret = Ogre::Vector3::ZERO;
598 NewtonBodyGetForceAcc(
m_body, &fVec.m_x);
600 ret.x = Ogre::Real(fVec.m_x);
602 ret.y = Ogre::Real(fVec.m_y);
604 ret.z = Ogre::Real(fVec.m_z);
611 Ogre::Vector3 ret = Ogre::Vector3::ZERO;
613 NewtonBodyGetTorqueAcc(
m_body, &fVec.m_x);
615 ret.x = Ogre::Real(fVec.m_x);
617 ret.y = Ogre::Real(fVec.m_y);
619 ret.z = Ogre::Real(fVec.m_z);
625 Ogre::Vector3 ret = Ogre::Vector3::ZERO;
628 fVel.m_x = dFloat(desiredVelocity.x);
629 fVel.m_y = dFloat(desiredVelocity.y);
630 fVel.m_z = dFloat(desiredVelocity.z);
632 NewtonBodyCalculateInverseDynamicsForce(
m_body, dFloat(timestep), &fVel.m_x, &fVec.m_x);
634 ret.x = Ogre::Real(fVec.m_x);
636 ret.y = Ogre::Real(fVec.m_y);
638 ret.z = Ogre::Real(fVec.m_z);
645 Ogre::Vector3 ret = Ogre::Vector3::ZERO;
647 NewtonBodyGetAngularDamping(
m_body, &fVec.m_x);
649 ret.x = Ogre::Real(fVec.m_x);
651 ret.y = Ogre::Real(fVec.m_y);
653 ret.z = Ogre::Real(fVec.m_z);
659 Ogre::Vector3 ret = Ogre::Vector3::ZERO;
661 NewtonBodyGetCentreOfMass(
m_body, &fVec.m_x);
663 ret.x = Ogre::Real(fVec.m_x);
665 ret.y = Ogre::Real(fVec.m_y);
667 ret.z = Ogre::Real(fVec.m_z);
673 Ogre::Vector3 offset = Ogre::Vector3::ZERO;
679 NewtonConvexCollisionCalculateInertialMatrix(NewtonBodyGetCollision(
m_body), &fIner.m_x, &fOff.m_x);
681 offset.x = Ogre::Real(fOff.m_x);
682 offset.y = Ogre::Real(fOff.m_y);
683 offset.z = Ogre::Real(fOff.m_z);
691 Ogre::Vector3 inertia = Ogre::Vector3::ZERO;
697 NewtonConvexCollisionCalculateInertialMatrix(NewtonBodyGetCollision(
m_body), &fIner.m_x, &fOff.m_x);
699 inertia.x = Ogre::Real(fIner.m_x);
700 inertia.y = Ogre::Real(fIner.m_y);
701 inertia.z = Ogre::Real(fIner.m_z);
709 Ogre::Vector3 bodypos;
710 Ogre::Quaternion bodyorient;
714 Ogre::Vector3 globalMassCenter = bodypos + bodyorient * localMassCenter;
716 Ogre::Vector3 topoint = pos - globalMassCenter;
717 Ogre::Vector3 torque = topoint.crossProduct(force);
730 Ogre::Vector3 bodypos;
731 Ogre::Quaternion bodyorient;
735 Ogre::Vector3 globalforce = bodyorient * force;
736 Ogre::Vector3 globalpoint = (bodyorient * pos) + bodypos;
745 return (
Body*)NewtonBodyGetUserData(body);
812 fVel.m_x = dFloat(vel.x);
813 fVel.m_y = dFloat(vel.y);
814 fVel.m_z = dFloat(vel.z);
816 NewtonBodySetVelocity(
m_body, &fVel.m_x);
831 dVector targetPositionInGlobalSpace(destPos.x, destPos.y, destPos.z, 0.0f);
833 dFloat invTimeStep = (timestep > 0.0f) ? 1.0f / timestep : 1.0f;
834 NewtonWorld*
const world = NewtonBodyGetWorld(
m_body);
837 NewtonWorldCriticalSectionLock(world, 0);
840 NewtonBodyGetMatrix(
m_body, &matrix[0][0]);
841 NewtonBodyGetVelocity(
m_body, &veloc0[0]);
843 dVector deltaVeloc(targetPositionInGlobalSpace - matrix.m_posit);
844 deltaVeloc = deltaVeloc.Scale(stiffness * invTimeStep);
851 NewtonBodyGetMassMatrix(
m_body, &mass, &Ixx, &Iyy, &Izz);
852 dVector force((deltaVeloc - veloc0).Scale(mass * invTimeStep));
854 NewtonBodyAddForce(
m_body, &force[0]);
855 NewtonBodySetSleepState(
m_body, 0);
857 NewtonWorldCriticalSectionUnlock(world);
867 dFloat invTimeStep = (timestep > 0.0f) ? 1.0f / timestep : 1.0f;
868 NewtonWorld*
const world = NewtonBodyGetWorld(
m_body);
872 NewtonWorldCriticalSectionLock(world, 0);
875 NewtonBodyGetMatrix(
m_body, &matrix[0][0]);
876 NewtonBodyGetOmega(
m_body, &omega0[0]);
877 NewtonBodyGetRotation(
m_body, &fRot.m_x);
884 NewtonBodyGetMassMatrix(
m_body, &mass, &Ixx, &Iyy, &Izz);
885 dVector angularMomentum(Ixx, Iyy, Izz);
887 Ogre::Quaternion rot(Ogre::Real(fRot.m_x), Ogre::Real(fRot.m_y), Ogre::Real(fRot.m_z), Ogre::Real(fRot.m_w));
888 Ogre::Quaternion diffq = destquat * rot.Inverse();
890 dVector domega(euler.
getPitch().valueRadians(), euler.
getYaw().valueRadians(), euler.
getRoll().valueRadians());
891 domega = domega.Scale(invTimeStep);
892 domega = domega - omega0;
895 angularMomentum = matrix.RotateVector(angularMomentum.CompProduct(matrix.UnrotateVector(domega)));
896 dVector torque(angularMomentum.Scale(invTimeStep * stiffness));
898 NewtonBodyAddTorque(
m_body, &torque[0]);
900 NewtonBodySetSleepState(
m_body, 0);
901 NewtonWorldCriticalSectionUnlock(world);
Class for Euler rotations.
Ogre::Radian getYaw() const
Get the Yaw angle.
Ogre::Radian getRoll() const
Get the Roll angle.
Ogre::Radian getPitch() const
Get the Pitch angle.
main class for all Rigid Bodies in the system.
void MoveToPosition(const Ogre::Vector3 &destPos, const Ogre::Real &stiffness, Ogre::Real timestep)
void updatePositionOrientation(const Ogre::Vector3 &pos, const Ogre::Quaternion &orient, int threadIndex=-1)
void getMassMatrix(Ogre::Real &mass, Ogre::Vector3 &inertia) const
get Ogre::Real(mass) and Ogre::Vector3(inertia) of the body.
Ogre::Real getMass() const
return body mass
void enableSimulation(bool state)
const OgreNewt::CollisionPtr & getCollision() const
set the factors that cause a body to "freeze" when equilibrium reached.
Ogre::Vector3 m_nodePosit
void setScale(const Ogre::Vector3 &scale, const Ogre::Quaternion &orient, Ogre::Vector3 &pos)
set the collision scale
void setCustomForceAndTorqueCallback(ForceCallback callback)
set a custom force callback for this body to use.
Ogre::Quaternion getOrientation() const
returns body orientation
void setTriggerExitCallback(TriggerExitCallback callback)
void addGlobalForceAccumulate(const Ogre::Vector3 &force, const Ogre::Vector3 &pos)
helper function that adds a force (and resulting torque) to an object in global cordinates called fro...
Ogre::Vector3 getForce() const
get the force acting on the body.
void getInvMass(Ogre::Real &mass, Ogre::Vector3 &inertia) const
get invert mass + inertia for the body.
void setTriggerEnterCallback(TriggerEnterCallback callback)
void addLocalForce(const Ogre::Vector3 &force, const Ogre::Vector3 &pos)
Ogre::Quaternion getVisualOrientation() const
returns body visual orientation
const OgreNewt::MaterialID * getMaterialGroupID() const
get a pointer to the Material assigned to this body.
void setNodeUpdateJointNotify(NodeUpdateNotifyCallback callback)
TriggerInsideCallback m_triggerInsideCallback
void setVisualPosition(Ogre::Vector3 pos, Ogre::Quaternion quat)
void freeze()
freeze the rigid body.
void addGlobalForce(const Ogre::Vector3 &force, const Ogre::Vector3 &pos)
helper function that adds a force (and resulting torque) to an object in global cordinates.
Ogre::Vector3 getVelocity() const
get velocity of the body. in global coordinates.
Ogre::Vector3 getPosition() const
returns body position
std::function< void(OgreNewt::Body *)> TriggerEnterCallback
void setStandardForceCallback()
set a standard gravity callback for this body to use.
ForceCallback m_forcecallback
TriggerEnterCallback m_triggerEnterCallback
Ogre::Vector3 m_prevPosit
void updateNode(Ogre::Real interpolatParam)
update the position of the node (if attached) and sets m_nodeupdateneeded to false
void addTorque(const Ogre::Vector3 &torque)
add torque to the body.
std::function< void(OgreNewt::Body *)> TriggerExitCallback
void setMassMatrix(Ogre::Real mass, const Ogre::Vector3 &inertia)
set the mass and inertia for the body.
void addForce(const Ogre::Vector3 &force)
add force to the body.
Body * getNext() const
use this function to iterate through all bodies
NodeUpdateNotifyCallback m_nodeupdatejointnotifycallback
Ogre::Vector3 getVisualPosition() const
return body visual position
Ogre::Vector3 getCenterOfMass() const
get the center of mass.
Ogre::Vector3 getOmega() const
get omega of the body. in global space.
std::function< void(OgreNewt::Body *)> NodeUpdateNotifyCallback
node update notify.
Ogre::Quaternion m_curRotation
std::function< void(OgreNewt::Body *, Ogre::Real timeStep, int threadIndex)> ForceCallback
custom force callback.
std::vector< std::pair< Ogre::Vector3, Ogre::Vector3 > > m_accumulatedGlobalForces
void setPositionOrientationFromNode(int threadIndex=-1)
position and orient the body from node.
void setMass(Ogre::Real mass)
Ogre::Vector3 calculateInverseDynamicsForce(Ogre::Real timestep, Ogre::Vector3 desiredVelocity)
calculate force needed for given velocity
Ogre::Vector3 calculateOffset() const
Body(World *const W, const OgreNewt::CollisionPtr &col, int bodytype=0, bool trigger=false)
constructor.
void setPositionOrientation(const Ogre::Vector3 &pos, const Ogre::Quaternion &orient, int threadIndex=-1)
position and orient the body arbitrarily.
Ogre::Vector3 getInertia() const
return body inertia
void attachNode(Ogre::Node *node)
attach this body to an Ogre::Node*
const MaterialID * m_matid
TriggerExitCallback m_triggerExitCallback
Ogre::Vector3 getForceAcceleration() const
get the linear acceleration due to forces acting on the body.
NodeUpdateNotifyCallback m_nodeupdatenotifycallback
void setNodeUpdateNotify(NodeUpdateNotifyCallback callback)
set a custom node update notify.
Ogre::AxisAlignedBox getAABB() const
get the axis-aligned bounding box for this body.
Ogre::Node * getOgreNode() const
get a pointer to the attached Node.
void setVelocity(const Ogre::Vector3 &vel)
set an arbitrary velocity for the body.
Ogre::Vector3 calculateInertialMatrix() const
Ogre::Vector3 getTorqueAcceleration() const
get the rotational acceleration due to torque acting on the body.
void RotateToOrientation(const Ogre::Quaternion &destquat, const Ogre::Real &stiffness, Ogre::Real timestep)
void getVisualPositionOrientation(Ogre::Vector3 &pos, Ogre::Quaternion &orient) const
get the node position and orientation in form of an Ogre::Vector(position) and Ogre::Quaternion(orien...
Ogre::Vector3 getAngularDamping() const
get angular damping
OgreNewt::CollisionPtr m_collision
Ogre::Vector3 getTorque() const
get the torque acting on the body.
void setCollision(const OgreNewt::CollisionPtr &col)
set the collision that represents the shape of the body
Ogre::Quaternion m_prevRotation
std::function< void(OgreNewt::Body *)> TriggerInsideCallback
void setTriggerInsideCallback(TriggerInsideCallback callback)
Ogre::Quaternion m_nodeRotation
void getPositionOrientation(Ogre::Vector3 &pos, Ogre::Quaternion &orient) const
get position and orientation in form of an Ogre::Vector(position) and Ogre::Quaternion(orientation)
void unFreeze()
unfreeze the rigid body.
represents a shape for collision detection
NewtonCollision *const getNewtonCollision() const
retrieve the Newton pointer
represents a physics world.
void DestroyController(CustomTriggerController *const controller)
void criticalSectionUnlock() const
notify the exit of a critical section of code.
const MaterialID * getDefaultMaterialID() const
get the default materialID object.
NewtonWorld * getNewtonWorld() const
retrieves a pointer to the NewtonWorld
void criticalSectionLock() const
notify an entrance to a critical section of code.
CustomTriggerController * CreateController(const dMatrix &matrix, NewtonCollision *const convexShape)
void waitForUpdateToFinish() const
void setTriggerInsideCallback(WorldTriggerInsideCallback callback)
void setTriggerExitCallback(WorldTriggerExitCallback callback)
void setTriggerEnterCallback(WorldTriggerEnterCallback callback)
_OgreNewtExport void QuatPosToMatrix(const Ogre::Quaternion &quat, const Ogre::Vector3 &pos, dFloat *matrix)
Take a Quaternion and Position Matrix and create a Newton-happy float matrix!