Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three.js: Rotate at center of an object

Tags:

three.js

I've modified this single spinning cube so that it contain 3 cubes in an Object3D

http://jsfiddle.net/VsWb9/1243/

In the above example it uses the first cube. I need it to rotate on a single axis at the exact center of the object.

The object3D code

  geometry = new THREE.CubeGeometry(50, 50, 50);
    material = new THREE.MeshNormalMaterial();
    mesh = new THREE.Object3D();
    mesh1 = new THREE.Mesh(geometry, material);
    mesh1.position.x = 50;
    mesh2 = new THREE.Mesh(geometry, material);
    mesh2.position.x = 100;
    mesh3 = new THREE.Mesh(geometry, material);

    mesh.add(mesh1);
    mesh.add(mesh2);
    mesh.add(mesh3);

    scene.add(mesh);

Here is the rotation

  mesh.rotation.x += 0.01;
  mesh.rotation.y += 0.02;

EDIT: Just to say that this is an example to demonstrate the problem, my actual code object contains many different sized shapes.

like image 467
jim smith Avatar asked Jul 31 '13 21:07

jim smith


People also ask

How do you rotate an object around a point in 3 JS?

js suggest the way to rotate an object around a point using three. js is to create a parent object at the position you want to rotate around, attach your object, and then move the child. Then when the parent is rotated the child rotates around the point.

How do you rotate an object in JavaScript?

Introduction to JavaScript rotate() canvas API The rotate() method allows you to rotate a drawing object on the canvas. The rotate() method accepts a rotation angle in radians. If the angle is positive, the rotation is clockwise. In case the angle is negative, the rotation is counterclockwise.


1 Answers

You may define the coordinates of the center.

mesh.position.set( center.x, center.y, center.z );
mesh.geometry.applyMatrix(new THREE.Matrix4().makeTranslation( -center.x, -center.y, -center.z ) );
like image 111
hayaishi Avatar answered Nov 22 '22 20:11

hayaishi