00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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 }
00154