robot play

buttons

by YouTingKuo

HTML

<div id="info">Volley Ball
  <br/>
    <button id="shoot" style="width:15%">start</button>
  <br/>
</div>
<script src="https://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>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://jyunming-chen.github.io/tutsplus/js/KeyboardState.js"></script>
<script src="https://youtingkuo.github.io/volleyball/basicScene.js"></script>
<script src="https://youtingkuo.github.io/volleyball/ball.js"></script>
<script src="https://youtingkuo.github.io/volleyball/agent.js"></script>

CSS

#info {
  position: absolute;
  top: 2%;
  width: 100%;
  padding: 10px;
  text-align: center;
  color: #ffff00
}

body {
  overflow: hidden;
}

JavaScript

var scene = new THREE.Scene();
var renderer, camera, controls;
var keyboard = new KeyboardState();
var basicScene = new BasicScene (scene);//add basic scene here
var ball = new Ball (scene);
var board, theta, phi;

init();
animate();

function init() {
  renderer = new THREE.WebGLRenderer();
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.setClearColor(0x888888);
  document.body.appendChild(renderer.domElement);

  camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);
  camera.position.set(0, 6, 15);
  controls = new THREE.OrbitControls(camera, renderer.domElement);
  
  rightBoard = new Agent (new THREE.Vector3 (0, 0, 0), 
			new THREE.Vector3(0,0,0));
  rightBoard.mesh.position.set(4.7, 1.1, 0);
  rightBoard.mesh.rotation.z = 0.75;
  scene.add(rightBoard.mesh);
  
  window.addEventListener('resize', onWindowResize, false);
}

function onWindowResize() {
  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(window.innerWidth, window.innerHeight);
}

function reset() {
	theta = Math.PI/2 + basicScene.barrel.rotation.x;
  phi = Math.PI/2 + basicScene.ballMachine.rotation.y;
	ball.v = rotation(new THREE.Vector3(gcontrols.v0, 0, 0), theta, phi);
  ball.mesh.position.copy(basicScene.barrel.localToWorld(new THREE.Vector3(0, 1, 0.105)));
}


function animate() {
  controls.update();
  keyboard.update();
  
  if (keyboard.pressed("up")) {
  	basicScene.barrel.rotation.x += 0.01;
  } else if (keyboard.pressed("down")) {
  	basicScene.barrel.rotation.x -= 0.01;
  } else if (keyboard.pressed("left")) {
  	basicScene.ballMachine.rotation.y += 0.01;
  } else if (keyboard.pressed("right")) {
  	basicScene.ballMachine.rotation.y -= 0.01;
  }

  requestAnimationFrame(animate);
  renderer.render(scene, camera);
}