PDA

View Full Version : What is wrong in this code? (3d)



Chriz74
March 26th, 2005, 10:21 AM
Ok... i found the bug in the 3d pyramids routine... anyway. Do you know what is wrong here? I have points for a 3d logo at z -20 and z +20 ... I am using the ifvisiblebetween function for backface culling... the problem is the program shows only the last logo (black) and not the first one (red) ... if you try to delete the last one (black) from the code you'll notice the red shows up. So why it doesn't show up when the black is under it? I really can't understand.

------

this.createEmptyMovieClip("theScene", 1);
theScene._x = 80;
theScene._y = 150;


focalLength = 300;
isVisibleBetween = function (a, b, c) {
if (((b.y-a.y)/(b.x-a.x)-(c.y-a.y)/(c.x-a.x)<0) ^ (a.x<=b.x == a.x>c.x)) {
return true;
} else {
return false;
}

};
make3DPoint = function(x,y,z){
var point = new Object();
point.x = x;
point.y = y;
point.z = z;
return point;
};
Transform3DPointsTo2DPoints = function(points, axisRotations){
var TransformedPointsArray = [];
var sx = Math.sin(axisRotations.x);
var cx = Math.cos(axisRotations.x);
var sy = Math.sin(axisRotations.y);
var cy = Math.cos(axisRotations.y);
var sz = Math.sin(axisRotations.z);
var cz = Math.cos(axisRotations.z);
var x,y,z, xy,xz, yx,yz, zx,zy, scaleFactor;
var i = points.length;
while (i--){
x = points[i].x;
y = points[i].y;
z = points[i].z;
// rotation around x
xy = cx*y - sx*z;
xz = sx*y + cx*z;
// rotation around y
yz = cy*xz - sy*x;
yx = sy*xz + cy*x;
// rotation around z
zx = cz*yx - sz*xy;
zy = sz*yx + cz*xy;

scaleFactor = focalLength/(focalLength + yz);
x = zx*scaleFactor;
y = zy*scaleFactor;
z = -yz;
TransformedPointsArray[i] = make3DPoint(x, y, z);
}
return TransformedPointsArray;
};
pointsArray = [
make3DPoint ( -120, -40, -20 ),
make3DPoint ( -105, -55, -20 ),
make3DPoint ( -40, -55, -20),
make3DPoint ( -165, 60, -20 ),
make3DPoint ( 170, 60, -20 ),
make3DPoint ( 170, -40, -20 ),
make3DPoint ( 130, -80, -20 ),
make3DPoint ( 45, -80, -20 ),
make3DPoint ( 20, -55, -20 ),
make3DPoint ( 110, -55, -20 ),
make3DPoint ( 135, -30, -20 ),
make3DPoint ( 135, 30, -20 ),
make3DPoint ( -90, 30, -20 ),
make3DPoint ( 30, -80, -20 ),
make3DPoint ( -150, -80, -20 ),
make3DPoint ( -120, -40, 20 ),
make3DPoint ( -105, -55, 20 ),
make3DPoint ( -40, -55, 20 ),
make3DPoint ( -165, 60, 20 ),
make3DPoint ( 170, 60, 20 ),
make3DPoint ( 170, -40, 20 ),
make3DPoint ( 130, -80, 20 ),
make3DPoint ( 45, -80, 20 ),
make3DPoint ( 20, -55, 20 ),
make3DPoint ( 110, -55, 20 ),
make3DPoint ( 135, -30, 20 ),
make3DPoint ( 135, 30, 20 ),
make3DPoint ( -90, 30, 20 ),
make3DPoint ( 30, -80, 20 ),
make3DPoint ( -150, -80, 20 ),
make3DPoint ( -120, -40, -20 ),
make3DPoint ( -105, -55, -20 ),
make3DPoint ( -40, -55, -20),
make3DPoint ( -165, 60, -20 ),
make3DPoint ( 170, 60, -20 ),
make3DPoint ( 170, -40, -20 ),
make3DPoint ( 130, -80, -20 ),
make3DPoint ( 45, -80, -20 ),
make3DPoint ( 20, -55, -20 ),
make3DPoint ( 110, -55, -20 ),
make3DPoint ( 135, -30, -20 ),
make3DPoint ( 135, 30, -20 ),
make3DPoint ( -90, 30, -20 ),
make3DPoint ( 30, -80, -20 ),
make3DPoint ( -150, -80, -20 ),
make3DPoint ( -120, -40, 20 ),
make3DPoint ( -105, -55, 20 ),
make3DPoint ( -40, -55, 20 ),
make3DPoint ( -165, 60, 20 ),
make3DPoint ( 170, 60, 20 ),
make3DPoint ( 170, -40, 20 ),
make3DPoint ( 130, -80, 20 ),
make3DPoint ( 45, -80, 20 ),
make3DPoint ( 20, -55, 20 ),
make3DPoint ( 110, -55, 20 ),
make3DPoint ( 135, -30, 20 ),
make3DPoint ( 135, 30, 20 ),
make3DPoint ( -90, 30, 20 ),
make3DPoint ( 30, -80, 20 ),
make3DPoint ( -150, -80, 20 )

];
theScene.AxisRotations = make3DPoint(0,0,0);
rotate1 = function(){
this.AxisRotations.x -= 0.0;
this.AxisRotations.y += 0.08;
this.AxisRotations.z += 0.00;
var pts2D = Transform3DPointsTo2DPoints(pointsArray, this.AxisRotations);
var mouseIsDown = Key.isDown(1);

this.clear();
if (isVisibleBetween(pts2D[3], pts2D[4], pts2D[14] )){
this.beginFill(0xFF0000, 100);
this.moveTo(pts2D[0].x, pts2D[0].y);
this.lineTo(pts2D[1].x, pts2D[1].y);
this.lineTo(pts2D[2].x, pts2D[2].y);
this.lineTo(pts2D[3].x, pts2D[3].y);
this.lineTo(pts2D[4].x, pts2D[4].y);
this.lineTo(pts2D[5].x, pts2D[5].y);
this.lineTo(pts2D[6].x, pts2D[6].y);
this.lineTo(pts2D[7].x, pts2D[7].y);
this.lineTo(pts2D[8].x, pts2D[8].y);
this.lineTo(pts2D[9].x, pts2D[9].y);
this.lineTo(pts2D[10].x, pts2D[10].y);
this.lineTo(pts2D[11].x, pts2D[11].y);
this.lineTo(pts2D[12].x, pts2D[12].y);
this.lineTo(pts2D[13].x, pts2D[13].y);
this.lineTo(pts2D[14].x, pts2D[14].y);
this.lineTo(pts2D[0].x, pts2D[0].y);
this.endFill();

}
this.clear();
if (isVisibleBetween(pts2D[48], pts2D[44], pts2D[49] )){
this.beginFill(0x000000, 100);
this.moveTo(pts2D[45].x, pts2D[45].y);
this.lineTo(pts2D[46].x, pts2D[46].y);
this.lineTo(pts2D[47].x, pts2D[47].y);
this.lineTo(pts2D[48].x, pts2D[48].y);
this.lineTo(pts2D[49].x, pts2D[49].y);
this.lineTo(pts2D[50].x, pts2D[50].y);
this.lineTo(pts2D[51].x, pts2D[51].y);
this.lineTo(pts2D[52].x, pts2D[52].y);
this.lineTo(pts2D[53].x, pts2D[53].y);
this.lineTo(pts2D[54].x, pts2D[54].y);
this.lineTo(pts2D[55].x, pts2D[55].y);
this.lineTo(pts2D[56].x, pts2D[56].y);
this.lineTo(pts2D[57].x, pts2D[57].y);
this.lineTo(pts2D[58].x, pts2D[58].y);
this.lineTo(pts2D[59].x, pts2D[59].y);
this.lineTo(pts2D[45].x, pts2D[45].y);
this.endFill();

}

};
theScene.onEnterFrame = rotate1;

-------

SilverFalcon
March 26th, 2005, 12:47 PM
i dont use 3d script code but why dont you egt swif 3d it is alot better than using tones of code for making 3d objects.

Templarian
March 26th, 2005, 02:42 PM
... hew wants a small file probably (that would make it alot smaller than swift one.

Chriz74
March 27th, 2005, 04:08 AM
i dont use 3d script code but why dont you egt swif 3d it is alot better than using tones of code for making 3d objects.

Thanks for the suggest. I want to learn a bit of coding rather than using a 3d generator. I think maybe I have to swap the order of the points the routine is checking to decide if a mesh is visible or not.