00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00027 #include "../include/IPSA/ipsaclasses.h"
00028 #include "../include/IPSA/viewer/IpsaViewer.h"
00029 #include "../include/IPSA/viewer/instructions/ListBody2BodyForces.h"
00030
00031 #include <ode/ode.h>
00032
00033 #include <sstream>
00034
00035 Instruction::tExecResultType ListBody2BodyForces::vExecute(std::vector<std::string>& Instr, std::ostringstream& answer, unsigned int& numberOfReturnValues)
00036 {
00037 numberOfReturnValues = 0;
00038 SoNode* node = SoNode::getByName(Instr[1].c_str());
00039 SoNode* node2 = SoNode::getByName(Instr[2].c_str());
00040
00041 if ((NULL == node) || (NULL == node2))
00042 return eBODY_NOT_FOUND;
00043
00044 if ( !(node->isOfType(SoPhysics::getClassTypeId())) || !(node2->isOfType(SoPhysics::getClassTypeId())))
00045 return eNODE_IS_NOT_A_BODY;
00046
00047 SoPhysics* phy = static_cast<SoPhysics*>(node);
00048 dBodyID body1 = phy->getBody();
00049 SoPhysics* phy2 = static_cast<SoPhysics*>(node2);
00050 dBodyID body2 = phy2->getBody();
00051
00052 if ((NULL == body1) || (NULL == body2))
00053 return eNO_ODE_BODY_FOUND;
00054
00055 int numberOfJoints = dBodyGetNumJoints(body1);
00056 int numberOfContacts = 0;
00057 tExecResultType result = eOK;
00058
00059
00060 for (int i = 0; i < numberOfJoints; i++)
00061 {
00062 dJointID joint = dBodyGetJoint(body1, i);
00063 dBodyID contactBody1 = dJointGetBody(joint, 0);
00064 dBodyID contactBody2 = dJointGetBody(joint, 1);
00065 int jointType = dJointGetType(joint);
00066 if ((jointType == dJointTypeContact) && (contactBody2 == body2))
00067 {
00068 numberOfContacts++;
00069 dJointFeedback* jointFeedback = dJointGetFeedback(joint);
00070 if (NULL != jointFeedback)
00071 {
00072 dBodyID collidingBody;
00073 SoPhysics* ncb = NULL;
00074 if (contactBody1 == body1)
00075 collidingBody = contactBody2;
00076 else
00077 collidingBody = contactBody1;
00078 if (NULL != collidingBody)
00079 ncb = static_cast<SoPhysics*>(dBodyGetData(collidingBody));
00080 answer << "contact force <";
00081 if (NULL != ncb)
00082 answer << ncb->getName().getString();
00083 answer << "> "
00084 << std::scientific << jointFeedback->f1[0]
00085 << ' ' << jointFeedback->f1[1]
00086 << ' ' << jointFeedback->f1[2]
00087 << '\r' << std::endl;
00088 result = eOK;
00089 numberOfReturnValues += 3;
00090 }
00091 else
00092 {
00093 answer << "Body1 provides no force feedback! ";
00094 result = eUNKNOWN_ERROR;
00095 numberOfReturnValues = 0;
00096 }
00097 }
00098 }
00099
00100 if (numberOfContacts == 0)
00101 answer << "no contacts found!\r" << std::endl;
00102 answer << "END\r" << std::endl;
00103
00104 return result;
00105 }