DanboardStory_BasketballGameByTouch(include hoop)
by 蔡 育曄
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/84/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stats.js/r16/Stats.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js"></script>
<script src="https://jyunming-chen.github.io/tutsplus/js/KeyboardState.js"></script>
<script src ="https://yuyeh.github.io/AR_Project/three.js/myexamples/vendor/Detector.js"></script>
<script src = "https://yuyeh.github.io/AR_Project/three.js/myexamples/vendor/Cloth.js"></script>
CSS
body {
overflow: hidden;
}
JavaScript
/* testing cloth simulation(模擬) */
var pinsFormation = [];
var pins = [ 6 ];
pinsFormation.push( pins );
pins = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
pinsFormation.push( pins );
pins = [ 0 ];
pinsFormation.push( pins );
pins = []; // cut the rope ;)
pinsFormation.push( pins );
pins = [ 0, cloth.w ]; // classic 2 pins
pinsFormation.push( pins );
pins = pinsFormation[ 1 ];
class Ball {
constructor (mass = 1, radius=38/Math.PI, friction = 0) {
this.type = 'ball';
this.mass = mass; // may need to differentiate basketball & bowling ball
this.radius = radius;
this.friction = friction; // for contact
// for dynamics calculation
this.pos = new THREE.Vector3();
this.vel = new THREE.Vector3();
this.force = new THREE.Vector3();
this.count = 0;
this.obj = new THREE.Object3D();
let mesh = new THREE.Mesh (new THREE.SphereGeometry(radius, 32, 32), new THREE.MeshLambertMaterial({map:loader.load("https://i.imgur.com/Sw4OGXN.png")}))
this.obj.add (mesh)
scene.add (this.obj)
}
update(dt) {
// after GLOBAL collision & contact
this.vel.add (this.force.clone().multiplyScalar(dt))
this.pos.add (this.vel.clone().multiplyScalar(dt))
this.obj.position.copy (this.pos);
if(this.obj.position.y < 38/Math.PI) {
if(this.count >2){
tmp = !tmp;
nready = !nready;
//console.log(tmp)
this.obj.position.copy(target.getWorldPosition());
this.pos.copy(this.obj.position);
this.vel.copy(v0);
}
if(this.count ===0) {
this.vel.y *= -0.7;
this.vel.z *= 0.7;
this.vel.x *=0.7;
}
this.count ++
}
else this.count = 0;
}
// useless API
/* moveTo(x,y,z) {
this.pos.set (x,y,z)
}*/
moveTo(thePos) {
this.pos.copy (thePos);
this.obj.position.copy (this.pos);
}
rotateTo (theta) { // abs CCW angle
this.obj.rotation.z = theta;
//this.normal.applyEuler (new THREE.Euler...