contact problem (deviceOrientation)
with borders
by jmchen
HTML
<div id="info">Contact Problem
<br/>Device Orientation</div>
<div id='deviceInfo'></div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r70/three.min.js"></script>
CSS
#info {
position: absolute;
top: 0px;
width: 100%;
padding: 10px;
text-align: center;
color: #ffff00
}
#deviceInfo {
position: absolute;
bottom: 20px;
width: 100%;
padding: 10px;
text-align: center;
color: #ffff00
}
body {
overflow: hidden;
}
JavaScript
// new contact problem:
// static XZ plane
// moving gravity vector (subject to deviceOrientation)
// add shadow map
// add border first ....
//
var camera, scene, renderer, light;
var ball, plane;
var clock = new THREE.Clock();
var mouse = new THREE.Vector2();
var gravity = new THREE.Vector3();
var deviceInfo;
// class defintions here
var Ball = function () {
// properties
this.pos = new THREE.Vector3(0, 0, 0);
this.vel = new THREE.Vector3();
this.force = new THREE.Vector3();
this.mesh = new THREE.Mesh(new THREE.SphereGeometry(10, 12, 12), new THREE.MeshPhongMaterial({
color: 0xff1234,
specular: 0x444444,
shininess: 80
}));
scene.add(this.mesh);
// methods
this.update = function (dt) {
this.vel.add(this.force.clone().multiplyScalar(dt));
this.pos.add(this.vel.clone().multiplyScalar(dt));
this.mesh.position.copy(this.pos);
// border check
if (this.pos.x > 90) {
this.pos.setX (90);
this.vel.setX (0);
} else if (this.pos.x < -90) {
this.pos.setX (-90);
this.vel.setX (0);
}
if (this.pos.z > 90) {
this.pos.setZ (90);
this.vel.setZ (0);
} else if (this.pos.z < -90) {
this.pos.setZ (-90);
this.vel.setZ (0);
}
}
}
var Plane = function () {
// properties
this.pos = new THREE.Vector3(0, 10, 0); // object frame
this.normal = new THREE.Vector3(0, 1, 0); // object frame
this.mesh = new THREE.Mesh(new THREE.BoxGeometry(500, 20, 500), new THREE.MeshLambertMaterial({
transparent: true,
opacity: 0.5
}));
scene.add(this.mesh);
// methods
this.isPointOut = function (point) {
// considering plane transformation
var posW = this.pos.clone(); // in world frame
var normalW = this.normal.clone();
posW.applyMatrix4(this.mesh.matrixWorld);
...