~robot3d-team/robot3d/trunk

« back to all changes in this revision

Viewing changes to inc/srCore/body/bodyBase.h

  • Committer: Anne van Rossum
  • Date: 2010-08-10 15:58:55 UTC
  • Revision ID: anne@gamix-20100810155855-kve7x2vwouagdij9
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
//! General body class, which all other body classes have to inherit.
 
3
 
 
4
/*!
 
5
 *
 
6
 * Describes general properties of a body in the simulation environment.
 
7
 *
 
8
 * @author Lutz Winkler
 
9
 *
 
10
 */
 
11
 
 
12
 
 
13
#ifndef BODY_H_
 
14
#define BODY_H_
 
15
 
 
16
#include <iostream>
 
17
 
 
18
#include <dtCore/dt.h>
 
19
#include <dtCore/object.h>
 
20
#include <dtCore/globals.h>
 
21
#include <dtCore/transform.h>
 
22
#include <dtGame/gameactor.h>
 
23
#include <dtCore/refptr.h>
 
24
 
 
25
 
 
26
#include <osg/Node>
 
27
#include <osg/Geode>
 
28
#include <osg/Group>
 
29
#include <osg/PositionAttitudeTransform>
 
30
#include <osgDB/ReadFile>
 
31
#include <osg/Material>
 
32
#include <osg/NodeVisitor>
 
33
 
 
34
#include <ode/ode.h>
 
35
 
 
36
#include <srCore/simulationUtils.h>
 
37
 
 
38
#define _I(i,j) I[(i)*4+(j)]
 
