I am struggling with a three.js scene that features a 3D Homer Simpson standing on a plane. My goal is to rotate him around his y-axis using the mouse.
The code I have put together is almost there, with pieces borrowed from various sources. However, I am facing an issue where I cannot rotate him around his y-axis that should be running through his core.
Currently, when I use the frame as a reference, he spins in a circular motion with the radius equalling the size of the plane. But, if I rotate using the model instead of the frame, he almost rotates around his y-axis, although slightly offset.
I am utilizing the following model. The rotation I am trying to achieve is similar to the one demonstrated in the 3D Warehouse example. By clicking on 3D View, you can see the desired rotation.
// declaring necessary variables
var camera;
var frame;
var loader = new THREE.ColladaLoader();
var model;
var plane;
var renderer;
var scene;
var mouseX = 0;
var mouseXOnMouseDown = 0;
var targetRotation = 0;
var targetRotationOnMouseDown = 0;
var windowHalfX = 800 / 2;
// loading the model
loader.load('Homer/models/body.dae', function(collada) {
model = collada.scene;
model.position.x = 0;
model.position.y = 0;
model.position.z = 100;
model.rotation.x = - Math.PI / 2; // adjusting Homer's initial rotation
init();
animate();
});
// initializing the scene
function init()
{
renderer = new THREE.WebGLRenderer();
renderer.setSize(800, 800);
document.body.appendChild(renderer.domElement);
camera = new THREE.PerspectiveCamera(45, 1, 100, 10000);
camera.position.x = 50;
camera.position.y = 120;
camera.position.z = 600;
scene = new THREE.Scene();
scene.add(model);
// Is a frame of reference necessary for rotating Homer on his own Y-axis?
frame = new THREE.Object3D();
frame.add(model);
scene.add(frame);
plane = new THREE.Mesh( new THREE.PlaneGeometry( 400, 400 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
plane.geometry.applyMatrix( new THREE.Matrix4().makeRotationX( - Math.PI / 2 ) );
scene.add( plane );
var light = new THREE.SpotLight();
light.position.set(200, 500, 4000);
scene.add(light);
renderer.render(scene, camera);
// Implementing mouse movement to rotate Homer
document.addEventListener( 'mousedown', onDocumentMouseDown, false );
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
document.addEventListener( 'touchmove', onDocumentTouchMove, false );
}
function onDocumentMouseDown( event ) {
event.preventDefault();
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'mouseup', onDocumentMouseUp, false );
document.addEventListener( 'mouseout', onDocumentMouseOut, false );
mouseXOnMouseDown = event.clientX - windowHalfX;
targetRotationOnMouseDown = targetRotation;
}
function onDocumentMouseMove() {
mouseX = event.clientX - windowHalfX;
targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
}
function onDocumentMouseUp() {
document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
}
function onDocumentMouseOut() {
document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
}
function onDocumentTouchStart( event ) {
if ( event.touches.length === 1 ) {
event.preventDefault();
mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
targetRotationOnMouseDown = targetRotation;
}
}
function onDocumentTouchMove( event ) {
if ( event.touches.length === 1 ) {
event.preventDefault();
mouseX = event.touches[ 0 ].pageX - windowHalfX;
targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
}
}
// Rotating Homer around the Y-Axis
function animate() {
requestAnimationFrame( animate );
// When using the frame, Homer rotates in a circular motion (coincidentally matching the plane's size)
frame.rotation.y += ( targetRotation - frame.rotation.y ) * 0.03;
// Removing the frame and using the following line rotates Homer around the y-axis
// However, the y-axis runs along his leg and arm, not through his core as desired
// model.rotation.z += ( targetRotation - model.rotation.z ) * 0.03;
renderer.render( scene, camera );
}