Kiran Bs
Kiran Bs

Reputation: 31

Threejs rotate an object around another object in 3d matrix axis

Right now I could able to set one axis to rotate.

Working code: 
https://codepen.io/itzkinnu/full/erwKzY

How to rotate an object in random axis instead of one fixed axis.

Something like this Random rotation

Upvotes: 1

Views: 1771

Answers (2)

Kiran Bs
Kiran Bs

Reputation: 31

Thank you Rabbid76 for the quick response. It actually helped.

I have added

child.rotateY(0.02);

to the above code in animate function to rotate in random axis.

May I know how to make child box self rotate?

(function onLoad() {
  var container, loader, camera, scene, renderer, controls, mesh, child;
  
  init();
  animate();

  function init() {
    container = document.getElementById('container');
    
    renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(window.innerWidth, window.innerHeight);
    container.appendChild(renderer.domElement);

    camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 100);
    camera.position.set(0, -4, -1.5);

    loader = new THREE.TextureLoader();
    loader.setCrossOrigin("");

    scene = new THREE.Scene();
    scene.background = new THREE.Color(0xffffff);
    scene.add(camera);
    window.onresize = resize;
    
    var ambientLight = new THREE.AmbientLight(0x404040);
    scene.add(ambientLight);

    var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
    directionalLight.position.x = -0.75;
    directionalLight.position.y = -0.5;
    directionalLight.position.z = -1;
    scene.add( directionalLight );

    controls = new THREE.OrbitControls(camera, renderer.domElement);
    
    var axis = new THREE.AxesHelper(2);
    scene.add(axis);
    
    var material = new THREE.MeshPhongMaterial({color:'#f08080'});
    var geometry = new THREE.BoxGeometry( 1, 1, 1 );
    mesh = new THREE.Mesh(geometry, material);

    var material2 = new THREE.MeshPhongMaterial({color:'#8080f0'});
    var geometry2 = new THREE.BoxGeometry( 1, 1, 1 );
    child = new THREE.Mesh(geometry2, material2);
    child.position.x = 1.5;

    mesh.add(child);
    scene.add(mesh);
  }

  function resize() {
    var aspect = window.innerWidth / window.innerHeight;
    renderer.setSize(window.innerWidth, window.innerHeight);
    camera.aspect = aspect;
    camera.updateProjectionMatrix();
  }

  function animate() {
  
    child.position.set(0, 0, 0);
    child.rotateY(0.02)
    child.translateX(1.5);

    mesh.position.set(0, 0, 0);
    mesh.rotateZ(0.01);
    mesh.translateX(1.0);
    
    requestAnimationFrame(animate);
    render();
  }

  function render() {
    renderer.render(scene, camera);
  }
})();
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<div id="container"></div>

Upvotes: 0

Rabbid76
Rabbid76

Reputation: 211136

If you want an object to positioned relative to an other object, then the object has to be added as a child of the reference object.

Use Object3D.add to add a child to an object.

See the example:

(function onLoad() {
  var container, loader, camera, scene, renderer, controls, mesh, child;
  
  init();
  animate();

  function init() {
    container = document.getElementById('container');
    
    renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(window.innerWidth, window.innerHeight);
    container.appendChild(renderer.domElement);

    camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 100);
    camera.position.set(0, -4, -1.5);

    loader = new THREE.TextureLoader();
    loader.setCrossOrigin("");

    scene = new THREE.Scene();
    scene.background = new THREE.Color(0xffffff);
    scene.add(camera);
    window.onresize = resize;
    
    var ambientLight = new THREE.AmbientLight(0x404040);
    scene.add(ambientLight);

    var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
    directionalLight.position.x = -0.75;
    directionalLight.position.y = -0.5;
    directionalLight.position.z = -1;
    scene.add( directionalLight );

    controls = new THREE.OrbitControls(camera, renderer.domElement);
    
    var axis = new THREE.AxesHelper(2);
    scene.add(axis);
    
    var material = new THREE.MeshPhongMaterial({color:'#f08080'});
    var geometry = new THREE.BoxGeometry( 1, 1, 1 );
    mesh = new THREE.Mesh(geometry, material);

    var material2 = new THREE.MeshPhongMaterial({color:'#8080f0'});
    var geometry2 = new THREE.BoxGeometry( 1, 1, 1 );
    child = new THREE.Mesh(geometry2, material2);
    child.position.x = 1.5;

    mesh.add(child);
    scene.add(mesh);
  }

  function resize() {
    var aspect = window.innerWidth / window.innerHeight;
    renderer.setSize(window.innerWidth, window.innerHeight);
    camera.aspect = aspect;
    camera.updateProjectionMatrix();
  }

  function animate() {
  
    child.position.set(0, 0, 0);
    child.rotateY(0.02)
    child.translateX(1.5);

    mesh.position.set(0, 0, 0);
    mesh.rotateZ(0.01);
    mesh.translateX(1.0);
    
    requestAnimationFrame(animate);
    render();
  }

  function render() {
    renderer.render(scene, camera);
  }
})();
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<div id="container"></div>

Upvotes: 1

Related Questions