Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# CollisionScene.cpp

Go to the documentation of this file.

    00001 //------------------------------------------------------------------------------
    00002 // Lamp : Open source game middleware
    00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
    00004 //
    00005 // This library is free software; you can redistribute it and/or
    00006 // modify it under the terms of the GNU Lesser General Public
    00007 // License as published by the Free Software Foundation; either
    00008 // version 2.1 of the License, or (at your option) any later version.
    00009 //
    00010 // This library is distributed in the hope that it will be useful,
    00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013 // Lesser General Public License for more details.
    00014 //
    00015 // You should have received a copy of the GNU Lesser General Public
    00016 // License along with this library; if not, write to the Free Software
    00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00018 //------------------------------------------------------------------------------
    00019
    00020 /** @file
    00021  * コリジョンシーン実装
    00022  * @author Junpee
    00023  */
    00024
    00025 #include "LampBasic.h"
    00026 #include "Collision/System/CollisionScene.h"
    00027 #include "Core/Renamer/CountRenamer.h"
    00028 #include "Collision/System/CollisionNode.h"
    00029 #include "Collision/Leaf/StaticSphereCollision.h"
    00030 #include "Collision/Leaf/StaticDeformedMeshCollision.h"
    00031
    00032 namespace Lamp{
    00033
    00034 //------------------------------------------------------------------------------
    00035 // 生成、破棄
    00036 //------------------------------------------------------------------------------
    00037 // コンストラクタ
    00038 CollisionScene::CollisionScene() : nodeDatabase_(256, 0.75f), nodeArray_(256),
    00039     leafDatabase_(256, 0.75f), leafArray_(256), tick_(0){
    00040     // リネーマの初期化
    00041     nodeRenamer_ = new CountRenamer();
    00042     nodeRenamerCallback_.scene_ = this;
    00043     leafRenamer_ = new CountRenamer();
    00044     leafRenamerCallback_.scene_ = this;
    00045     // ルートノードの初期化
    00046     rootNode_ = createCollisionNode("RootNode");
    00047 }
    00048 //------------------------------------------------------------------------------
    00049 // デストラクタ
    00050 CollisionScene::~CollisionScene(){
    00051     Assert(nodeDatabase_.getCount() == 1);
    00052     Assert(nodeArray_.getCount() == 1);
    00053     Assert(leafDatabase_.getCount() == 0);
    00054     Assert(leafArray_.getCount() == 0);
    00055     // ルートノードの後始末
    00056     if(nodeArray_.removeByValue(rootNode_) == -1){
    00057         ErrorOut("CollisionScene::~CollisionScene() "
    00058             "Not found RootNode in array");
    00059     }
    00060     if(nodeDatabase_.remove(rootNode_->getName()) == NULL){
    00061         ErrorOut("CollisionScene::~CollisionScene() "
    00062             "Not found RootNode in hashmap");
    00063     }
    00064     SafeDelete(rootNode_);
    00065     // リネーマの後始末
    00066     SafeDelete(leafRenamer_);
    00067     SafeDelete(nodeRenamer_);
    00068 }
    00069 //------------------------------------------------------------------------------
    00070 // シーン関連
    00071 //------------------------------------------------------------------------------
    00072 // 走査
    00073 void CollisionScene::traverse(){
    00074     // チックのインクリメント
    00075     tick_++;
    00076     getRootNode()->traverse();
    00077 }
    00078 //------------------------------------------------------------------------------
    00079 // 交差
    00080 //------------------------------------------------------------------------------
    00081 // 球交差
    00082 void CollisionScene::intersection(
    00083     IntersectionResult* result, const Sphere& sphere, u_int collisionMask){
    00084     int leafCount = getLeafCount();
    00085     for(int i = 0; i < leafCount; i++){
    00086         getLeaf(i)->intersection(result, sphere, collisionMask);
    00087     }
    00088 }
    00089 //------------------------------------------------------------------------------
    00090 // 球コリジョン交差
    00091 void CollisionScene::intersection(IntersectionResult* result,
    00092     StaticSphereCollision* sphere, u_int collisionMask){
    00093     Assert(sphere->isGlobalEnabled());
    00094     int leafCount = getLeafCount();
    00095     for(int i = 0; i < leafCount; i++){
    00096         CollisionLeaf* leaf = getLeaf(i);
    00097         leaf->intersection(result, sphere, collisionMask);
    00098     }
    00099 }
    00100 //------------------------------------------------------------------------------
    00101 // コリジョンノード生成
    00102 //------------------------------------------------------------------------------
    00103 // コリジョンノードの作成
    00104 CollisionNode* CollisionScene::createCollisionNode(const String& name){
    00105     // 名前の長さチェック
    00106     if(name.getSize() == 0){
    00107         ErrorOut("CollisionScene::createCollisionNode() 名前が空文字列です");
    00108         return NULL;
    00109     }
    00110     // 名前の重複チェック
    00111     if(existNodeName(name)){
    00112         ErrorOut("CollisionScene::createCollisionNode() "
    00113             "名前が重複しています %s", name.getBytes());
    00114         return NULL;
    00115     }
    00116     CollisionNode* collisionNode = new CollisionNode(name, this);
    00117     nodeDatabase_.put(name, collisionNode);
    00118     nodeArray_.add(collisionNode);
    00119     return collisionNode;
    00120 }
    00121 //------------------------------------------------------------------------------
    00122 // 静的コリジョンリーフ生成
    00123 //------------------------------------------------------------------------------
    00124 // 静的球コリジョンの作成
    00125 StaticSphereCollision* CollisionScene::createStaticSphereCollision(
    00126     const String& name){
    00127     if(!checkLeafName(name)){ return NULL; }
    00128     StaticSphereCollision* sphere = new StaticSphereCollision(name, this);
    00129     leafDatabase_.put(name, sphere);
    00130     leafArray_.add(sphere);
    00131     return sphere;
    00132 }
    00133 //------------------------------------------------------------------------------
    00134 // 静的変形メッシュコリジョンの作成
    00135 StaticDeformedMeshCollision* CollisionScene::createStaticDeformedMeshCollision(
    00136     const String& name){
    00137     if(!checkLeafName(name)){ return NULL; }
    00138     StaticDeformedMeshCollision* mesh =
    00139         new StaticDeformedMeshCollision(name, this);
    00140     leafDatabase_.put(name, mesh);
    00141     leafArray_.add(mesh);
    00142     return mesh;
    00143 }
    00144 //------------------------------------------------------------------------------
    00145 // ユーティリティ
    00146 //------------------------------------------------------------------------------
    00147 // リーフの名前チェック
    00148 bool CollisionScene::checkLeafName(const String& name){
    00149     // 名前の長さチェック
    00150     if(name.getSize() == 0){
    00151         ErrorOut("CollisionScene::checkLeafName() 名前が空文字列です");
    00152         return false;
    00153     }
    00154     // 名前の重複チェック
    00155     if(existLeafName(name)){
    00156         ErrorOut("CollisionScene::checkLeafName() 名前が重複しています %s",
    00157             name.getBytes());
    00158         return false;
    00159     }
    00160     return true;
    00161 }
    00162 //------------------------------------------------------------------------------
    00163 // コリジョンオブジェクト破棄
    00164 //------------------------------------------------------------------------------
    00165 // ノードオブジェクトの破棄
    00166 void CollisionScene::destroyNode(CollisionNode* node){
    00167     Assert(node != rootNode_);
    00168     if(nodeArray_.removeByValue(node) == -1){
    00169         ErrorOut("CollisionScene::~destroyNode() Not found node in array");
    00170     }
    00171     if(nodeDatabase_.remove(node->getName()) == NULL){
    00172         ErrorOut("CollisionScene::~destroyNode() Not found node in hashmap");
    00173     }
    00174     delete node;
    00175 }
    00176 //------------------------------------------------------------------------------
    00177 // リーフオブジェクトの破棄
    00178 void CollisionScene::destroyLeaf(CollisionLeaf* leaf){
    00179     if(leafArray_.removeByValue(leaf) == -1){
    00180         ErrorOut("CollisionScene::~destroyLeaf() Not found leaf in array");
    00181     }
    00182     if(leafDatabase_.remove(leaf->getName()) == NULL){
    00183         ErrorOut("CollisionScene::~destroyLeaf() Not found leaf in hashmap");
    00184     }
    00185     delete leaf;
    00186 }
    00187 //------------------------------------------------------------------------------
    00188 // クリア
    00189 int CollisionScene::clear(){
    00190     // ルートノードの子をクリア
    00191     int childCount = rootNode_->getChildCount();
    00192     for(int i = childCount - 1; i >= 0; i--){
    00193         rootNode_->removeChild(rootNode_->getChild(i));
    00194     }
    00195     // リーフの削除
    00196     int result = getLeafCount();
    00197     for(int i = getLeafCount() - 1; i >= 0; i--){ delete getLeaf(i); }
    00198     // ルートノード以外を破棄
    00199     result += getNodeCount();
    00200     for(int i = getNodeCount() - 1; i >= 1; i--){ delete getNode(i); }
    00201     leafArray_.clear();
    00202     leafDatabase_.clear();
    00203     nodeArray_.clear();
    00204     nodeDatabase_.clear();
    00205     nodeArray_.add(rootNode_);
    00206     nodeDatabase_.put(rootNode_->getName(), rootNode_);
    00207     return (result - 1);
    00208 }
    00209 //------------------------------------------------------------------------------
    00210 } // End of namespace Lamp
    00211 //------------------------------------------------------------------------------