Turret(atan2)
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://jyunming-chen.github.io/tutsplus/js/KeyboardState.js"></script>
CSS
body {
overflow: hidden;
}
JavaScript
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.initV = new THREE.Vector3();
this.force = new THREE.Vector3();
this.count = 0;
this.obj = new THREE.Object3D();
loader.setCrossOrigin ('');
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 <this.radius) {
this.pos.set(-20,this.radius,30);
this.vel.copy(ball.initV);
update = !update;
}
}
// useless API
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 (0,0,theta))
}
}
var renderer, camera, controls, scene, axes, stats;
var d, v0, theta,tmpV;
var loader = new THREE.TextureLoader();
var target, point, pp,power,prevPower;
var ball,tmp =true;
var update = false;
var keyboard = new KeyboardState();
var line, pLine, chargeUp = false;
var raycaster,pickables;
var reset = false;
init();
animate();
function makeLine(d) {
let dt = 0.002;
let geometry = new THREE.Geometry();
let check = false
for(pp=0; ball.pos.y > ball.radius||!check;pp++) {
geometry.vertices.push (new THREE.Vector3(ball.pos.x,ball.pos.y,ball.pos.z));
ball.vel.add (ball.force.clone().multiplyScalar(dt))
ball.pos.add...