Colobot
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-2015, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.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/error.h"
29 
30 #include "math/vector.h"
31 
32 #include "object/object_type.h"
33 
34 #include "object/interface/trace_drawing_object.h"
35 
36 
37 class CObject;
38 class COldObject;
39 class CMotion;
40 class CSoundInterface;
41 class CLevelParserLine;
42 class CJostleableObject;
43 struct Event;
44 
45 namespace Gfx
46 {
47 class CCamera;
48 class CEngine;
49 class CLight;
50 class CParticle;
51 class CTerrain;
52 class CWater;
53 class CLightManager;
54 }
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(COldObject* object);
102  ~CPhysics();
103 
104  void DeleteObject(bool bAll=false);
105 
106  bool EventProcess(const Event &event);
107 
108  void SetMotion(CMotion* motion);
109 
110  bool Write(CLevelParserLine* line);
111  bool Read(CLevelParserLine* line);
112 
113  void SetGravity(float value);
114  float GetGravity();
115 
116  float GetFloorHeight();
117 
118  void SetLinMotion(PhysicsMode mode, Math::Vector value);
119  Math::Vector GetLinMotion(PhysicsMode mode);
120  void SetLinMotionX(PhysicsMode mode, float value);
121  void SetLinMotionY(PhysicsMode mode, float value);
122  void SetLinMotionZ(PhysicsMode mode, float value);
123  float GetLinMotionX(PhysicsMode mode);
124  float GetLinMotionY(PhysicsMode mode);
125  float GetLinMotionZ(PhysicsMode mode);
126 
127  void SetCirMotion(PhysicsMode mode, Math::Vector value);
128  Math::Vector GetCirMotion(PhysicsMode mode);
129  void SetCirMotionX(PhysicsMode mode, float value);
130  void SetCirMotionY(PhysicsMode mode, float value);
131  void SetCirMotionZ(PhysicsMode mode, float value);
132  float GetCirMotionX(PhysicsMode mode);
133  float GetCirMotionY(PhysicsMode mode);
134  float GetCirMotionZ(PhysicsMode mode);
135 
136  float GetLinStopLength(PhysicsMode sMode=MO_ADVSPEED, PhysicsMode aMode=MO_STOACCEL);
137  float GetCirStopLength();
138  float GetLinMaxLength(float dir);
139  float GetLinTimeLength(float dist, float dir=1.0f);
140  float GetLinLength(float dist);
141 
142  void SetMotor(bool bState);
143  bool GetMotor();
144  void SetLand(bool bState);
145  bool GetLand();
146  void SetSwim(bool bState);
147  bool GetSwim();
148  void SetCollision(bool bCollision);
149  bool GetCollision();
150  void SetFreeze(bool bFreeze);
151  bool GetFreeze();
152 
153  void SetMotorSpeed(Math::Vector speed);
154  void SetMotorSpeedX(float speed);
155  void SetMotorSpeedY(float speed);
156  void SetMotorSpeedZ(float speed);
157  Math::Vector GetMotorSpeed();
158  float GetMotorSpeedX();
159  float GetMotorSpeedY();
160  float GetMotorSpeedZ();
161 
162  Error GetError();
163 
164  float GetFallingHeight();
165 
166  void SetMinFallingHeight(float value);
167  float GetMinFallingHeight();
168 
169  void SetFallDamageFraction(float value);
170  float GetFallDamageFraction();
171 
172 protected:
173  bool EventFrame(const Event &event);
174  void WaterFrame(float aTime, float rTime);
175  void SoundMotor(float rTime);
176  void SoundMotorFull(float rTime, ObjectType type);
177  void SoundMotorSlow(float rTime, ObjectType type);
178  void SoundMotorStop(float rTime, ObjectType type);
179  void SoundReactorFull(float rTime, ObjectType type);
180  void SoundReactorStop(float rTime, ObjectType type);
181  void FrameParticle(float aTime, float rTime);
182  void MotorUpdate(float aTime, float rTime);
183  void EffectUpdate(float aTime, float rTime);
184  void UpdateMotionStruct(float rTime, Motion &motion);
185  void FloorAdapt(float aTime, float rTime, Math::Vector &pos, Math::Vector &angle);
186  void FloorAngle(const Math::Vector &pos, Math::Vector &angle);
187  int ObjectAdapt(const Math::Vector &pos, const Math::Vector &angle);
188  bool JostleObject(CJostleableObject* pObj, Math::Vector iPos, float iRad);
189  bool JostleObject(CObject* pObj, float force);
190  bool ExploOther(ObjectType iType, CObject *pObj, ObjectType oType, float force);
191  int ExploHimself(ObjectType iType, ObjectType oType, float force);
192 
193  void PowerParticle(float factor, bool bBreak);
194  void CrashParticle(float crash);
195  void MotorParticle(float aTime, float rTime);
196  void WaterParticle(float aTime, Math::Vector pos, ObjectType type, float floor, float advance, float turn);
197  void WheelParticle(TraceColor color, float width);
198  void SetFalling();
199 
200 protected:
201  Gfx::CEngine* m_engine;
202  Gfx::CLightManager* m_lightMan;
203  Gfx::CParticle* m_particle;
204  Gfx::CTerrain* m_terrain;
205  Gfx::CWater* m_water;
206  Gfx::CCamera* m_camera;
207  CSoundInterface* m_sound;
208 
209  COldObject* m_object;
210  CMotion* m_motion;
211 
212  float m_gravity; // force of gravity
213  float m_time; // absolute time
214  Math::Vector m_motorSpeed; // motor speed (-1..1)
215  Motion m_linMotion; // linear motion
216  Motion m_cirMotion; // circular motion
217  bool m_bMotor;
218  bool m_bLand;
219  bool m_bSwim;
220  bool m_bCollision;
221  bool m_bObstacle;
222  bool m_bFreeze;
223  int m_repeatCollision;
224  float m_linVibrationFactor;
225  float m_cirVibrationFactor;
226  float m_inclinaisonFactor;
227  float m_lastPowerParticle;
228  float m_lastSlideParticle;
229  float m_lastMotorParticle;
230  float m_lastWaterParticle;
231  float m_lastUnderParticle;
232  float m_lastPloufParticle;
233  float m_lastFlameParticle;
234  bool m_bWheelParticleBrake;
235  Math::Vector m_wheelParticlePos[2];
236  float m_absorbWater;
237  float m_reactorTemperature;
238  float m_timeReactorFail;
239  float m_timeUnderWater;
240  float m_lastEnergy;
241  float m_lastSoundWater;
242  float m_lastSoundInsect;
243  float m_restBreakParticle;
244  float m_floorLevel; // ground level
245  float m_floorHeight; // height above the ground
246  int m_soundChannel;
247  int m_soundChannelSlide;
248  float m_soundTimePshhh;
249  float m_soundTimeJostle;
250  float m_soundTimeBoum;
251  bool m_bSoundSlow;
252  bool m_bForceUpdate;
253  bool m_bLowLevel;
254  float m_fallingHeight;
255  float m_fallDamageFraction;
256  float m_minFallingHeight;
257 };
Definition: old_object.h:77
Definition: physics.h:98
Definition: physics.h:74
Manager for dynamic lights in 3D scene.
Definition: lightman.h:146
ObjectType enum.
Definition: parserline.h:37
Particle engine.
Definition: particle.h:223
Interface for objects that can be jostled.
Definition: jostleable_object.h:30
Camera moving in 3D scene.
Definition: camera.h:134
Terrain loader/generator and manager.
Definition: terrain.h:145
Namespace for (new) graphics code.
Definition: app.h:49
Definition: motion.h:46
Vector struct and related functions.
The graphics engine.
Definition: engine.h:620
ObjectType
Type of game object.
Definition: object_type.h:33
Water manager/renderer.
Definition: water.h:74
3D (3x1) vector
Definition: vector.h:53
Event sent by system, interface or game.
Definition: event.h:709
Base class for all 3D in-game objects.
Definition: object.h:59
Sound plugin interface.
Definition: sound.h:57