2-Link Arm

buttons

by j91157j91157

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://jyunming-chen.github.io/tutsplus/js/KeyboardState.js"></script>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>

<div>
  <input id='arm_range1' type='range' min=-20 max=20> A R M 1 <br>
  <input id='arm_range2' type='range' min=-20 max=20> A R M 2
</div>

CSS

body {
  overflow: hidden;
}

div {
  position: absolute;
  text-align: center;
  font-family: impact;
  font-size:150%;
  left: 43vw;
}

JavaScript

var renderer, camera, controls, scene, keyboard = new KeyboardState();
var arm1, arm2;

init();
animate();


$('#arm_range1').change( function(){
  arm1.rotation.y = $(this).val()/10;
});

$('#arm_range2').change( function(){
  arm2.rotation.y = $(this).val()/10;
});

function init() {

  renderer = new THREE.WebGLRenderer({
	  antialias: true
	});
  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.y = 40;
  camera.position.z = 50;  // important

  controls = new THREE.OrbitControls(camera, renderer.domElement);

  scene = new THREE.Scene();
  
  let axes = new THREE.AxisHelper (100);
  scene.add (axes);
  
  var gridXZ = new THREE.GridHelper(100, 10, 'red', 'white');
  scene.add(gridXZ);
  
  arm1 = buildAou();
  arm2 = arm1.clone();
  arm2.position.set (10, 0, 0);
  
  scene.add (arm1);
  arm1.add(arm2);
  
  
  //scene.add (aou2);

}

function animate() {
  keyboard.update();
  if(keyboard.down("W") || keyboard.pressed("W")) arm1.rotation.y += .01;
  else if(keyboard.down("S") || keyboard.pressed("S")) arm1.rotation.y -= .01;
  else if(keyboard.down("A") || keyboard.pressed("A")) arm2.rotation.y += .01;
  else if(keyboard.down("D") || keyboard.pressed("D")) arm2.rotation.y -= .01;
  controls.update();
  renderer.render (scene, camera);
  requestAnimationFrame (animate);
}

function buildAou () {

  var aou = new THREE.Object3D();

  var normalMat = new THREE.MeshNormalMaterial();
  var base = new THREE.Mesh( new THREE.BoxGeometry (10,4,5), normalMat );
 	
  base.position.set(5, 2, 0);
  aou.add (base);
  
  
  return aou;
}