1 /***************************************************************************
2                           movingobject.h  -  Moving object class
3                              -------------------
4     begin                : Wed Dec 4 2002
5     copyright            : (C) 2002 by CJP
6     email                : cornware-cjp@users.sourceforge.net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifndef MOVINGOBJECT_H
19 #define MOVINGOBJECT_H
20 
21 
22 /**
23   *@author CJP
24   */
25 
26 #include <vector>
27 namespace std {}
28 using namespace std;
29 
30 #include "message.h"
31 #include "movobjinput.h"
32 #include "dataobject.h"
33 #include "chatmessage.h"
34 
35 #include "collisiondata.h"
36 #include "collisionface.h"
37 
38 #include "vector.h"
39 #include "matrix.h"
40 #include "body.h"
41 
42 class CPhysics;
43 
44 class CMovingObject : public CDataObject, CMessage
45 {
46 public:
47 	CMovingObject(CDataManager *manager);
48 	~CMovingObject();
49 
50 	virtual bool load(const CString &filename, const CParamList &list);
51 	virtual void unload();
52 
53 	//physics simulation
54 	virtual void update(CPhysics *simulator, float dt);
55 	//collision response
56 	virtual void correctCollisions();
57 
58 	//For network transfer & other stuff
59 	virtual CBinBuffer &getData(CBinBuffer &b) const;             //puts body positions etc. into buffer
60 	virtual bool setData(const CBinBuffer &b, unsigned int &pos); //rebuild class data from binbuffer
getType()61 	virtual CMessageBuffer::eMessageType getType() const {return CMessageBuffer::movingObject;}
62 
63 	//CMessage wrappers:
getBuffer()64 	CMessageBuffer getBuffer(){return CMessage::getBuffer();}
setBuffer(const CMessageBuffer & b)65 	bool setBuffer(const CMessageBuffer &b){return CMessage::setBuffer(b);}
66 
getMovObjID()67 	unsigned int getMovObjID()
68 		{return m_MovObjID;}
69 
70 	//should be called after setting object position, orientation
71 	virtual void resetBodyPositions()=0;
72 
getCameraPos()73 	const CVector &getCameraPos() const
74 		{return m_CameraPos;}
75 
76 	//static data:
77 	vector<unsigned int> m_Sounds; //The object sounds
78 	vector<unsigned int> m_Textures; //The object textures
79 
80 	//dynamic data:
81 	vector<CBody> m_Bodies; //The object bodies
82 	CMovObjInput *m_InputData;
83 
84 	//Messages to player:
85 	vector<CChatMessage> m_IncomingMessages;
86 
87 	//This one is used in correctCollisions(). Only the collisions of one simulation step.
88 	vector<CCollisionData> m_SimCollisions;
89 	//This is used in e.g. the sound subsystem
90 	vector<CCollisionData> m_AllCollisions;
91 
92 
93 	//State variables:
94 	CVector m_Velocity, m_AngularVelocity;
95 	CVector m_Position;
96 	CMatrix m_OrientationMatrix;
97 
98 	//static data:
getInvMass()99 	float getInvMass() const
100 		{return m_InvMass;}
101 
102 	//Timing of network communication
103 	float m_LastUpdateTime, m_LastNetworkUpdateTime;
104 
105 protected:
106 	CVector m_CameraPos; //the relative position of the inside camera
107 
108 	Uint8 m_MovObjID;
109 
110 	//Force accumulation:
111 	void addForce(CVector F);
112 	void addTorque(CVector M);
113 	void addForceAt(CVector F, CVector pos);
114 
115 	//placing the bodies:
116 	virtual void placeBodies() = 0;
117 
118 	//static data:
119 	float m_InvMass;
120 	CMatrix m_InvInertia;
121 
122 	CVector m_Ftot, m_Mtot;
123 
124 	//Collision data for misc. simulation purposes:
125 	virtual void determineGroundPlane(CPhysics *simulator);
126 	CCollisionFace m_Ground; //the ground plane ( |normal| < 0.5 is no plane)
127 };
128 
129 #endif
130