00001 /* 00002 SagaEngine library 00003 Copyright (c) 2002-2006 Skalden Studio AS 00004 00005 This software is provided 'as-is', without any express or implied 00006 warranty. In no event will the authors be held liable for any 00007 damages arising from the use of this software. 00008 00009 Permission is granted to distribute the library under the terms of the 00010 Q Public License version 1.0. Be sure to read and understand the license 00011 before using the library. It should be included here, or you may read it 00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl 00013 00014 The original version of this library can be located at: 00015 http://www.sagaengine.com/ 00016 00017 Rune Myrland 00018 rune@skalden.com 00019 */ 00020 00021 00022 #include "PhysicsManager.hpp" 00023 #include "PhysicsAreaComponent.hpp" 00024 #include "sim_physics.hpp" 00025 #include "../area/Area.hpp" 00026 #include "../area/AreaManager.hpp" 00027 #include "../schema/SimSchema.hpp" 00028 #include "../sim.hpp" 00029 #include "../config/sim_config.hpp" 00030 00031 00032 namespace se_core { 00033 PhysicsManager 00034 ::PhysicsManager() 00035 : RootComponent(sct_PHYSICS) 00036 , movers_(new PhysicsComponent*[MAX_MOVER_COUNT]), moverCount_(0) 00037 , activeSolverCount_(0) { 00038 activeSolvers_ = new PhysicsAreaComponent*[ MAX_ACTIVE ]; 00039 } 00040 00041 00042 PhysicsManager 00043 ::~PhysicsManager() { 00044 delete[] movers_; 00045 } 00046 00047 00048 PhysicsManager& PhysicsManager 00049 ::singleton() { 00050 static PhysicsManager instance; 00051 return instance; 00052 } 00053 00054 00055 void PhysicsManager 00056 ::setSolverActive(PhysicsAreaComponent* s) { 00057 for(int i = 0; i < activeSolverCount_; ++i) { 00058 if(s == activeSolvers_[i]) return; 00059 } 00060 00061 activeSolvers_[ activeSolverCount_ ] = s; 00062 ++activeSolverCount_; 00063 } 00064 00065 00066 void PhysicsManager 00067 ::setSolverInactive(PhysicsAreaComponent* s) { 00068 for(int i = 0; i < activeSolverCount_; ++i) { 00069 if(s == activeSolvers_[i]) { 00070 --activeSolverCount_; 00071 activeSolvers_[ i ] = activeSolvers_[ activeSolverCount_ ]; 00072 activeSolvers_[ activeSolverCount_ ] = 0; 00073 } 00074 } 00075 } 00076 00077 00078 void PhysicsManager 00079 ::step(long when) { 00080 // Affect thing with effects of movements 00081 affect(when + TIMESTEP_INTERVAL); 00082 00083 // Flip coors for next physics step 00084 flip(when); 00085 00086 // Precalc coordinate for the next frame 00087 performPhysics(when + TIMESTEP_INTERVAL); 00088 00089 // Test and resolve collisions between game things and between 00090 // game things and walls or other terrain obstructions. 00091 //testCollisions(when, when + TIMESTEP_INTERVAL); 00092 00093 } 00094 00095 00096 void PhysicsManager 00097 ::cleanupGame() { 00098 // 00099 moverCount_ = 0; 00100 } 00101 00102 00103 void PhysicsManager 00104 ::flip(long when) { 00105 for(int i = 0; i < activeSolverCount_; ++i) { 00106 PhysicsAreaComponent* s = activeSolvers_[i]; 00107 s->flipChildren(); 00108 Area::Ptr solverArea(s); 00109 solverArea->flipSpawns(); 00110 } 00111 } 00112 00113 00114 void PhysicsManager 00115 ::performPhysics(long when) { 00116 PhysicsComponent** m = movers_; 00117 moverCount_ = 0; 00118 for(int i = 0; i < activeSolverCount_; ++i) { 00119 PhysicsAreaComponent* s = activeSolvers_[i]; 00120 int c = s->performChildPhysics(m); 00121 00122 s->moverCount_ = c; 00123 s->movers_ = m; 00124 00125 m += c; 00126 moverCount_ += c; 00127 } 00128 } 00129 00130 00131 00132 00133 void PhysicsManager 00134 ::affect(long when) { 00135 for(int i = 0; i < activeSolverCount_; ++i) { 00136 PhysicsAreaComponent* s = activeSolvers_[i]; 00137 s->affectChildren(); 00138 } 00139 /* 00140 for(int i = 0; i < moverCount_; ++i) { 00141 // Affect actors with the effects of the terrain 00142 // they are standing on. 00143 // PS! Placed here to avoid an extra loop. Cannot 00144 // be placed in move(), because flip may move 00145 // the actor into a new area or no area. Placing it 00146 // here should have no unwanted side effects. 00147 PhysicsComponent* p = movers_[i]; 00148 p->affect(); 00149 } 00150 */ 00151 00152 } 00153 00154 00155 } 00156
Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:11 2007 by Doxygen version 1.3.9.1.