NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
heightSensor.cpp
1 /*
2  * heightSensor.cpp
3  *
4  * Created on: Jul 15, 2014
5  * Author: atiliscen
6  */
7 
8 #include "heightSensor.h"
9 
10 
11 heightSensor::heightSensor(){
12 
13 }
14 
15 //Place the marker to the current world position and attach it to the body.
16 heightSensor::heightSensor(const btRigidBody *body,btVector3 worldPos,int nodeNumber,tgWorld& world)
17 {
18  btworld=&(tgBulletUtil::worldToDynamicsWorld(world));
19  attachedBody=body;
20  //find relative position
21  attachedRelativeOriginalPosition=worldPos;//Use local transform;
22  this->nodeNumber = nodeNumber;
23 }
24 
25 //This returns current position relative to the rigidbody.
26 btVector3 heightSensor::getRelativePosition() const
27 {
28  btTransform tr = attachedBody->getWorldTransform();
29  btVector3 worldPos = tr * attachedRelativeOriginalPosition;
30  return worldPos-this->attachedBody->getCenterOfMassPosition();
31 }
32 
33 //This returns current absolute position of the marker.
34 btVector3 heightSensor::getWorldPosition() const
35 {
36  btTransform tr = attachedBody->getWorldTransform();
37  return tr * attachedRelativeOriginalPosition;
38 }
39 
40 double heightSensor::getHeight() const
41 {
42  double distance = -1.0;
43 
44  btVector3 origin=this->getWorldPosition();
45  btVector3 end = origin;
46  end.setY(-5000.0f);
47  origin.setY(origin.getY()-1);
48  btCollisionWorld::AllHitsRayResultCallback rayCallback(origin, end);
49  btworld->rayTest(origin, end, rayCallback);
50  if(rayCallback.hasHit())
51  {
52  int idx = 0;
53  rayCallback.m_hitPointWorld[idx] = origin - rayCallback.m_hitPointWorld[idx];
54  distance=rayCallback.m_hitPointWorld[idx].getY();
55  }
56  return distance;
57 }
static btDynamicsWorld & worldToDynamicsWorld(const tgWorld &world)