Stryder
08-03-04, 05:32 PM
I don't think anyones really discussed about Flashes Actionscripting before. I'm not going to suggest I'm a guru in the field because I'm still having problems to develop my Matrix functions, but it does seem that is growing up quite rapidly.
I'm actually thinking of developing something in 3D (once I get my engine up, thats taking a little while to setup, since I'm attempting to "Template" it alot so the functions can be reused.)
Anyone else on here have any clues about Actionscript in Flash?
Stryder
08-04-04, 04:45 PM
Okay I've decided my attempts at getting the matrix mathematics to work (and transform) are a failure, so I can create an image with 3D points I just can't get the damn thing to rotate.
Heres my code:
// Global Settings
_root.createEmptyMovieClip ("blam", 1);
// Create Arrays
var vertx=new Array();
var vert=new Array();
var edger=new Array();
var edge=new Array();
// Populate Arrays for shapes
// VERTX arrays are the x,y,z "Points" for a shape in STRINGS.
// EDGER arrays are the "Edges" from drawing one point to a second point in STRINGS.
vertx[0]=new Array("0,100,0","100,100,0","100,100,-100","0,100,-100","0,0,0","100,0,0","100,0,-100","0,0,-100");
vertx[1]=new Array("0,0,0","0,-50,0","-50,-50,0","-50,0,0");
edger[0]=new Array("1,2","2,3","3,4","4,1","5,6","6,7","7,8","8,5","1,5","2,6","3,7","4,8");
edger[1]=new Array("1,2","2,3","3,4","4,1");
// ProcObj(ob) = ob equal the objects arrays vertx[i] & edger[i]
// Function splits the objects arrays down into new arrays vert[i] & edge[i]
// the new arrays can be then used to define the atoms of a coord or edge.
// i.e. vert[i][0] is an X axis, vert[i][1] is a Y axis, vert [i][2] is a Z axis.
// edge[i][0] is where moveTo in the Draw function starts, and edge[i][1] is where it ends.
// Note: Due to the main arrays being "Strings" all atoms are also Strings
// so will need to use the "Number()" method to convert them to a number.
function ProcObj(ob:Number){
// These first two loops Clean out old data from the vert/edge Arrays
// without cleaning this arrays it could in extreme cases allow data to
// be left in the array.
for(var i=0; i < vert.length; i++){
vert[i]="";
}
for(var i=0; i < edge.length; i++){
edge[i]="";
}
for(var i=0; i < vertx[ob].length; i++){
vert[i]=vertx[ob][i].split(",");
}
for(var i=0; i < edger[ob].length; i++){
edge[i]=edger[ob][i].split(",");
}
}
// Draw(ob) = ob equal the objects arrays vertx[i] & edger[i]
// This function will get the number of edge occurances with edger[ob].length
// and generate a loop that will moveTo a point and lineTo a point for each edge
// using vert[i][i] to define the coords.
function Draw(ob:Number){
var newX:Number;
var newY:Number;
var VertNum:Number;
_root.blam.lineStyle (1, 0xFF00FF, 100);
for(var i=0; i < edger[ob].length; i++){
VertNum=Number(edge[i][0])-1;
newX=(Stage.width/2) + Number(vert[VertNum][0]);
newY=(Stage.height/2) - Number(vert[VertNum][1]);
_root.blam.moveTo(newX,newY);
VertNum=Number(edge[i][1])-1;
newX=(Stage.width/2)+Number(vert[VertNum][0]);
newY=(Stage.height/2)-Number(vert[VertNum][1]);
_root.blam.lineTo(newX,newY);
}
}
// Percept(ob) = ob equal the objects arrays vertx[i] & edger[i] and eye the distance
// of the eye from zAxis 0
// This function alters the Array generated by ProcObj() to create perspective.
function Percept(ob:Number,eye:Number){
for(var i=0; i < vert.length; i++){
var xCoord = vert[i][0];
var yCoord = vert[i][1];
var zCoord = vert[i][2];
var t = 1.0 / (1.0 - zCoord / eye);
vert[i][0]=xCoord * t;
vert[i][1]=yCoord * t;
}
}
// I've called this function main as it's the main function to run at the start to populate the screen.
function main(){
// Process Object must occur before being Draw.
// Percept(ob,eye) = ob is the objects array, eye is the distance from Zaxis 0
// Draw(ob) = ob is the same as above.
// Object 0 should draw
ProcObj(0);
Percept(0,350);
Draw(0);
// Object 1 should draw
ProcObj(1);
// since it's 2D it doesn't need perspective
Draw(1);
}
main();
I've posted this so if anyone want's to use it they can, and if they can work out how to get it to work with a transformation matrix (Scale/3d Rotate etc), message me.
It's basically a re-write of some VC++ code from an Andre LaMothe book, but it's been altered to deal with Actionscript constraints.
Blindman
08-05-04, 04:42 AM
// rotate verts around y axis
RotateAroundYAxis(angle)
{
mat[0][0] = cos(angle)
mat[0][1] = 0
mat[0][2] = sin(angle)
mat[1][0] = 0
mat[1][1] = 1
mat[1][2] = 0
mat[2][0] = -sin(angle)
mat[2][1] = 0
mat[2][2] = cos(angle)
for(i = 0; i < numberVerts; i++)
{
tranformedVert[i][0] = vert[i][0] * mat[0][0]+vert[i][1]*mat[0][1]+vert[i][2]*mat[0][2]
tranformedVert[i][1] = vert[i][0] * mat[1][0]+vert[i][1]*mat[1][1]+vert[i][2]*mat[1][2]
tranformedVert[i][2] = vert[i][0] * mat[2][0]+vert[i][1]*mat[2][1]+vert[i][2]*mat[2][2]
}
}
You can put all the rotations into a single matrix, also scaling.
Translation requires a [3][4] array.
It will help if you create some functions to multiply matrix.
Matrix = Matrix * Matrix;
multiplying matrix with vector as well will help
vec = vec * matrix;
There is plenty of sites that will show you how...
Stryder
08-05-04, 01:45 PM
Thanks for your input, I'll see what I can come up with in the future (I'll have to evalute all souces).
Btw, I know one thing thats difficult to program is a "Shape/Surface" script, I generate a function for it previously the problem was that when it came to fill the three dimensional shape it behaved weirdly which is mentioned at a few places on the net. (like the fill would cross the screen or when dealing with a square create a triangled hour glass effect rather than fill a square).
If anyone's got a way to do the fills that doesn't error I would be glad to hear (and likewise if I find one/write one I'll mention it here)