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