Browse Source

MeshCollider takes original mesh as argument. MeshCollider's rayMesh extended to support Face4 faces (implemented as two rayTriangle calls)

Nik Van den Wijngaert 14 years ago
parent
commit
85fe1042b6
1 changed files with 46 additions and 22 deletions
  1. 46 22
      src/extras/physics/Collisions.js

+ 46 - 22
src/extras/physics/Collisions.js

@@ -28,13 +28,11 @@ THREE.BoxCollider = function( min, max ) {
 
 };
 
-THREE.MeshCollider = function( vertices, faces, normals, box ) {
+THREE.MeshCollider = function( mesh, box ) {
 
-	this.vertices = vertices;
-	this.faces = faces;
-	this.normals = normals;
+	this.mesh = mesh;
 	this.box = box;
-	this.numFaces = this.faces.length;
+	this.numFaces = this.mesh.geometry.faces.length;
 
 	this.normal = new THREE.Vector3();
 
@@ -144,23 +142,49 @@ THREE.CollisionSystem.prototype.rayMesh = function( r, me ) {
 
 	var d = Number.MAX_VALUE;
 
-	for( var i = 0; i < me.numFaces/3; i++ ) {
-
-		var t = i * 3;
-
-		var p0 = me.vertices[ me.faces[ t + 0 ] ];
-		var p1 = me.vertices[ me.faces[ t + 1 ] ];
-		var p2 = me.vertices[ me.faces[ t + 2 ] ];
-
-		var nd = this.rayTriangle( rt, p0, p1, p2, d, this.collisionNormal );
-
-		if( nd < d ) {
-
-			d = nd;
-			me.normal.copy( this.collisionNormal );
-			me.normal.normalize();
-
-		}
+	for( var i = 0; i < me.numFaces; i++ ) {
+        var face = me.mesh.geometry.faces[i];
+		var p0 = me.mesh.geometry.vertices[ face.a ].position;
+		var p1 = me.mesh.geometry.vertices[ face.b ].position;
+		var p2 = me.mesh.geometry.vertices[ face.c ].position;
+        var p3 = face instanceof THREE.Face4 ? me.mesh.geometry.vertices[ face.d ].position : null;
+
+        if (face instanceof THREE.Face3) {
+            var nd = this.rayTriangle( rt, p0, p1, p2, d, this.collisionNormal );
+
+            if( nd < d ) {
+
+                d = nd;
+                me.normal.copy( this.collisionNormal );
+                me.normal.normalize();
+
+            }
+        
+        }
+        
+        else if (face instanceof THREE.Face4) {
+            
+            var nd = this.rayTriangle( rt, p0, p1, p2, d, this.collisionNormal );
+            
+            if( nd < d ) {
+
+                d = nd;
+                me.normal.copy( this.collisionNormal );
+                me.normal.normalize();
+
+            }
+            
+            nd = this.rayTriangle( rt, p1, p2, p3, d, this.collisionNormal );
+            
+            if( nd < d ) {
+
+                d = nd;
+                me.normal.copy( this.collisionNormal );
+                me.normal.normalize();
+
+            }
+
+        }
 
 	}