DodgeBall3-test
by Bai Shiuan Huang
HTML
<div id="info">Dodege Ball
<br>使用QW選擇方向發射;
<br>Press D to increase power; Release to shoot
<p id='v0value'></p>
<button id='reset'>Reset
</button>
</div>
<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://jyunming-chen.github.io/tutsplus/js/KeyboardState.js"></script>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
CSS
#info {
position: absolute;
top: 0px;
width: 100%;
padding: 10px;
text-align: center;
color: #ffff00
}
body {
overflow: hidden;
}
JavaScript
var camera, scene, renderer, controls;
var h, L0, v0, theta, theta2 = [-Math.PI/6, 0, Math.PI/6];
var theta2_c;
var clock;
var pos, vel, force, start;
var increaseV0 = false;
var ground;
var line;
var keyboard = new KeyboardState();
var pdcontrol;
var d_ball;
var Out = [];
let returnx, returny, returnz;
class OutPlayer{
constructor(){
this.mesh = makePlayer();
this.pos = new THREE.Vector3();
this.haveBall = false;
scene.add(this.mesh);
}
hit(ball){
if(Math.sqrt((ball.mesh.position.x - this.mesh.position.x)*(ball.mesh.position.x - this.mesh.position.x) + (ball.mesh.position.z - this.mesh.position.z)*(ball.mesh.position.z - this.mesh.position.z))<= 7 && ball.mesh.position.y >= 0 && ball.mesh.position.y < 17){
console.log('In Hit!!')
for(let i = 0; i< Out.length; i++){
Out[i].haveBall = false;
}
theta2_c = 1;
this.haveBall = true;
ball.shootStart = false;
let posF_DB = new THREE.Vector3(5, 10, 0);
this.mesh.updateMatrixWorld();
this.mesh.localToWorld(posF_DB);
ball.mesh.position.copy(posF_DB);
}
}
}
class Dodgeball{
constructor(){
this.increaseV0 = false;
this.v0 = 0;
this.mesh = new THREE.Mesh(new THREE.SphereGeometry(2,20,20), new THREE.MeshNormalMaterial());
scene.add(this.mesh);
this.pos = new THREE.Vector3();
this.vel = new THREE.Vector3();
this.shootStart = false;
}
start(){
}
update(dt){
this.vel.add(force.clone().multiplyScalar(dt));
this.pos.add(this.vel.clone().multiplyScalar(dt));
//console.log('x: '+this.pos.x+' y: '+this.pos.y+' z: '+this.pos.z)
if (this.pos.y < 0) {
this.vel.y = this.vel.y * (-0.5)
this.vel.x = this.vel.x * (0.8)
this.vel.z = this.vel.z * (0.8)
console.log('x: '+this.vel.x+' y: '+this.vel.y+' z: '+this.vel.z)
this.pos.y = 0 //this correction is VERY important! (bounce back or NOT)
}
if( Math.abs(this.vel.x) <= 1e-4 && Math.abs(this.vel.z) <= 1e-4){
...