I suspect this is a maths issue rather than a flash one. In a nutshell, I have two cubes that rotate around their common center point. So far so good. Using the appendTranslation and appendRotation matrix functions, I've managed to get the two cubes to rotate ok.
The problem is the drawing order seems to have little effect on the Z Buffering. I've managed to Z Buffer correctly and order the cubes in order of depth. However, despite getting the drawing order right, the cueb is only properly occluded half of the time.
Imagine two cubes on the x-axis plane, either side at equal spacing rotating around the Y axis. At 90 degrees one cube will occlude the other and vice-versa at 270 degrees rotation. My problem is that this occurs correctly only at one of these places. It appears to be a sign issue as rotating backwards (so -90 and -270) has the same effect only at the opposite places.
Here is the relevant code:
PixelCubeGroup.as - Rotate is called each cycle
function rotateObj(rotx:Number,roty:Number,rotz:Number):void {
var i:int;
for(i=0; i<pixelVec.length; i++){
var v:Vector3D = centerPos;
v = v.subtract(pixelVec[i].getPos());
pixelVec[i].rotateObj(rotx,roty,rotz,v);
}
}
PixelCube.as The things that are drawn and rotated
function rotateObj(xrot:Number,yrot:Number,zrot:Number, pivot:Vector3D){
// rotate the cube by changing the values in the matrix 3D around a pivot point
m.identity();
m.appendTranslation(-pivot.x,-pivot.y, -pivot.z);
m.appendRotation(xrot, Vector3D.X_AXIS);
m.appendRotation(yrot, Vector3D.Y_AXIS);
m.appendRotation(zrot, Vector3D.Z_AXIS);
m.appendTranslation(pivot.x,pivot.y,pivot.z);
}
function draw():void
{
var renderV:Vector.<Number> = new Vector.<Number>();
currentV = new Vector.<Vector3D>();
for each(var vv:Vector3D in v)
{
// apply the matrix to the vertices in 'v'
vv = m.transformVector(vv);
vv.w = (400 + vv.z) / 400;
vv.project();
renderV.push(vv.x, vv.y);
currentV.push(vv);
}
// draw things out
container.graphics.clear();
container.x = xPos;
container.y = yPos;
container.z = 1;
var id = 0
plane.sort(sortByZ);
for each(var p:Vector.<int> in plane)
{
container.graphics.beginFill(palleteColours[id],1.0);
var planeV:Vector.<Number> = new Vector.<Number>();
for (var i:int = 0; i < planeSides; i++ )
{
planeV.push(renderV[p[i]* 2], renderV[p[i] * 2 + 1]);
}
container.graphics.drawTriangles(planeV, indices, null, TriangleCulling.NEGATIVE );
container.graphics.endFill();
id ++;
}
}
I've managed to narrow it down to the translation lines. Performing a rotation of two cubes without translation works fine and respects the drawing order, whereas this does not. I suspect a rogue Z Value is getting in there somewhere.