Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
physics.h
Go to the documentation of this file.
1 // * This file is part of the COLOBOT source code
2 // * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
3 // * Copyright (C) 2012, Polish Portal of Colobot (PPC)
4 // *
5 // * This program is free software: you can redistribute it and/or modify
6 // * it under the terms of the GNU General Public License as published by
7 // * the Free Software Foundation, either version 3 of the License, or
8 // * (at your option) any later version.
9 // *
10 // * This program is distributed in the hope that it will be useful,
11 // * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // * GNU General Public License for more details.
14 // *
15 // * You should have received a copy of the GNU General Public License
16 // * along with this program. If not, see http://www.gnu.org/licenses/.
17 
23 #pragma once
24 
25 
26 #include "common/global.h"
27 
28 #include "object/object.h"
29 
30 #include "math/vector.h"
31 
32 
33 class CObject;
34 class CBrain;
35 class CMotion;
36 class CSoundInterface;
37 
38 namespace Gfx {
39 class CCamera;
40 class CEngine;
41 class CLight;
42 class CParticle;
43 class CTerrain;
44 class CWater;
45 }
46 
47 
48 enum PhysicsType
49 {
50  TYPE_ROLLING = 1,
51  TYPE_FLYING = 2,
52 };
53 
54 enum PhysicsMode
55 {
56  MO_ADVACCEL = 0,
57  MO_RECACCEL = 1,
58  MO_STOACCEL = 2,
59  MO_MOTACCEL = 3,
60  MO_ADVSPEED = 4,
61  MO_RECSPEED = 5,
62  MO_MOTSPEED = 6,
63  MO_CURSPEED = 7,
64  MO_TERFORCE = 8,
65  MO_TERSPEED = 9,
66  MO_TERSLIDE = 10,
67  MO_REASPEED = 11,
68 };
69 
70 
71 struct Motion
72 {
73  Math::Vector advanceAccel; // acceleration starting (+)
74  Math::Vector recedeAccel; // acceleration starting (+)
75  Math::Vector stopAccel; // acceleration stoping (+)
76  Math::Vector motorAccel; // current acceleration (+/-)
77 
78  Math::Vector advanceSpeed; // forward speed (+)
79  Math::Vector recedeSpeed; // reversing speed (+)
80  Math::Vector motorSpeed; // desired speed (+/-)
81  Math::Vector currentSpeed; // current speed (+/-)
82 
83  Math::Vector terrainForce; // power of resistance of the ground (+)
84  Math::Vector terrainSpeed; // speed of the ground (+/-)
85  Math::Vector terrainSlide; // limit sliding speed (+)
86 
87  Math::Vector realSpeed; // real speed(+/-)
88 
89  Math::Vector finalInclin; // final inclination
90 };
91 
92 
93 
94 
95 class CPhysics
96 {
97 public:
98  CPhysics(CObject* object);
99  ~CPhysics();
100 
101  void DeleteObject(bool bAll=false);
102 
103  bool EventProcess(const Event &event);
104 
105  void SetBrain(CBrain* brain);
106  void SetMotion(CMotion* motion);
107 
108  void SetType(PhysicsType type);
109  PhysicsType GetType();
110 
111  bool Write(char *line);
112  bool Read(char *line);
113 
114  void SetGravity(float value);
115  float GetGravity();
116 
117  float GetFloorHeight();
118 
119  void SetLinMotion(PhysicsMode mode, Math::Vector value);
120  Math::Vector GetLinMotion(PhysicsMode mode);
121  void SetLinMotionX(PhysicsMode mode, float value);
122  void SetLinMotionY(PhysicsMode mode, float value);
123  void SetLinMotionZ(PhysicsMode mode, float value);
124  float GetLinMotionX(PhysicsMode mode);
125  float GetLinMotionY(PhysicsMode mode);
126  float GetLinMotionZ(PhysicsMode mode);
127 
128  void SetCirMotion(PhysicsMode mode, Math::Vector value);
129  Math::Vector GetCirMotion(PhysicsMode mode);
130  void SetCirMotionX(PhysicsMode mode, float value);
131  void SetCirMotionY(PhysicsMode mode, float value);
132  void SetCirMotionZ(PhysicsMode mode, float value);
133  float GetCirMotionX(PhysicsMode mode);
134  float GetCirMotionY(PhysicsMode mode);
135  float GetCirMotionZ(PhysicsMode mode);
136 
137  float GetLinStopLength(PhysicsMode sMode=MO_ADVSPEED, PhysicsMode aMode=MO_STOACCEL);
138  float GetCirStopLength();
139  float GetLinMaxLength(float dir);
140  float GetLinTimeLength(float dist, float dir=1.0f);
141  float GetLinLength(float dist);
142 
143  void SetMotor(bool bState);
144  bool GetMotor();
145  void SetLand(bool bState);
146  bool GetLand();
147  void SetSwim(bool bState);
148  bool GetSwim();
149  void SetCollision(bool bCollision);
150  bool GetCollision();
151  void SetFreeze(bool bFreeze);
152  bool GetFreeze();
153  void SetReactorRange(float range);
154  float GetReactorRange();
155 
156  void SetMotorSpeed(Math::Vector speed);
157  void SetMotorSpeedX(float speed);
158  void SetMotorSpeedY(float speed);
159  void SetMotorSpeedZ(float speed);
160  Math::Vector GetMotorSpeed();
161  float GetMotorSpeedX();
162  float GetMotorSpeedY();
163  float GetMotorSpeedZ();
164 
165  void CreateInterface(bool bSelect);
166  Error GetError();
167 
168  float GetFallingHeight();
169 
170  void SetMinFallingHeight(float value);
171  float GetMinFallingHeight();
172 
173  void SetFallDamageFraction(float value);
174  float GetFallDamageFraction();
175 
176 protected:
177  bool EventFrame(const Event &event);
178  void WaterFrame(float aTime, float rTime);
179  void SoundMotor(float rTime);
180  void SoundMotorFull(float rTime, ObjectType type);
181  void SoundMotorSlow(float rTime, ObjectType type);
182  void SoundMotorStop(float rTime, ObjectType type);
183  void SoundReactorFull(float rTime, ObjectType type);
184  void SoundReactorStop(float rTime, ObjectType type);
185  void FrameParticle(float aTime, float rTime);
186  void MotorUpdate(float aTime, float rTime);
187  void EffectUpdate(float aTime, float rTime);
188  void UpdateMotionStruct(float rTime, Motion &motion);
189  void FloorAdapt(float aTime, float rTime, Math::Vector &pos, Math::Vector &angle);
190  void FloorAngle(const Math::Vector &pos, Math::Vector &angle);
191  int ObjectAdapt(const Math::Vector &pos, const Math::Vector &angle);
192  bool JostleObject(CObject* pObj, Math::Vector iPos, float iRad, Math::Vector oPos, float oRad);
193  bool JostleObject(CObject* pObj, float force);
194  bool ExploOther(ObjectType iType, CObject *pObj, ObjectType oType, float force);
195  int ExploHimself(ObjectType iType, ObjectType oType, float force);
196 
197  void PowerParticle(float factor, bool bBreak);
198  void CrashParticle(float crash);
199  void MotorParticle(float aTime, float rTime);
200  void WaterParticle(float aTime, Math::Vector pos, ObjectType type, float floor, float advance, float turn);
201  void WheelParticle(int color, float width);
202  void SetFalling();
203 
204 protected:
205  Gfx::CEngine* m_engine;
206  Gfx::CLightManager* m_lightMan;
207  Gfx::CParticle* m_particle;
208  Gfx::CTerrain* m_terrain;
209  Gfx::CWater* m_water;
210  Gfx::CCamera* m_camera;
211  CObject* m_object;
212  CBrain* m_brain;
213  CMotion* m_motion;
214  CSoundInterface* m_sound;
215 
216  PhysicsType m_type; // TYPE_*
217  float m_gravity; // force of gravity
218  float m_time; // absolute time
219  Math::Vector m_motorSpeed; // motor speed (-1..1)
220  Motion m_linMotion; // linear motion
221  Motion m_cirMotion; // circular motion
222  bool m_bMotor;
223  bool m_bLand;
224  bool m_bSwim;
225  bool m_bCollision;
226  bool m_bObstacle;
227  bool m_bFreeze;
228  int m_repeatCollision;
229  float m_linVibrationFactor;
230  float m_cirVibrationFactor;
231  float m_inclinaisonFactor;
232  float m_lastPowerParticle;
233  float m_lastSlideParticle;
234  float m_lastMotorParticle;
235  float m_lastWaterParticle;
236  float m_lastUnderParticle;
237  float m_lastPloufParticle;
238  float m_lastFlameParticle;
239  bool m_bWheelParticleBrake;
240  Math::Vector m_wheelParticlePos[2];
241  float m_absorbWater;
242  float m_reactorTemperature;
243  float m_reactorRange;
244  float m_timeReactorFail;
245  float m_timeUnderWater;
246  float m_lastEnergy;
247  float m_lastSoundWater;
248  float m_lastSoundInsect;
249  float m_restBreakParticle;
250  float m_floorLevel; // ground level
251  float m_floorHeight; // height above the ground
252  int m_soundChannel;
253  int m_soundChannelSlide;
254  float m_soundTimePshhh;
255  float m_soundTimeJostle;
256  float m_soundTimeBoum;
257  bool m_bSoundSlow;
258  bool m_bForceUpdate;
259  bool m_bLowLevel;
260  float m_fallingHeight;
261  float m_fallDamageFraction;
262  float m_minFallingHeight;
263 };
264