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-2016, 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 CParticle;
50 class CTerrain;
51 class CWater;
52 class CLightManager;
53 } // namespace Gfx
54 
55 
56 enum PhysicsMode
57 {
58  MO_ADVACCEL = 0,
59  MO_RECACCEL = 1,
60  MO_STOACCEL = 2,
61  MO_MOTACCEL = 3,
62  MO_ADVSPEED = 4,
63  MO_RECSPEED = 5,
64  MO_MOTSPEED = 6,
65  MO_CURSPEED = 7,
66  MO_TERFORCE = 8,
67  MO_TERSPEED = 9,
68  MO_TERSLIDE = 10,
69  MO_REASPEED = 11,
70 };
71 
72 
73 struct Motion
74 {
75  Math::Vector advanceAccel; // acceleration starting (+)
76  Math::Vector recedeAccel; // acceleration starting (+)
77  Math::Vector stopAccel; // acceleration stoping (+)
78  Math::Vector motorAccel; // current acceleration (+/-)
79 
80  Math::Vector advanceSpeed; // forward speed (+)
81  Math::Vector recedeSpeed; // reversing speed (+)
82  Math::Vector motorSpeed; // desired speed (+/-)
83  Math::Vector currentSpeed; // current speed (+/-)
84 
85  Math::Vector terrainForce; // power of resistance of the ground (+)
86  Math::Vector terrainSpeed; // speed of the ground (+/-)
87  Math::Vector terrainSlide; // limit sliding speed (+)
88 
89  Math::Vector realSpeed; // real speed(+/-)
90 
91  Math::Vector finalInclin; // final inclination
92 };
93 
94 
95 
96 
97 class CPhysics
98 {
99 public:
100  CPhysics(COldObject* object);
101  ~CPhysics();
102 
103  void DeleteObject(bool bAll=false);
104 
105  bool EventProcess(const Event &event);
106 
107  void SetMotion(CMotion* motion);
108 
109  bool Write(CLevelParserLine* line);
110  bool Read(CLevelParserLine* line);
111 
112  void SetGravity(float value);
113  float GetGravity();
114 
115  float GetFloorHeight();
116 
117  void SetLinMotion(PhysicsMode mode, Math::Vector value);
118  Math::Vector GetLinMotion(PhysicsMode mode);
119  void SetLinMotionX(PhysicsMode mode, float value);
120  void SetLinMotionY(PhysicsMode mode, float value);
121  void SetLinMotionZ(PhysicsMode mode, float value);
122  float GetLinMotionX(PhysicsMode mode);
123  float GetLinMotionY(PhysicsMode mode);
124  float GetLinMotionZ(PhysicsMode mode);
125 
126  void SetCirMotion(PhysicsMode mode, Math::Vector value);
127  Math::Vector GetCirMotion(PhysicsMode mode);
128  void SetCirMotionX(PhysicsMode mode, float value);
129  void SetCirMotionY(PhysicsMode mode, float value);
130  void SetCirMotionZ(PhysicsMode mode, float value);
131  float GetCirMotionX(PhysicsMode mode);
132  float GetCirMotionY(PhysicsMode mode);
133  float GetCirMotionZ(PhysicsMode mode);
134 
135  float GetLinStopLength(PhysicsMode sMode=MO_ADVSPEED, PhysicsMode aMode=MO_STOACCEL);
136  float GetCirStopLength();
137  float GetLinMaxLength(float dir);
138  float GetLinTimeLength(float dist, float dir=1.0f);
139  float GetLinLength(float dist);
140 
141  void SetMotor(bool bState);
142  bool GetMotor();
143  void SetLand(bool bState);
144  bool GetLand();
145  void SetSwim(bool bState);
146  bool GetSwim();
147  void SetCollision(bool bCollision);
148  bool GetCollision();
149  void SetFreeze(bool bFreeze);
150  bool GetFreeze();
151 
152  void SetMotorSpeed(Math::Vector speed);
153  void SetMotorSpeedX(float speed);
154  void SetMotorSpeedY(float speed);
155  void SetMotorSpeedZ(float speed);
156  Math::Vector GetMotorSpeed();
157  float GetMotorSpeedX();
158  float GetMotorSpeedY();
159  float GetMotorSpeedZ();
160 
161  Error GetError();
162 
163  float GetFallingHeight();
164 
165  void SetMinFallingHeight(float value);
166  float GetMinFallingHeight();
167 
168  void SetFallDamageFraction(float value);
169  float GetFallDamageFraction();
170 
171 protected:
172  bool EventFrame(const Event &event);
173  void WaterFrame(float aTime, float rTime);
174  void SoundMotor(float rTime);
175  void SoundMotorFull(float rTime, ObjectType type);
176  void SoundMotorSlow(float rTime, ObjectType type);
177  void SoundMotorStop(float rTime, ObjectType type);
178  void SoundReactorFull(float rTime, ObjectType type);
179  void SoundReactorStop(float rTime, ObjectType type);
180  void FrameParticle(float aTime, float rTime);
181  void MotorUpdate(float aTime, float rTime);
182  void EffectUpdate(float aTime, float rTime);
183  void UpdateMotionStruct(float rTime, Motion &motion);
184  void FloorAdapt(float aTime, float rTime, Math::Vector &pos, Math::Vector &angle);
185  void FloorAngle(const Math::Vector &pos, Math::Vector &angle);
186  int ObjectAdapt(const Math::Vector &pos, const Math::Vector &angle);
187  bool JostleObject(CJostleableObject* pObj, Math::Vector iPos, float iRad);
188  bool JostleObject(CObject* pObj, float force);
189  bool ExploOther(ObjectType iType, CObject *pObj, ObjectType oType, float force);
190  int ExploHimself(ObjectType iType, ObjectType oType, float force);
191 
192  void PowerParticle(float factor, bool bBreak);
193  void CrashParticle(float crash);
194  void MotorParticle(float aTime, float rTime);
195  void WaterParticle(float aTime, Math::Vector pos, ObjectType type, float floor, float advance, float turn);
196  void WheelParticle(TraceColor color, float width);
197  void SetFalling();
198 
199 protected:
200  Gfx::CEngine* m_engine;
201  Gfx::CLightManager* m_lightMan;
202  Gfx::CParticle* m_particle;
203  Gfx::CTerrain* m_terrain;
204  Gfx::CWater* m_water;
205  Gfx::CCamera* m_camera;
206  CSoundInterface* m_sound;
207 
208  COldObject* m_object;
209  CMotion* m_motion;
210 
211  float m_gravity; // force of gravity
212  float m_time; // absolute time
213  Math::Vector m_motorSpeed; // motor speed (-1..1)
214  Motion m_linMotion; // linear motion
215  Motion m_cirMotion; // circular motion
216  bool m_bMotor;
217  bool m_bLand;
218  bool m_bSwim;
219  bool m_bCollision;
220  bool m_bObstacle;
221  bool m_bFreeze;
222  int m_repeatCollision;
223  float m_linVibrationFactor;
224  float m_cirVibrationFactor;
225  float m_inclinaisonFactor;
226  float m_lastPowerParticle;
227  float m_lastSlideParticle;
228  float m_lastMotorParticle;
229  float m_lastWaterParticle;
230  float m_lastUnderParticle;
231  float m_lastPloufParticle;
232  float m_lastFlameParticle;
233  bool m_bWheelParticleBrake;
234  Math::Vector m_wheelParticlePos[2];
235  float m_absorbWater;
236  float m_reactorTemperature;
237  float m_timeReactorFail;
238  float m_timeUnderWater;
239  float m_lastEnergy;
240  float m_lastSoundWater;
241  float m_lastSoundInsect;
242  float m_restBreakParticle;
243  float m_floorLevel; // ground level
244  float m_floorHeight; // height above the ground
245  int m_soundChannel;
246  int m_soundChannelSlide;
247  float m_soundTimePshhh;
248  float m_soundTimeJostle;
249  float m_soundTimeBoum;
250  bool m_bSoundSlow;
251  bool m_bForceUpdate;
252  bool m_bLowLevel;
253  float m_fallingHeight;
254  float m_fallDamageFraction;
255  float m_minFallingHeight;
256 };
Definition: old_object.h:79
Definition: physics.h:97
Definition: physics.h:73
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:128
Terrain loader/generator and manager.
Definition: terrain.h:147
Namespace for (new) graphics code.
Definition: app.h:49
Definition: motion.h:47
Vector struct and related functions.
The graphics engine.
Definition: engine.h:585
ObjectType
Type of game object.
Definition: object_type.h:33
Water manager/renderer.
Definition: water.h:72
3D (3x1) vector
Definition: vector.h:53
Event sent by system, interface or game.
Definition: event.h:735
Base class for all 3D in-game objects.
Definition: object.h:63
Sound plugin interface.
Definition: sound.h:60