39
 
 
40
namespace srCore {
 
41
 
 
42
 
 
43
 
 
44
class BodyBase;
 
45
class osgBodyNodeVisitor;
 
46
 
 
47
class osgBodyTransform : public osg::PositionAttitudeTransform {
 
48
 
 
49
public:
 
50
        osgBodyTransform(dtCore::RefPtr<BodyBase> srBody);
 
51
 
 
52
        dtCore::RefPtr<BodyBase> getMyBody();
 
53
        virtual void accept(osgBodyNodeVisitor& bnv);
 
54
        virtual void traverse (osgBodyNodeVisitor &bnv);
 
55
 
 
56
protected:
 
57
        ~osgBodyTransform();
 
58
 
 
59
private:
 
60
        dtCore::RefPtr<BodyBase> myBody;
 
61
 
 
62
};
 
63
 
 
64
typedef std::vector< dtCore::RefPtr<osgBodyTransform> > BodyTransformNodeList;
 
65
 
 
66
class osgBodyNodeVisitor : public osg::NodeVisitor
 
67
{
 
68
public:
 
69
        osgBodyNodeVisitor(BodyBase* root, TraversalMode tm=TRAVERSE_NONE);
 
70
        osgBodyNodeVisitor (TraversalMode tm=TRAVERSE_NONE);
 
71
        osgBodyNodeVisitor (VisitorType type, TraversalMode tm=TRAVERSE_NONE);
 
72
 
 
73
        virtual void apply(osgBodyTransform &node);
 
74
//      virtual void accept(osgBodyNodeVisitor& bnv);
 
75
 
 
76
        void addChildToBodyRoot (BodyBase* child);
 
77
        BodyBase* returnBodyRoot ();
 
78
 
 
79
        virtual ~osgBodyNodeVisitor ();
 
80
 
 
81
private:
 
82
        BodyBase* parent;
 
83
 
 
84
};
 
85
 
 
86
class RobotActorBase;
 
87
 
 
88
class BodyBase : public dtCore::Object
 
89
{
 
90
 
 
91
        friend class ConnectorBase;
 
92
        friend class SimulationUtils;
 
93
 
 
94
public:
 
95
 
 
96
        static std::map< dBodyID, BodyBase*> odeBodyMap;
 
97
 
 
98
        //! Constuctor
 
99
        BodyBase();
 
100
        
 
101
        //! Register the body to the simulation, i.e. map the BodyBase object to the ODE body ID
 
102
        void registerBody();
 
103
 
 
104
        //! Merges the collision shape and the masses of two bodies together. theBody will become child of this body and the physics will be disabled of theBody
 
105
        void mergeBody(BodyBase* theBody);
 
106
 
 
107
        //! Adds the body to the scene, makes it visible and activates the physical behaviour
 
108
        void addToScene(); //dtCore::Scene*& scene,  osg::Group*& osgGroup);
 
109
 
 
110
        //! Scaling of the model to Robot3D environment
 
111
        void setScale(float scaleX, float scaleY, float scaleZ);
 
112
 
 
113
        //! Show the collision geometry of the body.
 
114
 
 
115
        /*! Activate this to check manually/optically if collision geometry fits to the model.
 
116
         *  Will also be used to indicate which robot is under manual control.
 
117
         */
 
118
        void renderCollisionGeometry(bool renderIt);
 
119
        void renderCollisionGeometry2(bool renderIt);
 
120
        void colorBody(osg::Vec4 color);
 
121
        void colorCollisionShape(osg::Vec4 color);
 
122
 
 
123
        /*! Initializes the body. Needs to be called before using the body
 
124
         *
 
125
         *      @param name                                     The name of the body
 
126
         *      @param robot                                    The robot the body belongs to
 
127
         *      @param x,y,z                                    The relative position of the body within the robot
 
128
         *      @param heading, roll, pitch             The relative orientation of the body within the robot
 
129
         *
 
130
         */
 
131
        void init(      std::string name, RobotActorBase* robot,
 
132
                                float x, float y, float z,
 
133
                                float heading, float roll, float pitch);
 
134
 
 
135
        //! Return the body Id, which represents the geomCollide and geomCategory bits
 
136
        long getID();
 
137
 
 
138
        //! returns the global position of the body
 
139
        osg::Vec3f getTranslation() const;
 
140
 
 
141
        //! returns the global Rotation of the body ()
 
142
        osg::Vec3f getRotation() const;
 
143
 
 
144
        //! returns the scaling factor between the physical and the geometrical world
 
145
        osg::Vec3f getScale() const;
 
146
 
 
147
        //! returns the Transform of the Body
 
148
        dtCore::Transform getTransform() const {return this->xform;}
 
149
 
 
150
        //! sets the Transform of the Body
 
151
        virtual void setTransform(dtCore::Transform &transform);
 
152
 
 
153
        //! sets the position of the Body. Only use that while physics is deactivated.
 
154
        virtual void setTranslation(osg::Vec3f pos);
 
155
 
 
156
        //! sets the rotation of the Body. Only use that while physics is deactivated.
 
157
        virtual void setRotation(osg::Vec3f hrp);
 
158
 
 
159
        //! Returns the Robot the body belongs to
 
160
        RobotActorBase* getRobot() const {return myRobot;};
 
161
 
 
162
        //! Returns the current collision model of the body
 
163
        dtCore::RefPtr<osg::Node> getCollisionModel() const {return collisionModel;};
 
164
 
 
165
        //! Sets the collision model
 
166
        void setCollisionModel(dtCore::RefPtr<osg::Node> collModel);
 
167
        void setCollisionModel(osgBodyTransform* collModel);
 
168
        osgBodyTransform* getCollisionModel();
 
169
 
 
170
 
 
171
        unsigned long int getCollisionBits() const {return collCategoryBit;};
 
172
        void setCollisionBits(unsigned long int bit);
 
173
 
 
174
        //! Set the mass to this subOrgansim, consisting of this body and all its children
 
175
        void setSubOrgBodyMass(dMass *mass) {dMassSetParameters(&subOrgBodyMass, mass->mass,
 
176
                                                                                                                        mass->c[0], mass->c[1], mass->c[2],
 
177
                                                                                                                        mass->_I(0,0), mass->_I(1,1), mass->_I(2,2),
 
178
                                                                                                                        mass->_I(1,2), mass->_I(1,3), mass->_I(2,3));};
 
179
 
 
180
        //! Returns the mass to this subOrgansim, consisting of this body and all its children
 
181
        void getSubOrgBodyMass(dMass &mass) {dMassSetParameters(&mass, subOrgBodyMass.mass,
 
182
                                                                                                                        subOrgBodyMass.c[0], subOrgBodyMass.c[1], subOrgBodyMass.c[2],
 
183
                                                                                                                        subOrgBodyMass._I(0,0), subOrgBodyMass._I(1,1), subOrgBodyMass._I(2,2),
 
184
                                                                                                                        subOrgBodyMass._I(1,2), subOrgBodyMass._I(1,3), subOrgBodyMass._I(2,3));};
 
185
        
 
186
        //! Returns true, when the body has a contact with the floot or other obstacles
 
187
        bool hasContact() {return bodyHasContact;};
 
188
 
 
189
protected:
 
190
        virtual ~BodyBase();
 
191
 
 
192
        //! To be set true, when that body has collided with an obstacle or the floor (currently by the simulationUtils class)
 
193
        void setContact(bool contact) {bodyHasContact = true;};
 
194
 
 
195
 
 
196
 
 
197
        long int myID;
 
198
        RobotActorBase* myRobot;
 
199
        float scaleX, scaleY, scaleZ;
 
200
 
 
201
        //! collision model of merged bodies (e.g. the through the coupling devices merged bodies)
 
202
        //dtCore::RefPtr<osg::PositionAttitudeTransform>
 
203
        dtCore::RefPtr<osgBodyTransform> collisionModel;
 
204
        dMass subOrgBodyMass;
 
205
 
 
206
private:
 
207
 
 
208
        void setModelTranslation(osg::Vec3 pos);
 
209
        void setAllModelTranslations(dtCore::Object &object, std::string theOtherObjectName, osg::Vec3 pos);
 
210
 
 
211
        // transformation of the body
 
212
        dtCore::Transform xform;
 
213
 
 
214
        //! If true, collision geometry will be rendered
 
215
        bool renderGeom;
 
216
 
 
217
        bool bodyHasContact;
 
218
 
 
219
 
 
220
        //!collision model of the single body
 
221
        dtCore::RefPtr<dtCore::Object> bodyCollisionModel;
 
222
 
 
223
        unsigned long int collCategoryBit;
 
224
 
 
225
 
 
226
};
 
227
 
 
228
typedef std::map< dBodyID, BodyBase*> body_map_type;
 
229
typedef body_map_type::value_type body_pair_type;
 
230
 
 
231
}
 
232
 
 
233
//#include "../robotActorBase.h"
 
234
 
 
235
 
 
236
#endif /*BODY_H_*/