summaryrefslogtreecommitdiffstats
path: root/game/code/worldsim/worldphysicsmanager.h
blob: e8509cec2760952ab10d86b6530d78dfbeb597da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//=============================================================================
// Copyright (C) 2002 Radical Entertainment Ltd.  All rights reserved.
//
// File:        worldphysicsmanager.h
//
// Description: manage the world physics
//
// History:     May 1, 2002 - created, gmayer
//
//=============================================================================

#ifndef WORLDPHYSICSMANAGER_H
#define WORLDPHYSICSMANAGER_H

//========================================
// Nested Includes
//========================================

//========================================
// Forward References
//========================================
class AnimCollisionEntityDSG;
class CollisionEntityDSG;
class DynaPhysDSG;
class WorldCollisionSolverAgentManager;
class StaticPhysDSG;
class GroundPlanePool;
class FenceEntityDSG;
class Vehicle;
namespace sim
{
    class CollisionManager;
    class ManualSimState;
    class SimEnvironment;
    class SimState;
    class PhysicsProperties;
}

//=============================================================================
//
// Synopsis:    this is the key tie-in point to the gameplay context to
//              run the simulation of the game world
//
//=============================================================================
class WorldPhysicsManager
{
    public:

        // Structure thats filled out upon call to ApplyForceToDynamics
        // indicating the counts for the various types of objects that got hit
        struct NumObjectsHit
        {
            NumObjectsHit() : numNPCsHit(0) {}
            int numNPCsHit;
        };

        struct CollisionEntityDSGList
        {
            static const int NUM_COLLISION_LIST_ENTITIES = 3;

            CollisionEntityDSG* collisionEntity[NUM_COLLISION_LIST_ENTITIES];
        };

        // Static Methods for accessing this singleton.
        static WorldPhysicsManager* GetInstance();
        static WorldPhysicsManager* CreateInstance();
        static void DestroyInstance();

        // Important!
        // this method must be called before we load _anything_!
        //static void SetSimUnits();

        void Init();    // TODO - need this?
        
        void Update(unsigned int timeDeltaMilliSeconds);

        // kind of a hack?
        void SuspendForInterior();
        void ResumeForOutside();

        void OnQuitLevel();

        sim::SimEnvironment* mSimEnvironment;
        float mCollisionDistanceCGS;
        rmt::Vector mWorldUp;

        sim::CollisionManager* mCollisionManager;

        WorldCollisionSolverAgentManager* mpWorldCollisionSolverAgentManager;

        int GetCameraCollisionAreaIndex();  // return the next free one
        int GetVehicleCollisionAreaIndex();
        int GetCharacterCollisionAreaIndex();

        void FreeCollisionAreaIndex(int index);
        void FreeAllCollisionAreaIndicies();
        void EmptyCollisionAreaIndex(int index);
        static const int INVALID_COLLISION_AREA = -1;
        void SubmitStaticsPseudoCallback(rmt::Vector& position, float radius, int collisionAreaIndex, sim::SimState* callerSimState, bool allowAutoPairing = false);
        void SubmitFencePiecesPseudoCallback(rmt::Vector& position, float radius, int collisionAreaIndex, sim::SimState* callerSimState, bool allowAutoPairing = false);
        //void SubmitFencePiecesPseudoCallback(rmt::Vector& position, float radius, int collisionAreaIndex);
        void SubmitDynamicsPseudoCallback(rmt::Vector& position, float radius, int collisionAreaIndex, sim::SimState* caller, bool allowAutoPairing = false);
        
        //bool WorldPhysicsManager::IsASubmitter(sim::SimState* testSimState);
        
        void SubmitAnimCollisionsPseudoCallback(rmt::Vector& position, float radius, int collisionAreaIndex, sim::SimState* callerSimState);

        void SubmitAnimCollisionsForUpdateOnly(rmt::Vector& position, float radius, int collisionAreaIndex);

        void UpdateAnimCollisions(float dt, int collisionAreaIndex);
        void UpdateDynamicObjects(float dt, int collisionAreaIndex);


        void DisplayFencesInArea(int area);

        // need to do this when we remove a vehicle from an active collision area index...
        void RemoveFromAnyOtherCurrentDynamicsListAndCollisionArea(DynaPhysDSG* vehicle);

        // allow access to groundplanepool
        int GetNewGroundPlane(sim::SimState* simStateOwner);    // user refers to it with the returned index
        void FreeGroundPlane(int index);

        // add pair to collision
        // called by object
        //void SubmitGroundPlaneToCollisionManager(sim::SimState* simStateToPair, int groundPlaneIndex);
        //void RemoveGroundPlaneFromCollisionManager(int groundPlaneIndex);

        void EnableGroundPlaneCollision(int index);
        void DisableGroundPlaneCollision(int index);
        
        void StartTimer();
        void StopTimer();
        void ResetTimer();

        void ToggleTimerState();

