contact problem (deviceOrientation)
by jmchen
HTML
<div id="info">Contact Problem</div>
<div id='deviceInfo'> </div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r70/three.min.js"></script>
<script src="https://dl.dropboxusercontent.com/u/3587259/Code/Threejs/OrbitControls.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
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);
}
}
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);
var tt = new THREE.Matrix4();
normalW.applyMatrix4(tt.getInverse(this.mesh.matrixWorld).transpose());
return true; // for infinite plane
// for contact problem, this should check whether the ball
// rolls out of boundary
}
};
init();
animate();
function ProjVonU(v, u) { // v & u unchanged!
var uhat = u.clone().normalize();
return uhat.multiplyScalar(uhat.dot(v));
}
function Contact(ball, plane) {
// plane is immbile
// considering...