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 #ifndef engine_area_CollisionGrid_hpp 00023 #define engine_area_CollisionGrid_hpp 00024 00025 #include "util/type/util_type.hpp" 00026 #include "sim_react.hpp" 00027 #include "sim/pos/sim_pos.hpp" 00028 #include "sim/sim.hpp" 00029 #include "sim/config/sim_config.hpp" 00030 #include "sim/thing/sim_thing.hpp" 00031 #include "sim/stat/sim_stat.hpp" 00032 #include "util/vecmath/util_vecmath.hpp" 00033 #include "util/template/SinglyLinkedList.hpp" 00034 00035 00036 00037 namespace se_core { 00038 // CollisionGridThingList 00039 // Typedef of singly linked lists container that 00040 // will hold members of all singly linked lists 00041 // of all grid containers. 00042 typedef SinglyLinkedList<class CollisionComponent, MAX_GAME_OBJECTS, 243> CGTL; 00043 struct CollisionGridCollisionComponentList : public CGTL { 00044 CollisionGridCollisionComponentList(const char* name) : CGTL(name) {} 00045 }; 00046 00047 class _SeCoreExport CollisionGrid { 00048 public: 00049 CollisionGrid(coor_tile_t width, coor_tile_t height, short depth); 00050 virtual ~CollisionGrid(); 00051 00059 void setSize(int width, int height); 00060 00061 void setOffset(const Point3& c); 00062 00070 inline int lineOffset(short level) { 00071 return 1 << level; 00072 } 00073 00080 inline coor_tile_t nodeSize(short level) { 00081 return shiftRight(rootNodeSize_, level); 00082 } 00083 00088 inline coor_tile_t halfNodeSize(short level) { 00089 return shiftRight(halfRootNodeSize_, level); 00090 } 00091 00092 inline int calcLevel(coor_tile_t size) { 00093 // Find the right level 00094 short level = 0; 00095 while((level + 1) < depth_ && halfNodeSize(level) > size) 00096 ++level; 00097 return level; 00098 } 00099 00100 00105 inline int indexAtLevel(coor_tile_t x, coor_tile_t z, short level) { 00106 Assert(level < depth_); 00107 coor_tile_t nodeX = shiftRight(x, (rootNodeShift_ - level)); 00108 coor_tile_t nodeZ = shiftRight(z, (rootNodeShift_ - level)); 00109 return nodeX + nodeZ * lineOffset(level); 00110 } 00111 00116 inline int indexAtLevelAndNode(coor_tile_t nodeX, coor_tile_t nodeZ, short level) { 00117 Assert(level < depth_); 00118 return nodeX + nodeZ * lineOffset(level); 00119 } 00120 00124 void insert(const Point3& c, coor_t size, CollisionComponent& thing); 00125 00129 bool remove(const Point3& c, coor_t size, CollisionComponent& thing); 00130 00134 void move(const Point3& from, coor_t oldSize 00135 , const Point3& to, coor_t newSize, CollisionComponent& thing); 00136 00142 short collisionCandidates(const Point3& c, coor_t size 00143 , CollisionComponent* things[], short max); 00144 00145 bool find(CollisionComponent& thing); 00146 00150 void clear(); 00151 00152 private: 00153 static coor_tile_t shiftRight(coor_tile_t value, short shift) { 00154 return value >> shift; 00155 //return value / ((coor_world_t)(1L << shift)); 00156 } 00157 00158 static coor_tile_t shiftLeft(coor_tile_t value, short shift) { 00159 return value << shift; 00160 //return value * (1L << shift); 00161 } 00162 00164 coor_tile_t xOffset_, zOffset_; 00165 00167 coor_tile_t rootNodeSize_; 00168 short rootNodeShift_; 00169 00171 short depth_; 00172 00174 coor_tile_t halfRootNodeSize_; 00175 00178 CollisionGridCollisionComponentList::iterator_type* nodes_; 00179 short totalNodeCount_; 00180 00182 CollisionGridCollisionComponentList::iterator_type** nodeLevels_; 00183 00184 static CollisionGridCollisionComponentList& thingList() { 00185 static CollisionGridCollisionComponentList instance(__FILE__); 00186 return instance; 00187 } 00188 00189 }; 00190 } 00191 00192 #endif
Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:11 2007 by Doxygen version 1.3.9.1.