biped
with orbitControls, XZgrid, info
by Millieyan
HTML
<div id="info">biped walking
</div>
<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/game3js/js/ccdsys.js"></script>
CSS
#info {
position: absolute;
top: 0px;
width: 100%;
padding: 10px;
text-align: center;
color: #ffff00
}
body {
overflow: hidden;
}
JavaScript
class biped{
constructor(){
this.theta1=0;
this.theta2=0;
}
init() {
let body = new THREE.Object3D();
let mat = new THREE.MeshNormalMaterial();
this.thigh = new THREE.Object3D();
let thighMesh = new THREE.Mesh (new THREE.BoxGeometry(1, L2, 1), mat)
this.thigh.add (thighMesh)
thighMesh.position.y = -L2/2
this.leg = new THREE.Object3D()
let legMesh = new THREE.Mesh (new THREE.BoxGeometry(1, L1, 1), mat)
this.leg.add (legMesh)
legMesh.position.y = -L1/2
this.leg.position.y = -L2
this.torso = new THREE.Object3D();
let torsoMesh = new THREE.Mesh (new THREE.BoxGeometry(1, Lh, 1), mat)
this.torso.add (torsoMesh)
torsoMesh.position.y = Lh/2
body.add (this.torso)
body.add (this.thigh)
this.thigh.add (this.leg)
return body;
}
update(theta) {
console.log (theta)
this.thetaGroin = theta[0]
this.thetaKnee = theta[1]
this.thigh.rotation.z = this.thetaGroin
this.leg.rotation.z = this.thetaKnee;
}
}
var camera, scene, renderer;
const LS = 5.3
const L2 = 5.2
const L1 = 4.8
const Lh = 3.3
const Hh = 8.3
const Fc=1.0
var body;
var clock = new THREE.Clock();
var Ts=clock.getElapsedTime();
init();
animate();
function fk (theta, joints) { // all memory assumed in place
joints[0].set (0,0,0);
var localzero = new THREE.Vector3(0, 0, 0);
var m = new THREE.Matrix4();
m.makeRotationZ(theta[0]);
m.multiply(new THREE.Matrix4().makeTranslation(0, -L2, 0));
localzero.applyMatrix4(m);
joints[1].copy(localzero);
localzero.set(0, 0, 0);
m.multiply(new THREE.Matrix4().makeRotationZ(theta[1]));
m.multiply(new THREE.Matrix4().makeTranslation(0, -L1, 0));
localzero.applyMatrix4(m);
joints[2].copy(localzero);
}
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...