3-DOF arm

by Rebecca Chen

HTML

<div id="info">3-DOF Arm
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r78/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js">


</script>
<script src="https://rawgit.com/jyunming-chen/game3js/master/js/ccdbox.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 theta1, theta2, theta3;
var link1, link2, link3, end;

var joints = [];
var axes = [];
var target = new THREE.Vector3();
////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////
init();
animate();

function setarm() {
	var axis = new CCD_axis (new THREE.Vector3(1,0,0), 0);
  axes.push (axis);
	var axis = new CCD_axis (new THREE.Vector3(0,-1,0), 1);
  axes.push (axis);
	var axis = new CCD_axis (new THREE.Vector3(0,-1,0), 2);
  axes.push (axis);
}

function fk (theta, joints) {
	joints[0] = new THREE.Vector3(0,0,0);
  
  var m = new THREE.Matrix4();
  m.makeRotationZ (Math.PI/2);
  m.multiply (new THREE.Matrix4().makeRotationX(theta[0]));
  m.multiply (new THREE.Matrix4().makeTranslation (40,0,0));
  var localzero = new THREE.Vector3(0,0,0);
  localzero.applyMatrix4(m);
  joints[1].copy (localzero);
  
  m.multiply(new THREE.Matrix4().makeRotationY (-theta[1]));
  m.multiply(new THREE.Matrix4().makeTranslation(40,0,0));
  localzero.set (0,0,0);
  localzero.applyMatrix4(m);
  joints[2].copy (localzero);
  
  m.multiply(new THREE.Matrix4().makeRotationY(-theta[2]));
  m.multiply(new THREE.Matrix4().makeTranslation(30,0,0));
  localzero.set (0,0,0);
  localzero.applyMatrix4(m);
  joints[3].copy (localzero);
}

function makeLink(length) {
  var mesh = new THREE.Mesh(new THREE.BoxGeometry(length, 5, 5), new THREE.MeshNormalMaterial());
  var link = new THREE.Object3D();
  link.add(mesh);
  mesh.position.x = length / 2;
  return link;
}

function init() {
  scene = new THREE.Scene();

  camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);
  camera.position.z = 500;
  scene.add(camera);

  var gridXZ = new THREE.GridHelper(100, 10, 'red', 'white');
  scene.add(gridXZ);

  renderer = new THREE.WebGLRenderer();
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.setClearColor(0x888888);

 ...