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

StaticDeformedMeshCollision.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/Leaf/StaticDeformedMeshCollision.h"
00027 #include "Collision/System/CollisionScene.h"
00028 #include "Collision/System/CollisionNode.h"
00029 #include "Collision/Leaf/StaticSphereCollision.h"
00030 
00031 namespace Lamp{
00032 
00033 //------------------------------------------------------------------------------
00034 // 生成、破棄
00035 //------------------------------------------------------------------------------
00036 // コンストラクタ
00037 StaticDeformedMeshCollision::StaticDeformedMeshCollision(
00038     const String& name, CollisionScene* scene) :
00039     StaticCollisionLeaf(name, scene), worldMeshChanged_(true){
00040 }
00041 //------------------------------------------------------------------------------
00042 // デストラクタ
00043 StaticDeformedMeshCollision::~StaticDeformedMeshCollision(){
00044 }
00045 //------------------------------------------------------------------------------
00046 // コピー
00047 //------------------------------------------------------------------------------
00048 // 静的変形メッシュコリジョンのコピー
00049 StaticDeformedMeshCollision*
00050     StaticDeformedMeshCollision::copyStaticDeformedMeshCollision() const{
00051     StaticDeformedMeshCollision* destination =
00052         getScene()->createStaticDeformedMeshCollision(
00053         getScene()->renameLeaf(getName()));
00054     // 静的コリジョンリーフの値をコピー
00055     copyStaticCollisionLeafValue(destination);
00056     // メンバのコピー
00057     destination->mesh_ = mesh_;
00058     destination->worldMesh_ = worldMesh_;
00059     Assert(destination->worldMeshChanged_);
00060     return destination;
00061 }
00062 //------------------------------------------------------------------------------
00063 // 交差
00064 //------------------------------------------------------------------------------
00065 // 球交差
00066 void StaticDeformedMeshCollision::intersection(
00067     IntersectionResult* result, const Sphere& sphere, u_int collisionMask){
00068     // グローバルでの有効、無効判定
00069     if(!isGlobalEnabled()){ return; }
00070     // コリジョンマスク判定
00071     if((getCollisionMask() & collisionMask) == 0){ return; }
00072     // バウンディング判定
00073     if(!worldMesh_.intersectBounding(sphere)){ return; }
00074     // メッシュ判定、ワールドメッシュの算出も行う
00075     getWorldMesh().intersectMesh(result, sphere);
00076 }
00077 //------------------------------------------------------------------------------
00078 // 球コリジョン交差
00079 void StaticDeformedMeshCollision::intersection(IntersectionResult* result,
00080     StaticSphereCollision* sphere, u_int collisionMask){
00081     // グローバルでの有効、無効判定
00082     if(!isGlobalEnabled()){ return; }
00083     // コリジョンマスク判定
00084     if((getCollisionMask() & collisionMask) == 0){ return; }
00085     // バウンディング判定
00086     const Sphere& worldSphere = sphere->getWorldSphere();
00087     if(!worldMesh_.intersectBounding(worldSphere)){ return; }
00088     // メッシュ判定
00089     if(isScaled()){
00090         // スケールありの場合はワールドメッシュを算出して交差
00091         getWorldMesh().intersectMesh(result, worldSphere);
00092     }else{
00093         // スケール無しの場合は球をローカル座標に持ってきて交差
00094         Matrix34 matrix;
00095         getWorldMatrix().invertTransformation(&matrix);
00096         Sphere localSphere = worldSphere.transform(matrix);
00097         getMesh().intersectMesh(result, localSphere);
00098     }
00099 }
00100 //------------------------------------------------------------------------------
00101 // 走査
00102 //------------------------------------------------------------------------------
00103 // 走査
00104 void StaticDeformedMeshCollision::traverseImplement(
00105     const Matrix34& parentMatrix, bool parentEnabled, bool parentScaled,
00106     bool parentChanged){
00107     // 走査のセットアップ
00108     if(!traverseSetup(parentEnabled, parentChanged)){ return; }
00109 
00110     // ワールドバウンディングの構築
00111     if(parentScaled){
00112         // スケール入りバウンディング
00113         worldMesh_.setBoundingBox(
00114             mesh_.getBoundingBox().transform(parentMatrix));
00115         worldMesh_.setBoundingSphere(
00116             mesh_.getBoundingSphere().scaledTransform(parentMatrix));
00117     }else{
00118         // スケール無しバウンディング
00119         worldMesh_.setBoundingBox(
00120             mesh_.getBoundingBox().transform(parentMatrix));
00121         worldMesh_.setBoundingSphere(
00122             mesh_.getBoundingSphere().transform(parentMatrix));
00123     }
00124 
00125     // ワールドメッシュ変更フラグを立てる
00126     worldMeshChanged_ = true;
00127 
00128     // ツリーへの挿入、またはツリー内の移動
00129 }
00130 //------------------------------------------------------------------------------
00131 // メッシュ
00132 //------------------------------------------------------------------------------
00133 // ワールドメッシュの取得
00134 const DeformedMeshGeometry& StaticDeformedMeshCollision::getWorldMesh(){
00135     // 有効でないとワールドメッシュは生成されない
00136     Assert(isGlobalEnabled());
00137 
00138     // ワールドメッシュに変更がなければそのまま返す
00139     if(!worldMeshChanged_){ return worldMesh_; }
00140     // ワールドメッシュ変更フラグのクリア
00141     worldMeshChanged_ = false;
00142 
00143     // トランスフォーム
00144     Assert(getParent() != NULL);
00145     const Matrix34& matrix = getParent()->getWorldMatrix();
00146     int triangleCount = mesh_.getTriangleCount();
00147     for(int i = 0; i < triangleCount; i++){
00148         worldMesh_.setTriangle(i, mesh_.getTriangle(i).transform(matrix));
00149     }
00150     return worldMesh_;
00151 }
00152 //------------------------------------------------------------------------------
00153 } // End of namespace Lamp
00154 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:36 2005 for Lamp by doxygen 1.3.2