00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <physics/ODEJoint>
00026 #include <physics/ODEMotor>
00027 #include <physics/ODEConstraintGroup>
00028 #include <physics/ODESolid>
00029
00030 using physics::ODEJoint;
00031 using physics::ODEMotor;
00032 using physics::Joint;
00033 using physics::Motor;
00034 using physics::Body;
00035 using physics::ODESolid;
00036
00037
00038 ODEJoint::ODEJoint()
00039 {
00040 }
00041
00042 ODEJoint::~ODEJoint()
00043 {
00044 }
00045
00046
00047 void ODEJoint::attach(ref<Body> body1, ref<Body> body2)
00048 {
00049 Assert(body1 != 0);
00050 Assert(body2 != 0);
00051 Assertm(body1 != body2, "body1 is not body2");
00052
00053 if (jointID == 0)
00054 throw std::runtime_error(Exception("Constraint must be added to a ConstraintGroup before being attached to bodies"));
00055
00056 this->body1 = body1;
00057 this->body2 = body2;
00058
00059 ref<ODESolid> solid1 = narrow_ref<ODESolid>(body1);
00060 ref<ODESolid> solid2 = narrow_ref<ODESolid>(body2);
00061
00062 dJointAttach(jointID, solid1->bodyID, solid2->bodyID);
00063 }
00064
00065 ref<Body> ODEJoint::getBody(Int index)
00066 {
00067 Assert(index < 2);
00068 return (index==0)?body1:body2;
00069 }
00070
00071 ref<const Body> ODEJoint::getBody(Int index) const
00072 {
00073 Assert(index < 2);
00074 return (index==0)?body1:body2;
00075 }
00076
00077
00078 void ODEJoint::attachMotor(Int dof, ref<Motor> motor)
00079 {
00080 if (!hasMotor(dof))
00081 throw std::invalid_argument(Exception("Invalid motor dof attachment for this Joint"));
00082
00083 if (motor) {
00084
00085 Assertm( instanceof(*motor, ODEMotor), "Motor created by same SolidSystem as Joint");
00086
00087 ref<ODEMotor> omotor = narrow_ref<ODEMotor>(motor);
00088 motors[dof] = omotor;
00089 if (motor != 0) {
00090 ref<Joint> self(this);
00091 omotor->setJoint(self, dof);
00092 }
00093 } else {
00094 motors[dof]->setJoint(ref<Joint>(0),dof);
00095 }
00096
00097 }
00098
00099
00100 ref<Motor> ODEJoint::getMotor(Int dof)
00101 {
00102 if (!hasMotor(dof))
00103 throw std::invalid_argument(Exception("Invalid motor dof attachment for this Joint"));
00104
00105 return motors[dof];
00106 }
00107
00108 ref<const Motor> ODEJoint::getMotor(Int dof) const
00109 {
00110 if (!hasMotor(dof))
00111 throw std::invalid_argument(Exception("Invalid motor dof attachment for this Joint"));
00112
00113 return motors[dof];
00114 }
00115
00116
00117