Ok, so a while back I decided to challenge myself to create a working 3D engine without looking at any tutorials. I've got what I think is a pretty good prototype, but the z-axis is giving me some really odd behavior.

Here's my code (also, this is in AS2):

PHP Code:
var magic 180/Math.PI;
function 
getA(x1,y1,z1,x2,y2,z2) {
    var 
Math.sqrt((x2-x1)*(x2-x1)+(z2-z1)*(z2-z1));
    var 
yd y2-y1;
    var 
Math.atan2(yd,d)*magic;
    return 
r;
}

function 
aDist(a1,a2) {
    var 
a2-a1;
    if (
180-= 360;
    else if (
< -180+= 360;
    return 
a;
}

p0 = [0,0,0];
p1 = [0,100,0];
p2 = [0,0,100];
p3 = [0,100,100];
p4 = [100,0,0];
p5 = [100,100,0];
p6 = [100,0,100];
p7 = [100,100,100];
p8 = [50,50,50];

var 
50;
var 
50;
var 
= -50;

var 
xa 0;
var 
ya 0;

onEnterFrame = function() {
    
removeMovieClip(s);
    
createEmptyMovieClip("s",1);
    
    if (
Key.isDown(Key.UP)) z++;
    if (
Key.isDown(Key.DOWN)) z--;
    if (
Key.isDown(Key.LEFT)) x++;
    if (
Key.isDown(Key.RIGHT)) x--;
    
    if (
xa >= 360xa 0;
    else if (
xa 0xa 359;
    for (var 
n=0_root["p"+n]; n++) {
        
_root["p"+n];
        
s.attachMovie("a","a"+n,n);
        
s["a"+n];
        
        
x2 getA(y,x,z,p[1],p[0],p[2]);
        
y2 getA(x,y,z,p[0],p[1],p[2]);
        
//x2 = aDist(x2,xa);
        //y2 = aDist(y2,ya);
        
        
o._x x2;
        
o._y y2;
    }

My problem is that whenever the z position gets within the box's depths, even when the camera is outside the box, the box seems to invert itself.


To explain my methodology a bit, I'm rendering any given object (or, as of now, point) by finding it's x&y angles relative to the position of the camera and then just using that angle as a strait up x/y position. There is no view limit right now, affording the player a 360 degree view around themselves.

In theory, this would mean that an x/y/z position of 50/50/-50 would render this box the same as -50/50/50. Yet while 50/50/-50 seems to render the box just fine, -50/50/50 renders it in a triangular shape.

I really can't wrap my head around why this would be happening, any ideas?