~robot3d-team/robot3d/trunk

« back to all changes in this revision

Viewing changes to inc/srCore/robot/robotActorBase.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
//! Robot Actor base class.
 
2
 
 
3
/*!
 
4
 *
 
5
 * Handles general functions of the robot class.
 
6
 *
 
7
 * @author Lutz Winkler
 
8
 *
 
9
 */
 
10
 
 
11
#ifndef ROBOTACTORBASE_H_
 
12
#define ROBOTACTORBASE_H_
 
13
 
 
14
// ODE
 
15
#include <ode/ode.h>
 
16
 
 
17
// Open scene graph
 
18
#include <osg/Texture2D>
 
19
#include <osg/Geode>
 
20
#include <osg/Geometry>
 
21
 
 
22
// Delta3D files
 
23
#include <dtGame/gameactor.h>
 
24
#include <dtCore/refptr.h>
 
25
#include <dtCore/object.h>
 
26
#include <dtCore/camera.h>
 
27
#include <dtCore/transform.h>
 
28
#include <dtDAL/namedparameter.h>
 
29
#include <dtUtil/coordinates.h>
 
30
#include <dtGame/actorupdatemessage.h>
 
31
#include <dtGame/basemessages.h>
 
32
 
 
33
// Robot3D files
 
34
#include <srCore/export.h>
 
35
#include <srCore/xmlConfig.h>
 
36
#include <srCore/robot/objectBase.h>
 
37
#include <srCore/actuator/actuatorBase.h>
 
38
#include <srCore/sensor/sensorBase.h>
 
39
#include <srCore/actuator/connectorBase.h>
 
40
#include <srCore/robot/robotActorBase.h>
 
