Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

407 lines (313 sloc) 13.028 kb
<!DOCTYPE html>
<html>
<head>
<title>
CubicVR.js: FPS Demo Level 1: Trigger and Event tests
</title>
<script src="../../lib/ammo.js" type="text/javascript"></script>
<script src="../../CubicVR.js" type="text/javascript"></script>
<script type='text/javascript'>
function GameLevel(levelFile) {
this.levelShape = null;
this.body = null;
this.levelModel = CubicVR.loadCollada(levelFile,"../images/");
this.playerStart = this.levelModel.getSceneObject("PlayerStart");
this.sceneObj = this.levelModel.getSceneObject("level");
this.playerStartRotation = [0,0,0];
if (this.playerStart) this.playerStartRotation = this.playerStart.rotation;
if (this.playerStart) this.playerStart = this.playerStart.position;
}
GameLevel.prototype = {
setupRigidBody: function(physics) {
var sceneObjs = this.levelModel.sceneObjects;
this.levelShape = new CubicVR.CollisionMap();
for (var i = 0; i < sceneObjs.length; i++) {
var sceneObj = sceneObjs[i];
var objMesh = sceneObj.getMesh();
if (objMesh) {
if (objMesh.name === "collisionCube-mesh") {
var body = new CubicVR.RigidBody(sceneObj,{
type: "static",
mass:0,
margin:0,
collision: {
type: "box",
size: CubicVR.vec3.multiply(sceneObj.scale,2.0),
}
});
physics.bind(body);
this.levelModel.removeSceneObject(sceneObj);
i--;
}
}
}
},
getPlayerStart: function() {
return this.playerStart||[0,0,0];
},
getPlayerStartRotation: function() {
return this.playerStartRotation||[0,0,0];
},
getLevel: function() {
return this.levelModel;
}
}
function setupPlayer(scene,physics,level) {
var playerObj = new CubicVR.SceneObject({
mesh:null,
position: level.getPlayerStart(),
rotation:[0,0,0],
name:"player"
});
var playerCollision = new CubicVR.CollisionMap({
type: "capsule",
radius:1,
height:2
});
var rigidObj = new CubicVR.RigidBody(playerObj, {
type: "dynamic",
mass: 40,
collision: playerCollision
});
scene.bind(playerObj);
physics.bind(rigidObj);
rigidObj.setAngularFactor(0);
return rigidObj;
}
function webGLStart() {
// by default generate a full screen canvas with automatic resize
var gl = CubicVR.init();
var canvas = CubicVR.getCanvas();
var kbd = CubicVR.keyboard;
var vec3 = CubicVR.vec3;
if (!gl) {
alert("Sorry, no WebGL support.");
return;
};
//----------- SCENE INIT:START -------------
CubicVR.setGlobalAmbient([0.2,0.2,0.2]);
// New scene with our canvas dimensions and default camera with FOV 80
var level = new GameLevel("fps_level1.dae");
var scene = level.getLevel();
scene.resize(canvas.width,canvas.height);
// load skybox
scene.setSkyBox(new CubicVR.SkyBox({texture:"../../tests/images/space_skybox.jpg"}));
// set initial camera position and target
scene.camera.position = [0,0,0];
//scene.camera.rotation = level.getPlayerStartRotation();
scene.camera.rotation = [0,0,0];
scene.camera.setClip(0.1,1000);
scene.camera.setFOV(75);
scene.camera.setTargeted(false);
var camParent = new CubicVR.SceneObject({position:[0,1,0]});
camParent.rotation[1] = level.getPlayerStartRotation()[1];
scene.camera.setParent(camParent);
scene.lights = [];
// Add a simple directional light
scene.bind(new CubicVR.Light({
type: "directional",
specular: [1, 1, 1],
direction: [0.6, -0.5, 0.6]
}));
// init physics manager
var physics = new CubicVR.ScenePhysics();
level.setupRigidBody(physics);
physics.setGravity([0,-15,0]);
// Add our scene to the window resize list
CubicVR.addResizeable(scene);
//----------- SCENE INIT:END -------------
// initialize a mouse view controller
var mvc = new CubicVR.MouseViewController(canvas, scene.camera);
//----------- OBJECTS AND EVENTS -----------
var player = setupPlayer(scene,physics,level);
player.getSceneObject().bindChild(camParent);
player.getSceneObject().setProperty("inventory",{});
var playerEvent = player.getSceneObject().addEvent({
id: "tick",
action: function(event) {
var lus = event.getLastUpdateSeconds();
var sceneObj = player.getSceneObject();
if (!player.isActive()) {
player.activate();
}
var linV = player.getLinearVelocity();
var mMoveSpeed = 1200*lus;
var slowDown = 500*lus;
var moved = false;
linV[0] = linV[2] = 0;
if (mvc.isKeyPressed(kbd.KEY_W)) {
linV[0] += +Math.sin(-camParent.rotation[1]*Math.PI/180);
linV[2] += -Math.cos(-camParent.rotation[1]*Math.PI/180);
moved = true;
}
if (mvc.isKeyPressed(kbd.KEY_S)) {
linV[0] += -Math.sin(-camParent.rotation[1]*Math.PI/180);
linV[2] += +Math.cos(-camParent.rotation[1]*Math.PI/180);
moved = true;
}
if (mvc.isKeyPressed(kbd.KEY_D)) {
linV[0] += +Math.sin((-camParent.rotation[1]+90)*Math.PI/180);
linV[2] += -Math.cos((-camParent.rotation[1]+90)*Math.PI/180);
moved = true;
}
if (mvc.isKeyPressed(kbd.KEY_A)) {
linV[0] += -Math.sin((-camParent.rotation[1]+90)*Math.PI/180);
linV[2] += +Math.cos((-camParent.rotation[1]+90)*Math.PI/180);
moved = true;
}
if (!moved) {
linV = player.getLinearVelocity();
linV[0] -= 0.98*linV[0]*lus*10;
linV[2] -= 0.98*linV[2]*lus*10;
} else {
var d = Math.sqrt(linV[0]*linV[0]+linV[2]*linV[2]);
if (d) {
linV[0]/=d;
linV[2]/=d;
linV[0]*=mMoveSpeed;
linV[2]*=mMoveSpeed;
}
}
player.setLinearVelocity(linV);
},
});
// Example of event trigger with sceneObject and separate collision trigger
var blueKey = scene.getSceneObject("blueKey");
var blueKeyTrigger = scene.getSceneObject("blueKey_hull");
var blueKeyTriggerMesh = blueKeyTrigger.getMesh();
var blueKeyDims = CubicVR.vec3.subtract(blueKeyTriggerMesh.bb[1],blueKeyTriggerMesh.bb[0]);
var blueKeyBody = new CubicVR.RigidBody(blueKeyTrigger,{
type: "ghost",
mass: 0,
collision: {
type: "box",
size: blueKeyDims
}
});
physics.bind(blueKeyBody);
blueKeyTrigger.visible = false;
blueKeyTrigger.addEvent({
id: "contact_ghost",
action: function(event,handler) {
var ep = event.getEventProperties();
var contacts = ep.contacts;
if (ep.contacts.indexOf(player)!==-1) {
player.getSceneObject().getProperty("inventory").blueKey = blueKey;
scene.remove(blueKey);
scene.remove(blueKeyTrigger);
handler.removeEvent(event);
}
}
});
blueKey.addEvent({
id: "tick",
properties: {
startPos: blueKey.position.slice(0)
},
action: function(event) {
var seconds = event.getSeconds();
var prop = event.getProperties();
blueKey.position = CubicVR.vec3.add(prop.startPos,[0,Math.sin(seconds*3)+0.5,0]);
blueKey.rotation = [0,seconds*20,0];
var cv = (Math.sin(seconds*3)/2)+1;
blueKey.getMesh().materials[0].ambient = [cv,cv,cv];
blueKey.getMesh().materials[1].ambient = [cv,cv,cv];
}
});
// Example of event trigger where the object is both the collision listener and the rigid body in one "blocker: true"
var door = scene.getSceneObject("door1");
door.setProperties({
open: false,
opening: false
});
var doorMesh = door.getMesh();
var doorDims = CubicVR.vec3.subtract(doorMesh.bb[1],doorMesh.bb[0]);
var doorBody = new CubicVR.RigidBody(door,{
type: "ghost",
collision: {
type: "box",
size: doorDims
},
blocker:true
});
door.addEvent({
id: "contact_ghost",
action: function(event,handler) {
var ep = event.getEventProperties();
if ((ep.contacts.indexOf(player)!==-1) && player.getSceneObject().getProperty("inventory").blueKey) {
door.setProperty("opening",true);
handler.removeEvent(event);
};
}
});
door.addEvent({
id: "tick",
interval: 1/10,
properties: {
startPos: door.position.slice(0),
},
action: function(event,handler) {
var prop = event.getProperties();
var lus = event.getLastUpdateSeconds();
if (door.getProperty("opening")) {
var startPos = prop.startPos;
var targetY = startPos[1]+doorDims[1]-0.5;
if (door.position[1]<targetY) {
door.position[1]+=lus*3;
}
if (door.position[1]>=targetY) {
door.position[1] = targetY;
door.setProperty("open",true);
event.disable();
}
doorBody.setPosition(door.position);
event.rest(0);
}
}
});
physics.bind(doorBody);
// ------------ OBJECT AND EVENTS:END ---------
//----------- MOUSE EVENTS:START -------------
var invert = false;
var mouseSpeed = 0.4;
mvc.setEvents({
mouseMove: function (ctx, mpos, mdelta, keyState) {
if (!ctx.mdown) return;
// Quick and Dirty FPS controller
var dRad = -camParent.rotation[1]*(Math.PI/180.0);
var mDist = Math.sqrt(mdelta[0]*mdelta[0]+mdelta[1]*mdelta[1]);
camParent.rotation[1] -= mdelta[0] * mouseSpeed;
if (scene.camera.rotation[0]>90) scene.camera.rotation[0] = 90;
if (scene.camera.rotation[0]<-90) scene.camera.rotation[0] = -90;
scene.camera.rotation[0] -= mdelta[1] * mouseSpeed * (invert?-1:1);
},
mouseWheel: function (ctx, mpos, wdelta, keyState) {
// ctx.zoomView(wdelta);
},
mouseDown: function (ctx, mpos, keyState) {
},
mouseUp: null,
keyDown: function(ctx,mpos,keyCode,keyState) {
if (keyCode == kbd.KEY_R) {
physics.reset();
return false;
}
},
keyPress: null,
keyUp: null
});
//----------- MOUSE EVENTS:END -------------
// Start our main drawing loop, it provides a timer and the gl context as parameters
CubicVR.MainLoop(function(timer, gl) {
var seconds = timer.getSeconds();
physics.stepSimulation(timer.getLastUpdateSeconds(),6);
scene.render();
physics.triggerEvents();
scene.runEvents(seconds);
});
}
</script>
</head>
<body onLoad="webGLStart();">
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.