Obxectos da escea:



	scene.add( new THREE.AxisHelper(200) );

	manivela = new THREE.Mesh( 
			new THREE.CubeGeometry( 2*m + 5, 5, 1 ),
			new THREE.MeshPhongMaterial( { color: 0x0066ff, side: THREE.DoubleSide} ) );
	scene.add( manivela );

	biela = new THREE.Mesh( 
			new THREE.CylinderGeometry(2, 2, b+5, 60, 60, false),
			new THREE.MeshPhongMaterial( { color: 0xff6600, side: THREE.DoubleSide} ) );
	scene.add( biela );

	piston = new THREE.Mesh( 
			new THREE.CylinderGeometry(10, 10, 20, 60, 60, true),
			new THREE.MeshPhongMaterial( { color: 0xffff00, side: THREE.DoubleSide} ) );
	scene.add( piston );

	camisa = new THREE.Mesh( 
			new THREE.CylinderGeometry(12, 12, 35, 60, 60, true),
			new THREE.MeshPhongMaterial( { color: 0x00ffff, side: THREE.DoubleSide,
						       opacity:0.5, transparent:true} ) );
	camisa.position.y = 63;
	scene.add( camisa );


A xestión da animación faise a partir das lonxitudes da manivela do cigüeñal (m), da biela (b) e do ángulo α variable que se forma entre o semieixe positivo das abscisas e a propia manivela; a posición e os águlos de xiro da biela e do pistón se calculan a partir de α:

Cálculos

A porción de código que xestiona a animación desenvólvese dentro da función animate:



function animate() {
   requestAnimationFrame(animate);

	angulo += 0.02;
	var m1 = m * Math.sin(angulo);
	var c  = m * Math.cos(angulo);
	var b1 = Math.sqrt(b*b - c*c);
	manivela.rotation.z = angulo;
	biela.rotation.z = Math.asin(c/b);
	biela.position.x = c/2;
	biela.position.y = (2*m1+b1)/2;
	piston.position.y = m1+b1;

   renderer.render(scene, camera);
   controls.update();
   }