41
 
 
42
 
 
43
namespace srCore {
 
44
 
 
45
class ConnectorBase;
 
46
class ActuatorBase;
 
47
class SensorBase;
 
48
class ObjectBase;
 
49
 
 
50
 
 
51
class ROBOT_EXPORT RobotActorBase : public dtGame::GameActor
 
52
{
 
53
public:
 
54
 
 
55
        //! Constructs the robot actor.
 
56
        RobotActorBase(dtGame::GameActorProxy &proxy);
 
57
 
 
58
 
 
59
        /**
 
60
         * This ID will be used for collision handling in the ode. It is not
 
61
         * a UUID.
 
62
         */
 
63
        virtual void            setID(long int id);
 
64
        virtual void            setSelectionState(bool selState);
 
65
        virtual void            setConnectorStates(int state);
 
66
 
 
67
        /**
 
68
         * Returns the robot ID. This ID will be used for collision handling in the ode. It is not
 
69
         * a UUID.
 
70
         */
 
71
        virtual long int        getID() const { return myID; };
 
72
        virtual bool            getSelectionState() { return selectionState; };
 
73
        virtual int             getConnectorStates() const {
 
74
 
 
75
                int state = 0;
 
76
                int bitCheck = 1;
 
77
                for (unsigned int i=0; i<myConnectors.size(); i++) {
 
78
 
 
79
                        if (myConnectors[i]->getState() != ConnectorBase::DISCONNECTED) state += bitCheck;
 
80
                        bitCheck = bitCheck << 1;
 
81
 
 
82
                }
 
83
 
 
84
                return state;
 
85
 
 
86
        };
 
87
 
 
88
 
 
89
        //! Returns the osgGroup. Will be used for shadows or later special scenes used for sensors
 
90
        virtual dtCore::RefPtr<osg::Group> getOsgGroup() const { return osgGroup; };
 
91
 
 
92
        //! Dirty short-cut to enable/disable physics
 
93
        void EnableDynamics(bool enable);
 
94
 
 
95
        //! Sets global position of the robot
 
96
        virtual void setTranslation(const osg::Vec3& pos);
 
97
 
 
98
        //! Sets global orientation (hpr) of the robot
 
99
        virtual void setRotation(const osg::Vec3& rot);
 
100
 
 
101
        /**
 
102
         * Gets global position of the robot. It is not position, it is translation.
 
103
         * @deprecated
 
104
         */
 
105
        virtual osg::Vec3f getTranslation() const { return myPos; };
 
106
 
 
107
        virtual osg::Vec3f getPosition() const { return myPos; };
 
108
 
 
109
        virtual void setPosition(const osg::Vec3 &pos);
 
110
 
 
111
        //! Gets global orientation of the robot (hpr)
 
112
        virtual osg::Vec3f getRotation();                       // const { return myRot; };
 
113
 
 
114
        dtCore::UniqueId* getBusID() const { return globalCommID; };
 
115
        void setBusID(dtCore::UniqueId* id) {globalCommID = id;};
 
116
 
 
117
        /**
 
118
         * This method is an invokable called when an object is local and
 
119
         * receives a tick.
 
120
         * @param tickMessage A message containing tick related information.
 
121
         */
 
122
        virtual void TickLocal(const dtGame::Message &tickMessage);
 
123
 
 
124
        /**
 
125
         * This method is an invokable called when an object is remote and
 
126
         * receives a tick.
 
127
         * @param tickMessage A message containing tick related information.
 
128
         */
 
129
        virtual void TickRemote(const dtGame::Message &tickMessage);
 
130
 
 
131
        /**
 
132
         * Generic handler for messages. Overridden from base class.
 
133
         * This is the default invokable on GameActorProxy.
 
134
         */
 
135
        virtual void ProcessMessage(const dtGame::Message &message);
 
136
 
 
137
 
 
138
        /** Called when the actor has been added to the game manager.
 
139
         * You can respond to OnEnteredWorld on either the proxy or actor or both.
 
140
         */
 
141
        virtual void OnEnteredWorld();
 
142
 
 
143
        inline std::vector<RobotActorBase* > getConnectedRobots() {
 
144
                std::vector<RobotActorBase* > connectedRobots;
 
145
                getConnectedRobots(connectedRobots);
 
146
                return connectedRobots;
 
147
        };
 
148
 
 
149
        inline std::vector<BodyBase* > getConnectedBodies(BodyBase* parentBody) {
 
150
 
 
151
                std::vector<BodyBase* > connectedBodies;
 
152
 
 
153
                getConnectedBodies(connectedBodies, parentBody);
 
154
                return connectedBodies;
 
155
        };
 
156
 
 
157
        inline std::vector<dtCore::RefPtr<SensorBase> > getSensors() const {return mySensors; }
 
158
 
 
159
        //! Returns a list of actuators that are attached to the robot
 
160
        inline std::vector<dtCore::RefPtr<ActuatorBase> > getActuators() const {return myActuators;}
 
161
 
 
162
        //! Returns a list of bodies the robot consists of
 
163
        inline std::vector<dtCore::RefPtr<BodyBase> > getBodies() const {return myBodies;}
 
164
 
 
165
        //! Returns a list of connectors that are attached to the robot
 
166
        inline std::vector<dtCore::RefPtr<ConnectorBase> > getConnectors() const {return myConnectors;}
 
167
 
 
168
        //! Returns a list of objects that are attached to the robot and not of the types above
 
169
        inline std::vector<dtCore::RefPtr<ObjectBase> > getObjects() const {return myObjects;}
 
170
 
 
171
        //! If true, the collision geometry of the robot will be rendered
 
172
        inline void renderCollisionGeometry(bool renderIt) {
 
173
 
 
174
                for (unsigned int i=0; i<myBodies.size(); i++)
 
175
                        myBodies[i]->RenderCollisionGeometry(false);
 
176
//                      myBodies[i]->renderCollisionGeometry(renderIt);
 
177
 
 
178
        }
 
179
 
 
180
        //! colors the robot -- todo: stays in conflict with RenderCollisionGeometry
 
181
        inline void colorCollisionGeometry(osg::Vec4 color) {
 
182
 
 
183
//              for (unsigned int i=0; i<myBodies.size(); i++)
 
184
//                      myBodies[i]->colorBody(color);
 
185
 
 
186
        }
 
187
 
 
188
        //! sends message to the bus, robots within the same organsim receive that message
 
189
        void sendBusMessage(const std::string& message);
 
190
 
 
191
        inline std::string getLastBusMessage(const std::string& message) const {return globalMsg;};
 
192
 
 
193
        inline void setSensorData(std::vector<SensorInitData> newSensorData) { sensorData = newSensorData; };
 
194
 
 
195
        inline void setInitialJointAngles(std::vector<float> initialJointAngles) { jointAngles = initialJointAngles;};
 
196
protected:
 
197
 
 
198
        virtual void initialize();
 
199
 
 
200
        void getConnectedBodies (std::vector<BodyBase* > &connectedBodies, BodyBase* bodyNode);
 
201
        void getConnectedRobots (std::vector<RobotActorBase* > &connectedRobots);
 
202
        osg::Vec3f myPos, myRot;                                                // position and rotation vectors of the first body element
 
203
        // register this robot and all other robots connected to this robot to the same message type
 
204
        void registerToCommBus(dtCore::UniqueId* id);
 
205
        void unregisterFromCommBus();
 
206
 
 
207
        void addBody(BodyBase* body);
 
208
        void addActuator(ActuatorBase* actuator);
 
209
        void addConnector(ConnectorBase* connector);
 
210
        void addSensor(SensorBase* sensor);
 
211
 
 
212
        dtCore::UniqueId* globalCommID;
 
213
        std::string globalMsg;
 
214
 
 
215
        bool selectionState;                                                    //is true if this robot is currently selected
 
216
        long int myID;
 
217
        int connectorStates;
 
218
        dtCore::RefPtr<osg::Group> osgGroup;                                                    // all drawables that should be detected by sensors
 
219
 
 
220
 
 
221
        std::vector<dtCore::RefPtr<SensorBase> > mySensors;
 
222
        std::vector<dtCore::RefPtr<ActuatorBase> > myActuators;
 
223
        std::vector<dtCore::RefPtr<BodyBase> > myBodies;
 
224
        std::vector<dtCore::RefPtr<ConnectorBase> > myConnectors;               // might be useful/necessary to have an extra container for the connectors
 
225
        std::vector<dtCore::RefPtr<ObjectBase> > myObjects;
 
226
 
 
227
        std::vector<SensorInitData> sensorData;
 
228
 
 
229
        //! inital joint angles
 
230
        std::vector<float> jointAngles;
 
231
 
 
232
public:
 
233
         ~RobotActorBase();
 
234
 
 
235
private:
 
236
        osg::Vec3f initPos, initRot;                                            // init position and rotation how the robot has been created
 
237
};
 
238
 
 
239
 
 
240
/**
 
241
 * Our proxy class for the robot actor.  The proxy contains properties,
 
242
 * invokables, and robot actor.
 
243
 */
 
244
class ROBOT_EXPORT RobotActorBaseProxy : public dtGame::GameActorProxy
 
245
{
 
246
public:
 
247
 
 
248
        //! Constructs the proxy.
 
249
        RobotActorBaseProxy();
 
250
 
 
251
        //! Set Robots ID
 
252
        virtual void setID(long int id);
 
253
 
 
254
        //! Get Robots ID
 
255
        virtual long int getID();
 
256
 
 
257
        virtual void setConnectorStates(int state);
 
258
        virtual int getConnectorStates();
 
259
 
 
260
 
 
261
        //! Translate Robot -- use only if physics are deactived or robot is not attached to another one
 
262
        virtual void setTranslation(osg::Vec3 pos);
 
263
 
 
264
        //! Get global translation of the robot
 
265
        virtual osg::Vec3 getTranslation();
 
266
 
 
267
        //! Rotate Robot -- use only if physics are deactived or robot is not attached to another one
 
268
        virtual void setRotation(osg::Vec3 rot);
 
269
 
 
270
        //! Get global rotation of the robot
 
271
        virtual osg::Vec3 getRotation();
 
272
 
 
273
        //! Creates the properties that are custom to the robot proxy.
 
274
        virtual void BuildPropertyMap();
 
275
 
 
276
        //! Add a sensor to robot.
 
277
        virtual void setSensors(std::vector<SensorInitData> sensors);
 
278
 
 
279
        //! Set inital joint angles
 
280
        virtual void setInitialJointAngles(std::vector<float> jointAngles);
 
281
 
 
282
protected:
 
283
        virtual ~RobotActorBaseProxy();
 
284
 
 
285
        //! Creates an instance of our robot tank actor
 
286
        virtual void CreateActor();
 
287
 
 
288
        /**
 
289
         *  Called when this proxy is added to the game manager (ie, the "world")
 
290
         * You can respond to OnEnteredWorld on either the proxy or actor or both.
 
291
         */
 
292
        virtual void OnEnteredWorld();
 
293
        
 
294
private:
 
295
        
 
296
        osg::Vec3 rot;
 
297
        
 
298
};
 
299
}
 
300
 
 
301
 
 
302
#endif /*ROBOTACTORBASE_H_*/