biped

with orbitControls, XZgrid, info

by jmcjc5u

HTML

<div id="info">biped walking
</div>
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.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;
var motor1, link1, link2;
var theta0, theta1, theta2
const LS = 5.3
const L2 = 5.2
const L1 = 4.8
const Lh = 3.3
const Hh = 8.3
var mat = new THREE.MeshNormalMaterial();
var thetaGroin, thetaKnee;

init();
animate();

function makeBody () {
	let body = new THREE.Object3D();
  
  thigh = new THREE.Object3D();
  let thighMesh = new THREE.Mesh (new THREE.BoxGeometry(1, L2, 1), mat)
  thigh.add (thighMesh)
  thighMesh.position.y = -L2/2
  
  leg = new THREE.Object3D()
  let legMesh = new THREE.Mesh (new THREE.BoxGeometry(1, L1, 1), mat)
  leg.add (legMesh)
  legMesh.position.y = -L1/2
  leg.position.y = -L2
  
  torso = new THREE.Object3D();
  let torsoMesh = new THREE.Mesh (new THREE.BoxGeometry(1, Lh, 1), mat)
  torso.add (torsoMesh)
  torsoMesh.position.y = Lh/2
  
  body.add (torso)
  body.add (thigh)
  thigh.add (leg)
  return body;
}

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

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

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

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

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

  document.body.appendChild(renderer.domElement);
  ///////////////////////////////////////////////////////////
	let body = makeBody();
  scene.add (body)
  body.position.y = Hh;
  
}

function animate() {
  thetaGroin = 0.31
  thetaKnee = 0.6
  
  thigh.rotation.z = thetaGroin
  leg.rotation.z = - thetaKnee;
  
  requestAnimationFrame(animate);
  render();
}

function render() {
  renderer.render(scene, camera);
}