        // Apply the given force to nearby objects. Returns the number of objects affected
        int ApplyForceToDynamics( int collisionAreaIndex, 
                                  const rmt::Vector& position, 
                                  float radius, 
                                  const rmt::Vector& direction, 
                                  float force, 
                                  WorldPhysicsManager::NumObjectsHit* pObjectsHit = NULL,
                                  CollisionEntityDSGList* pCollisionEntityList = NULL );

        int ApplyForceToDynamicsSpherical( int collisionAreaIndex,
                                           const rmt::Vector& position, 
                                           float radius, 
                                           float force,
                                           CollisionEntityDSGList* pCollisionEntityList = NULL );
        float GetLoopTime();

        //--------
        // statics
        //--------
        struct StaticsInCollisionDetection
        {
            StaticPhysDSG* mStaticPhysDSG;
            bool clean;
        };
        int mMaxStatics;   // per area           

        // for each collision area, need the 'clean' list
        StaticsInCollisionDetection** mCurrentStatics;

        void PurgeDynamics( int collisionAreaIndex );

        bool FenceSanityCheck(unsigned collisionAreaIndex, const rmt::Vector lastFrame, const rmt::Vector thisFrame, rmt::Vector* fixPos);

    private:
    
        // No public access to these, use singleton interface.
        WorldPhysicsManager();
        ~WorldPhysicsManager();

        void InitCollisionManager();


        void WorldSimSubstepGuts(float dt, bool firstSubstep);

        // pointer to the single instance
        static WorldPhysicsManager* spInstance;
    
        float mTotalTime;
        float mTimerTime;
        bool mTimerOn;


        void DebugInfoDisplay();

        //----------------------
        // anim collision entity
        //----------------------

        // these are for collision detection only
        struct AnimCollisionInCollisionDetection
        {
            AnimCollisionEntityDSG* mAnimCollisionEntityDSG;
            bool clean;
        };
        int mMaxAnimCollisions;

        AnimCollisionInCollisionDetection** mCurrentAnimCollisions;

        // these ones are for update only - we can and want to update stuff that is too far away to collide with, for appearance sake
        AnimCollisionInCollisionDetection** mCurrentUpdateAnimCollisions;
        int mMaxUpdateAnimCollisions;

        //---------
        // dynamics
        //---------
        struct DynamicsInCollisionDetection
        {
            //InstDynaPhysDSG* mDynamicPhysDSG;
            DynaPhysDSG* mDynamicPhysDSG;
            bool clean;
            //int groundPlaneIndex;     have to hold this in the InstDynaPhysDSG class since object could be in more than one update list
        };
        int mMaxDynamics;   // per area           

        // the 'clean' list
        //
        DynamicsInCollisionDetection** mCurrentDynamics;
        
        // this list will hold all dynamics that should be tested for collision against.
        // some will be under ai ctrl, and one's that have been collided with will be under
        // simulation ctrl
        //
        // only the simulation ctrl ones need a physics update and ground plane and a rest test
        //
        // for now... don't remove from the list until object has come to rest
     
        GroundPlanePool* mGroundPlanePool;
        void UpdateSimluatingDynamicObjectGroundPlanes();
    
        bool* mCollisionAreaAllocated;      // has been requested for use by a vehicle, character, or camera
        bool* mCollisionAreaActive;     // for intermediate, temporary disabling
        int mNumCollisionAreas;
        

        // for debugging?
        int mReservedCollisionAreas;    // reserve 0...mReservedCollisionAreas - 1

        int mMaxVehicles;
        int mMaxChars;
        int mMaxCameras;

        //-------------
        // fence pieces
        //-------------

        sim::PhysicsProperties* mFencePhysicsProperties;
        
        // have a pool of these

        struct FencePieces
        {
            sim::ManualSimState* mFenceSimState;
            bool mInCollision;
            bool mClean;
            rmt::Vector start, end;
        };
    
        FencePieces** mFences;

        // debug drawing:
        // note this only really works for one thing calling submit fence pieces!
        int mNumDebugFences;
        FenceEntityDSG** mFenceDSGResults;
        

        //sim::ManualSimState*** mFenceSimStates;   // a 2D array of pointers

        int mMaxFencePerArea;
        int* mFencesInEachArea; // ? still need this
        
        bool UpdateFencePiece(rmt::Vector& callerPosition, sim::ManualSimState* fencePiece, rmt::Vector& end0, rmt::Vector& end1, rmt::Vector& normal, sim::SimState* callerSimState);

        
        // ? bit of a hack here for player character going into interiors
        bool mInInterior;

        // current physics "frame" (for detecting duplicate updating 
        // when an object is in multip[le lists
        unsigned updateFrame;

        float mLoopTime;
        unsigned mLastTime;
};

// A little syntactic sugar for getting at this singleton.
inline WorldPhysicsManager* GetWorldPhysicsManager() { return( WorldPhysicsManager::GetInstance() ); }


         
#endif //WORLDPHYSICSMANAGER_H