0

I need to create animation for opening door, for this I have the loaded model for the door and I need to open it by by rotating 90 degree. Right now I have create a sample fiddle but the door is rotating about centre, how I can make it rotate about the side axis.

var camera, scene, renderer;
var mesh, pivot;

init();
animate();

function init() {

    camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
    camera.position.z = 1;

    scene = new THREE.Scene();

    var material = new THREE.MeshNormalMaterial();
    material.side = THREE.DoubleSide;

    axis = new THREE.Mesh( new THREE.PlaneGeometry( 0.01, 0.6, 0.01 ), material );
    axis.position.set(0.4, 0, .20);
    scene.add( axis );

    var door = new THREE.Mesh( new THREE.PlaneGeometry( 0.2, 0.5, 0.2 ), material );
    door.position.set( 0, 0, 0 );
    axis.add(door);

    pivot = new THREE.Group();
    pivot.position.set( 0.0, 0.0, 0 );
    axis.add( pivot );
    pivot.add( door );

    scene.add( new THREE.AxesHelper() );

    renderer = new THREE.WebGLRenderer( { antialias: true } );
    renderer.setSize( window.innerWidth, window.innerHeight );
    document.body.appendChild( renderer.domElement );

}

function animate() {

    requestAnimationFrame( animate );

        pivot.rotation.y += 0.01;

    renderer.render( scene, camera );

}

https://jsfiddle.net/95eq6vaw/3/

var camera, scene, renderer;
var mesh, pivot;

init();
animate();

function init() {

    camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
    camera.position.z = 1;

    scene = new THREE.Scene();

    var material = new THREE.MeshNormalMaterial();
    material.side = THREE.DoubleSide;

    axis = new THREE.Mesh( new THREE.PlaneGeometry( 0.01, 0.6, 0.01 ), material );
    axis.position.set(0.4, 0, .20);
    scene.add( axis );
    
    var door = new THREE.Mesh( new THREE.PlaneGeometry( 0.2, 0.5, 0.2 ), material );
    door.position.set( 0, 0, 0 );
    axis.add(door);
    
    pivot = new THREE.Group();
    pivot.position.set( 0.0, 0.0, 0 );
    axis.add( pivot );
    pivot.add( door );
    
    scene.add( new THREE.AxesHelper() );

    renderer = new THREE.WebGLRenderer( { antialias: true } );
    renderer.setSize( window.innerWidth, window.innerHeight );
    document.body.appendChild( renderer.domElement );

}

function animate() {

    requestAnimationFrame( animate );

  pivot.rotation.y += 0.01;
  
    renderer.render( scene, camera );

}
body {
   margin: 0;
}
<script src="//cdn.rawgit.com/mrdoob/three.js/master/build/three.min.js"></script>
TheJim01
  • 8,411
  • 1
  • 30
  • 54
CodeDezk
  • 1,230
  • 1
  • 12
  • 40
  • I think this answer is relevant, regarding rotating an object about a point other than its center: https://stackoverflow.com/questions/42812861/three-js-pivot-point/42866733#42866733 In your case, you would rotate the door about its hinge, so pick a point at that location as your center point. – TheJim01 Oct 03 '19 at 18:14
  • Yes I tried that, and it works on only one step, if I do the rotation in animation loop only first step is working fine from second step the object is move away from it's original position. – CodeDezk Oct 04 '19 at 09:40
  • 1
    Could you please use a [snippet](https://stackoverflow.blog/2014/09/16/introducing-runnable-javascript-css-and-html-code-snippets/) instead of jsfiddle? – gman Oct 04 '19 at 10:25

0 Answers0