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 d = Math.sqrt((x2-x1)*(x2-x1)+(z2-z1)*(z2-z1));
var yd = y2-y1;
var r = Math.atan2(yd,d)*magic;
return r;
}
function aDist(a1,a2) {
var a = a2-a1;
if (a > 180) a -= 360;
else if (a < -180) a += 360;
return a;
}
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?
First off, I don't see you rotating the points in relation to the rotation of the camera...then again, have you even implemented camera rotation?
I'm not too sure about you're problem, but I think I can some what explain it (I had had this problem (if i understand yours correctly) when I made my engine):
--see picture--
After you read the picture:
The way to go around this is z-clipping, which will chop the intersecting line, and put a vertice closely in front of the viewing plane (never do: z = 0, because then you start dividing with 0, which will make it go whack).
Ugh, I always do this. Solved my own problem fairly shortly after posting. But thanks for your reply Pazil!
Apparently my problem was in try to calculate the x and y positions using the same functions. I'm guessing I only have to deal with the z-axis once, and my old code dealt with it twice, causing the errors.
So my new code has separate function for finding the x and y positions. Now, I have have no idea how flawed this attempt may or may not be, but here's the code form my latest version for those interested:
PHP Code:
var magic = 180/Math.PI; function getA(x1,y1,z1,x2,y2,z2) { var d = Math.sqrt((x2-x1)*(x2-x1)+(z2-z1)*(z2-z1)); var yd = y2-y1; var r = Math.atan2(yd,d)*magic; return r; }
function getAX(x1,z1,x2,z2) { var zd = z2-z1; var xd = x2-x1; var r = Math.atan2(xd,zd)*magic; return r; }