Skip to content

Commit

Permalink
better working collision detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Hulha committed Nov 21, 2013
1 parent fd7091e commit 6f8e93d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 42 deletions.
56 changes: 44 additions & 12 deletions js/loadScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ require([
// Create typical goo application
var goo = new GooRunner({
manuallyStartGameLoop: true,
tpfSmoothingCount:1
tpfSmoothingCount:3,
showStats:true
});
Game.goo = goo;
goo.world.setSystem(new Time(goo));
Expand Down Expand Up @@ -99,19 +100,23 @@ require([
var point = loader.getCachedObjectForRef("Point/entities/RootNode.entity");
point.transformComponent.setScale(0.01, 0.01, 0.01);
point.removeFromWorld();
//viewCam = loader.getCachedObjectForRef("MultiLevel-1/entities/Camera.entity");
//viewCam.transformComponent.transform.translation.y = 2.8;

navMesh = generateRoomsFromMesh(loader.getCachedObjectForRef("NavMesh/entities/RootNode.entity"));
for(var i in navMesh.vert){
var p = EntityUtils.clone(goo.world, point);
p.transformComponent.setTranslation(navMesh.vert[i]);
//p.transformComponent.setScale(0.1, 0.2, 0.1);
p.addToWorld();
}

generateDoors(navMesh);

var physHull = loader.getCachedObjectForRef("PhysicsHull/entities/RootNode.entity");
for(var i = 0, ilen = physHull.transformComponent.children.length; i < ilen; i++){
console.log(physHull.transformComponent.children[i].entity.name);
physHull.transformComponent.children[i].entity.hitMask = 2;
physHull.transformComponent.children[i].entity.skip = true;
}

Game.userEntity = goo.world.createEntity("User");
Game.userEntity.transformComponent.transform.translation.y = 1;
Game.userEntity.addToWorld();
Expand All @@ -134,6 +139,8 @@ require([
Game.zombie = zombie;
Game.zombieRoot = zombie.transformComponent.parent.entity;

//console.log(navRef);

goo.renderer.domElement.id = 'goo';
document.body.appendChild(goo.renderer.domElement);
goo.startGameLoop();
Expand All @@ -148,22 +155,47 @@ require([
this.pickRay = ray;
this.onPick = function(result){
//console.log(result);
var hit = null;
if(null != result && result.length > 0){
var distance = Infinity;
for(var i = 0, ilen = result.length; i < ilen; i++){
//console.log(result[i].entity.name);
if(null != result[i].entity.hitMask){
if((result[i].entity.hitMask & mask) != 0){
callback({
entity:result[i].entity,
point:result[i].intersection.points[0],
vertex:result[i].intersection.vertices[0],
distance:result[i].intersection.distances[0]
});
return;
for(var j = 0, jlen = result[i].intersection.distances.length; j < jlen; j++){
var v1 = new Vector3(
result[i].intersection.vertices[j][1].x - result[i].intersection.vertices[j][0].x,
result[i].intersection.vertices[j][1].y - result[i].intersection.vertices[j][0].y,
result[i].intersection.vertices[j][1].z - result[i].intersection.vertices[j][0].z);

var v2 = new Vector3(
result[i].intersection.vertices[j][1].x - result[i].intersection.vertices[j][2].x,
result[i].intersection.vertices[j][1].y - result[i].intersection.vertices[j][2].y,
result[i].intersection.vertices[j][1].z - result[i].intersection.vertices[j][2].z);

var c = new Vector3(
(v1.y * v2.z) - (v1.z * v2.y),
(v1.z * v2.x) - (v1.x * v2.z),
(v1.x * v2.y) - (v1.y * v2.x));
c.normalize();
var dp = (this.pickRay.direction.x*c.x)+(this.pickRay.direction.y*c.y)+(this.pickRay.direction.z*c.z);
console.log(dp);
// if(dp >= 0){
if(result[i].intersection.distances[j] < distance){
distance = result[i].intersection.distances[j];
hit = hit || {entity:null,point:null,vertex:null,distance:null};
hit.entity = result[i].entity;
hit.point =result[i].intersection.points[j];
hit.normal = c;
hit.distance = result[i].intersection.distances[j];
}
// }
}
}
}
}
}
callback(null);
callback(hit);
};
this._process();
}
Expand Down
85 changes: 55 additions & 30 deletions lib/FPSCamComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ define([
var oldRot = new Vector3();
var newRot = new Vector3();

var stepHeight = new Vector3(0,1.5,0);
//var wantPos = new Vector3(0,0,0);

function FPSCamComponent(){
this.type = "FPSCamComponent";
//entity = entityRef;
entityTransform = Game.userEntity.transformComponent.transform;
cam = EntityUtils.createTypicalEntity(Game.goo.world, new Camera(45, 1, 0.1, 100));
cam = EntityUtils.createTypicalEntity(Game.goo.world, new Camera(45, 1.0, 0.01, 100));
Game.viewCam = cam;
camTransform = cam.transformComponent.transform;
cam.addToWorld();
Expand Down Expand Up @@ -103,35 +106,8 @@ define([
function fixedUpdate(){
grounded = false;
oldPos.copy(newPos);
newPos.y -= (gravity * Time.fixedDT);
Vector3.add(newPos, Vector3.UP, ray.origin);
ray.direction = Vector3.DOWN;
Game.picking.castRay(ray, function(hit){
if(null != hit){
room = hit.entity.navID;
if(hit.distance <= 1.0){
grounded = true;
}
if(true == grounded){
newPos.y = hit.point.y;
var p = new Plane();
/*
normal.set(pointB).sub(pointA);
this.normal.cross(pointC.x - pointA.x, pointC.y - pointA.y, pointC.z - pointA.z).normalize();
*/
/*
this.normal.set(pointB).subtractLocal(pointA);
this.normal.crossLocal(pointC.x - pointA.x, pointC.y - pointA.y, pointC.z - pointA.z).normalizeLocal();
*/

p.normal.set(hit.vertex[1]).sub(hit.vertex[0]);
p.normal.cross(new Vector3(hit.vertex[2].x - hit.vertex[0].x, hit.vertex[2].y - hit.vertex[0].y, hit.vertex[2].z - hit.vertex[0].z)).normalize();
//var p = new Plane().setPlanePoints(hit.vertex[0], hit.vertex[1], hit.vertex[2]);
//console.log(p.normal);
}
}
}, 1);

//wantPos.copy(newPos);

camTransform.applyForwardVector( fwdBase, direction); // get the direction the camera is looking
camTransform.applyForwardVector( leftBase, left); // get the direction to the left of the camera

Expand All @@ -150,7 +126,56 @@ define([

movement.y = 0;
movement.normalize(); // move the same amount regardless of where we look-

//wantPos.add(movement.scale(Time.fixedDT * speed));

Vector3.add(Game.userEntity.transformComponent.transform.translation, stepHeight, ray.origin);
ray.direction.copy(movement);
Game.picking.castRay(ray, function(hit1){
if(null != hit1){
if(hit1.distance < 0.5){
//console.log("1st hit "+hit1.entity.name + " @ "+hit1.distance);
//var p1 = new Plane();
//p1.normal.set(hit1.vertex[1]).sub(hit1.vertex[0]);
//p1.normal.cross(new Vector3(hit1.vertex[2].x - hit1.vertex[0].x, hit1.vertex[2].y - hit1.vertex[0].y, hit1.vertex[2].z - hit1.vertex[0].z)).normalize();
tmpVec = Vector3.cross(hit1.normal, movement);
movement = Vector3.cross(tmpVec, hit1.normal);
var name = hit1.entity.name;

Vector3.add(newPos, stepHeight, ray.origin);
ray.direction.copy(movement);
Game.picking.castRay(ray, function(hit2){
if(null != hit2){
if(hit2.distance < 0.5){
if(hit2.entity.name != hit1.entity.name){
//console.log("2nd hit "+hit2.entity.name + " @ "+hit2.distance);
//var p2 = new Plane();
//p2.normal.set(hit2.vertex[1]).sub(hit2.vertex[0]);
//p2.normal.cross(new Vector3(hit2.vertex[2].x - hit2.vertex[0].x, hit2.vertex[2].y - hit2.vertex[0].y, hit2.vertex[2].z - hit2.vertex[0].z)).normalize();
tmpVec = Vector3.cross(hit2.normal, movement);
movement = Vector3.cross(tmpVec, hit2.normal);
}}
}
}, 2);
}
}
}, 2);
newPos.add(movement.scale(Time.fixedDT * speed));

newPos.y -= (gravity * Time.fixedDT);
Vector3.add(newPos, Vector3.UP, ray.origin);
ray.direction.copy(Vector3.DOWN);
Game.picking.castRay(ray, function(hit0){
if(null != hit0){
room = hit0.entity.navID;
if(hit0.distance <= 1.0){
grounded = true;
}
if(true == grounded){
newPos.y = hit0.point.y;
}
}
}, 1);
}

function renderUpdate(){
Expand Down
Binary file removed res/images/whitewall_dirty_01_full_diff.png
Binary file not shown.

0 comments on commit 6f8e93d

Please sign in to comment.