Skip to content

Commit

Permalink
added latest collision detection code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Hulha committed Nov 22, 2013
1 parent c85499a commit 9e6ea80
Show file tree
Hide file tree
Showing 18 changed files with 3,182 additions and 2,746 deletions.
143 changes: 85 additions & 58 deletions js/loadScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,10 @@ require([
//var point;
// The Loader takes care of loading data from a URL...
var loader = new DynamicLoader({world: goo.world, rootPath: 'res'});
//var loader2 = new DynamicLoader({world: goo.world, rootPath: 'res'});
//var loader3 = new DynamicLoader({world: goo.world, rootPath: 'res'});

var promises = [];
promises.push(loader.loadFromBundle('project.project', 'root.bundle'));
promises.push(loader.loadFromBundle('project.project', 'Point.bundle'));
promises.push(loader.loadFromBundle('project.project', 'zombie.bundle'));
//promises.push(loader2.loadFromBundle('project.project', 'zombie.bundle'));
//promises.push(loader3.loadFromBundle('project.project', 'zombie.bundle'));
promises.push(loader.loadFromBundle('project.project', 'Point.bundle'));
RSVP.all(promises)
.then(function(){
initGoobers(goo);
Expand All @@ -107,19 +102,26 @@ require([
point.removeFromWorld();

navMesh = generateRoomsFromMesh(loader.getCachedObjectForRef("NavMesh/entities/RootNode.entity"));
for(var i in navMesh.vert){
/*for(var i in navMesh.vert){
var p = EntityUtils.clone(goo.world, point);
p.transformComponent.setTranslation(navMesh.vert[i]);
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;
//physHull.transformComponent.children[i].entity.skip = true;
}

var groundPhys = loader.getCachedObjectForRef("GroundPhys/entities/RootNode.entity");
//groundPhys.transformComponent.transform.translation.y = -0.3;
//groundPhys.transformComponent.setUpdated();
for(var i = 0, ilen = groundPhys.transformComponent.children.length; i < ilen; i++){
groundPhys.transformComponent.children[i].entity.hitMask = 4;
//groundPhys.transformComponent.children[i].entity.skip = true;
}

Game.userEntity = goo.world.createEntity("User");
Expand All @@ -133,20 +135,17 @@ require([

Game.userEntity.setComponent(new FlashlightComponent());

function addZombie( loader, x, y, z ) {
var z = loader.getCachedObjectForRef("zombie_idle/entities/Zombie_Geo_0.entity");
//zombie.removeFromWorld(); EntityUtils.clone(goo.world, zombie); // this breaks the parent child relationship, the parent has the animation that I need...
z.transformComponent.setTranslation( x, y, z);
z.setComponent(new AIComponent(z));
z.aIComponent.addBehavior({name:"Zombie-Idle", update:ZombieIdle}, 0);
z.aIComponent.addBehavior({name:"Zombie-PathFind", update:ZombiePathFind}, 1);
}
// var z3 = EntityUtils.clone(goo.world, zombie.transformComponent.parent.entity); z3.addToWorld(); // shares animation... this sucks...

addZombie( loader, 7, 0, -50);
//addZombie( loader2, 50, 0, -50);
//addZombie( loader3, -50, 0, -50);

var zombie = loader.getCachedObjectForRef("zombie_idle/entities/Zombie_Geo_0.entity");
//zombie.removeFromWorld(); // this breaks the parent child relationship, the parent has the animation that I need...
var z2 = zombie; // EntityUtils.clone(goo.world, zombie);
z2.transformComponent.setTranslation(-2,0,2);
z2.setComponent(new AIComponent(z2));
z2.aIComponent.addBehavior({name:"Zombie-Idle", update:ZombieIdle}, 0);
z2.aIComponent.addBehavior({name:"Zombie-PathFind", update:ZombiePathFind}, 1);
// z2.addToWorld();
Game.zombie = zombie;
Game.zombieRoot = zombie.transformComponent.parent.entity;

//console.log(navRef);

goo.renderer.domElement.id = 'goo';
Expand All @@ -157,6 +156,10 @@ require([
var goal;
var viewCam;
var ray = new Ray();
var v1 = new Vector3();
var v2 = new Vector3();
var cross = new Vector3();
//var localPos = new Vector3();
// Add PickingSystem
var picking = new PickingSystem({pickLogic: new PrimitivePickLogic()});
picking.castRay = function(ray, callback, mask){
Expand All @@ -167,37 +170,36 @@ require([
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){
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];
}
// }
if(result[i].intersection.distances[j] < distance){

v1.x = result[i].intersection.vertices[j][0].x - result[i].intersection.vertices[j][1].x;
v1.y = result[i].intersection.vertices[j][0].y - result[i].intersection.vertices[j][1].y;
v1.z = result[i].intersection.vertices[j][0].z - result[i].intersection.vertices[j][1].z;


v2.x = result[i].intersection.vertices[j][2].x - result[i].intersection.vertices[j][0].x;
v2.y = result[i].intersection.vertices[j][2].y - result[i].intersection.vertices[j][0].y;
v2.z = result[i].intersection.vertices[j][2].z - result[i].intersection.vertices[j][0].z;

cross.x = (v1.y * v2.z) - (v1.z * v2.y);
cross.y = (v1.z * v2.x) - (v1.x * v2.z);
cross.z = (v1.x * v2.y) - (v1.y * v2.x);
cross.normalize();

//if(dp <=0){
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 = cross;
hit.distance = result[i].intersection.distances[j];
//}
}
}
}
}
Expand Down Expand Up @@ -241,6 +243,7 @@ require([
//
var currentRoom;
function getPathRoomToRoom(roomStart, roomGoal){
if(null == roomStart || null == roomGoal){return null;}
console.log("start:"+roomStart+" goal:"+roomGoal);
openList.push({room:roomStart, parent:null});
var pathFound = false;
Expand Down Expand Up @@ -368,6 +371,25 @@ require([
case 3:
entity.aIComponent.setActiveByName("Zombie-Idle", true);
break;
case 4:
node.goalRoom = Game.userEntity.room;

if(node.goalRoom == entity.room){
console.log("I am already in "+node.goalRoom);
node.state = 2;
return;
}
console.log("I am not in "+node.goalRoom+" getting path.");
node.path = getPathRoomToRoom(entity.room, node.goalRoom);
if(node.path != null){
node.curNode = node.path.first;
node.doorPos = navMesh.room[entity.room].door[node.curNode.door].center;
entity.aIComponent.setActiveByName("Zombie-Idle", false);
node.state = 1;
var eac = Game.zombieRoot.animationComponent;
eac.transitionTo( eac.getStates()[1]);
}
break;
}
function playerMoved(room, pos){
console.log("playerMoved:"+room+","+pos);
Expand All @@ -380,12 +402,17 @@ require([
}
console.log("I am not in "+node.goalRoom+" getting path.");
node.path = getPathRoomToRoom(entity.room, node.goalRoom);
node.curNode = node.path.first;
node.doorPos = navMesh.room[entity.room].door[node.curNode.door].center;
entity.aIComponent.setActiveByName("Zombie-Idle", false);
node.state = 1;
var eac = entity.transformComponent.parent.entity.animationComponent;
eac.transitionTo( eac.getStates()[1]);
if(node.path != null){
node.curNode = node.path.first;
node.doorPos = navMesh.room[entity.room].door[node.curNode.door].center;
entity.aIComponent.setActiveByName("Zombie-Idle", false);
node.state = 1;
var eac = Game.zombieRoot.animationComponent;
eac.transitionTo( eac.getStates()[1]);
}
else{
node.state = 4;
}
}
}

Expand Down
77 changes: 50 additions & 27 deletions lib/FPSCamComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ define([
var entityTransform;
var cam;
var camTransform;
var speed = 10;
var speed = 5;
var tmpVec = new Vector3();
var fwdBase = new Vector3(0,0,-1);
var leftBase = new Vector3(-1,0,0);
Expand All @@ -46,13 +46,14 @@ define([
var newRot = new Vector3();

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

function FPSCamComponent(){
this.type = "FPSCamComponent";
//entity = entityRef;
entityTransform = Game.userEntity.transformComponent.transform;
cam = EntityUtils.createTypicalEntity(Game.goo.world, new Camera(45, 1.0, 0.01, 130));
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 @@ -106,8 +107,7 @@ define([
function fixedUpdate(){
grounded = false;
oldPos.copy(newPos);
//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 @@ -127,54 +127,77 @@ define([
movement.y = 0;
movement.normalize(); // move the same amount regardless of where we look-

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

ray.direction.copy(wantMove);

wantMove.scale(Time.fixedDT * speed);

Vector3.add(oldPos, wantMove, wantPos);
Vector3.add(wantPos, stepHeight, ray.origin);

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();
if(hit1.distance <= 0.5){
tmpVec = Vector3.cross(hit1.normal, movement);
movement = Vector3.cross(tmpVec, hit1.normal);
var name = hit1.entity.name;
movement.y = 0;

Vector3.add(hit1.point, ray.direction.mul(-0.5), wantPos);
wantPos.y = oldPos.y;

Vector3.add(newPos, stepHeight, ray.origin);
ray.direction.copy(movement);
wantMove.copy(movement);
//wantMove.normalize();
wantPos.add(wantMove.scale(Time.fixedDT * speed));
Vector3.add(wantPos, stepHeight, ray.origin);

wantPos.normalize();

ray.direction.copy(wantMove);
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);
}}
if(hit2.distance <= 0.5){
Vector3.add(hit2.point, hit2.normal.mul(-0.5), wantPos);
wantPos.y = oldPos.y;
movement.copy(Vector3.ZERO);
}
}
}, 2);
}
}
}, 2);

//newPos.copy(wantPos);
newPos.add(movement.scale(Time.fixedDT * speed));
//Vector3.add(wantPos, movement, newPos);

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){
room = hit0.entity.navID || room;
if(hit0.distance < 1.0){
grounded = true;
}
if(true == grounded){
newPos.y = hit0.point.y;
}
}
else{
//newPos.y = oldPos.y;
console.log("Fell through...");
Game.picking.castRay(ray, function(hit3){
if(null != hit3){
if(hit3.distance <= 1.0){
grounded = true;
}
if(true == grounded){
newPos.y = hit3.point.y;
}
}
},4);
}
}, 1);
}

Expand Down
Binary file added res/Geometry/binaries/1385086209_data.bin
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/GroundPhys/binaries/1385047867_data.bin
Binary file not shown.
Binary file added res/GroundPhys/binaries/1385086129_data.bin
Binary file not shown.
Binary file added res/NavMesh/binaries/1385084232_data.bin
Binary file not shown.
Binary file added res/NavMesh/binaries/1385086104_data.bin
Binary file not shown.
Binary file added res/PhysicsHull/binaries/1385047858_data.bin
Binary file not shown.
Binary file added res/PhysicsHull/binaries/1385084263_data.bin
Binary file not shown.
Binary file added res/PhysicsHull/binaries/1385086121_data.bin
Binary file not shown.
Binary file added res/images/Night_NegX.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/images/Night_NegY.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/images/Night_NegZ.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/images/Night_PosX.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/images/Night_PosY.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/images/Night_PosZ.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 9e6ea80

Please sign in to comment.