Rotating meshes around a point

Software: Away3D 4.x

rory, Newbie
Posted: 01 October 2012 12:00 PM   Total Posts: 2

HI,

First of all - great 3d package! I’ve managed to add it to my current Flex project with only minor problems (my mistakes) and I’ve been very impressed so far.

On to the problem - I have 64 cubes contained in an ObjectContainer3D and I’d like them to rotate 90 degrees on a different axis when I press a button. It kind-of-works (the gimbal lock threw me for a bit) but I’m hoping there’s a better way. My hope for this is to develop something similar to a Rubik’s cube - like a number puzzle.

Here’s what I’ve done so far in order to make the container rotate:

public function rotateCube(axis:String, deg:int = 90):void
{
 
if (!Tweener.isTweening(cubeContainer)) {
  
var mat:Matrix3D = dummyCube.transform.clone();
  switch (
axis) {
   
case 'x':
    
mat.appendRotation(deg, new Vector3D(1, 0, 0));
    break;
   case 
'y':
    
mat.appendRotation(deg, new Vector3D(0, 1, 0));
    break;
   case 
'z':
    
mat.appendRotation(deg, new Vector3D(0, 0, 1));
    break;
  
}
  dummyCube
.transform = mat;

  
// Tween it!
  
Tweener.addTween(cubeContainer, { rotationX: dummyCube.rotationX, rotationY: dummyCube.rotationY, rotationZ: dummyCube.rotationZ, time: 0.7, transition:'easeOutBack' });
 
}
} 

A matrix transformation is applied to a dummy cube, then the Tweener rotates the container to match the rotationX/Y/Z of the dummy. It has potential, but I think I’m doing it wrong and would welcome some advice.

Cheers

   

rory, Newbie
Posted: 07 October 2012 09:44 AM   Total Posts: 2   [ # 1 ]

I’ve managed to fix this issue by re-evaluating the problem (had to stop thinking it was a rubik’s cube).

Just in case anyone would like the code:

public function rotateCubes(axis:String, deg:int = 90):void
{
 
if (isRotatable) {
 
  
// Disbale rotation
  
isRotatable = false;
    
  var 
i:uint, cube:Mesh, imax:int = cubeContainer.numChildren, destX:int, destY:int, destZ:int, atan2:Number, dist:int;
  for (
i = 0; i<imax; i++) {
     
   
// The cube
   
cube = cubeContainer.getChildAt(i) as Mesh;
     
   switch(
axis)
   
{
    
case 'x': // up/down
    
{
     atan2 
= Math.atan2(cube.y, cube.z) + ( deg * (Math.PI/180) );
     
dist = Math.ceil(Math.sqrt((cube.y * cube.y) + (cube.z * cube.z)));
     
destX = cube.x;
     
destY = Math.sin(atan2) * dist;
     
destZ = Math.cos(atan2) * dist;
     break;
    
}
    
case 'y': // left/right
    
{
     atan2 
= Math.atan2(cube.x, cube.z) + ( deg * (Math.PI/180) );
     
dist = Math.ceil(Math.sqrt((cube.x * cube.x) + (cube.z * cube.z)));
     
destX = Math.sin(atan2) * dist;
     
destY = cube.y;
     
destZ = Math.cos(atan2) * dist;
     break;
    
}
    
case 'z': // clock/counter
    
{
     atan2 
= Math.atan2(cube.y, cube.x) + ( deg * (Math.PI/180) );
     
dist = Math.ceil(Math.sqrt((cube.y * cube.y) + (cube.x * cube.x)));
     
destX = Math.cos(atan2) * dist;
     
destY = Math.sin(atan2) * dist;
     
destZ = cube.z;
     break;
    
} 
   }
     
   TweenLite
.to(cube, 0.6, {x: destX, y: destY, z: destZ, onComplete:enableRotate});
     
  
}
 }
} 
   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X