A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: 3D Maths...please help

  1. #1
    Senior Member
    Join Date
    Apr 2001
    Location
    UK
    Posts
    493

    3D Maths...please help

    Ok, i've been trying to bash the square peg of 3D Maths into the round hole of my head for a while, and the time has come to give it a bloody good whack.

    Has anyone got any straight forward, anybody should understand, fully explained tutorial and or tutorial links. I'm NOT looking for sample code or fla examples (thanks anyway) as i can download these anywhere.
    I want..no...NEED, to understand this properly as i am almost there and just have to get my head fully around this subject, so i can write my own engines for games, navigation etc... And i dont like copying or letting a simple(?) thing like a bit of maths, beat me.

    Any help, pointers or lessons would be most appreciated. Im a competant in AS so the coding isnt a problem...just the math!!


    Cheers

  2. #2
    Life Member
    Join Date
    Jan 2003
    Location
    Australia
    Posts
    87
    i know exactly what you mean. i tried understsnding 3d maths from tutorials and stuff and couldn;t get the matricies or rotation stuff, so i looked and looked for heaps and heaps of tutes.

    any ways for 3d maths there is rotation which is rotating a point around a a center (0,0,0) usually, as you translate the rotate center co-ords and point co-ords relative to 0,0,0 and then back after rotation. there is also translation, this is basic addition to the x,y,z point or subtraction, what ever way you translate the point. now there is also making these x,y,z co-ords into a 2d point.
    so thats
    rotate, translate and transform points

    to rotate the points some people use matricies, i don't. i just use the general formula extracted from the matricies ( i couldn't be stuffed trying to implement matricies when optimized code is better). to rotate a point , you will need seperate calculations for each rotation around each axis. if around the x-axis you just need to calculate y,z. if y-axis - x,z and z-axis y,x.

    translation, i suppose you get, i won't explain further.

    transforming 3d to 2d is fairly simple.
    for x-point -> x/z
    for y-point -> y/z
    this works because as z(the dist) gets farther away, x or y drops in screen height.
    If your wondering that the points will be small on the screen there is a further addition to these formulae
    for x-point -> (x/z) * screen width
    for y-point -> (y/z) * screen height

    i will give you some code i used to draw a cube:


    // BOX ===========[[[[;
    // _root.MyBox_x = new Array(-1,-1,1,1,-1,-1,1,1);
    // _root.MyBox_y = new Array(-1,1,1,-1,-1,1,1,-1);
    // _root.MyBox_z = new Array(-1,-1,-1,-1,1,1,1,1);
    // _root.createEmptyMovieClip("MyBox", 1);
    // _root.MyBoxxscrn = new Array(8);
    // _root.MyBoxyscrn = new Array(8);
    // BOX ===========]]]];

    // get vector x,y,z;
    // get vector magnitude;
    // divide vector by magnitude for x,y,z;
    // ================= [[rotate function;
    // if (_root.rotX == true) {
    // cos = Math.cos(x_angle);
    // sin = Math.sin(x_angle);
    // for (i=0; i<=7; i++) {
    // oldy = _root.MyBox_y[i];
    // oldz = _root.MyBox_z[i];
    // _root.MyBox_y[i] = (oldy*cos) - (oldz*sin);
    // _root.MyBox_z[i] = (oldy*sin) + (oldz*cos);
    // }
    // }
    // if (_root.rotY == true) {
    // cos = Math.cos(y_angle);
    // sin = Math.sin(y_angle);
    // for (i=0; i<=7; i++) {
    // oldx = _root.MyBox_x[i];
    // oldz = _root.MyBox_z[i];
    // _root.MyBox_x[i] = (oldx*cos) + (oldz*sin);
    // _root.MyBox_z[i] = (oldz*cos) - (oldx*sin);
    // }
    // }
    // if (_root.rotZ == true) {
    // cos = Math.cos(z_angle);
    // sin = Math.sin(z_angle);
    // for (i=0; i<=7; i++) {
    // oldx = _root.MyBox_x[i];
    // oldy = _root.MyBox_y[i];
    // _root.MyBox_x[i] = (oldx*cos)-(oldy*sin);
    // _root.MyBox_y[i] = (oldx*sin)+(oldy*cos);
    // }
    // }
    // ================= ]]rotate function;
    // ========================================;
    // ========================================;
    // ========================================;
    // ================= [[draw screen points;
    // if (_root.rotX == true || _root.rotY == true || _root.rotZ == true || _root.drawscreen == true) {
    // ================= [[calc screen points;
    // for (i=0; i<=7; i++) {
    // _root.MyBoxxscrn[i] = (150*_root.n*(_root.MyBox_x[i])/(_root.MyBox_z[i]-zcam))+150;
    // _root.MyBoxyscrn[i] = (150*_root.n*(_root.MyBox_y[i])/(_root.MyBox_z[i]-zcam))+150;
    // }
    // ================= ]]calc screen points;
    // with (_root.MyBox) {
    // _root.MyBox.clear();
    // lineStyle(.5, 0xffffff, 100);
    // moveTo(MyBoxxscrn[0], MyBoxyscrn[0]);
    // lineTo(MyBoxxscrn[1], MyBoxyscrn[1]);
    // moveTo(MyBoxxscrn[1], MyBoxyscrn[1]);
    // lineTo(MyBoxxscrn[2], MyBoxyscrn[2]);
    // moveTo(MyBoxxscrn[2], MyBoxyscrn[2]);
    // lineTo(MyBoxxscrn[3], MyBoxyscrn[3]);
    // moveTo(MyBoxxscrn[3], MyBoxyscrn[3]);
    // lineTo(MyBoxxscrn[0], MyBoxyscrn[0]);
    // moveTo(MyBoxxscrn[0], MyBoxyscrn[0]);
    // lineTo(MyBoxxscrn[4], MyBoxyscrn[4]);
    // moveTo(MyBoxxscrn[4], MyBoxyscrn[4]);
    // lineTo(MyBoxxscrn[5], MyBoxyscrn[5]);
    // moveTo(MyBoxxscrn[5], MyBoxyscrn[5]);
    // lineTo(MyBoxxscrn[6], MyBoxyscrn[6]);
    // moveTo(MyBoxxscrn[6], MyBoxyscrn[6]);
    // lineTo(MyBoxxscrn[7], MyBoxyscrn[7]);
    // moveTo(MyBoxxscrn[7], MyBoxyscrn[7]);
    // lineTo(MyBoxxscrn[4], MyBoxyscrn[4]);
    // moveTo(MyBoxxscrn[1], MyBoxyscrn[1]);
    // lineTo(MyBoxxscrn[5], MyBoxyscrn[5]);
    // moveTo(MyBoxxscrn[2], MyBoxyscrn[2]);
    // lineTo(MyBoxxscrn[6], MyBoxyscrn[6]);
    // moveTo(MyBoxxscrn[3], MyBoxyscrn[3]);
    // lineTo(MyBoxxscrn[7], MyBoxyscrn[7]);
    // }
    // }

    i got that out of a txt file, and sorry about the // lines but hopefully you will understand from this.
    _root.n is just zoom and zcam is the camera dist from the center 0,0,0. also if you go past the cube, the cam will swing back and look at the cube. wierd but i just learnt 3d from stupid mounds of tutes. there might be a few ommisions in the code but the code i have provided has been optimized a fair bit as well. good luck.

  3. #3
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    Here's a couple of good links...

    http://www.macromedia.com/devnet/mx/...animation.html -> "Creating 3D objects..." or there's a perspective tut on bit-101 too, which is a bit like this.

    Three Dimensional Rotations - gamedev is great

    Aaaah, the good ol' 3D cube, a classic. I've got one myself around somewhere...
    jonmack
    flash racer blog - advanced arcade racer development blog

  4. #4
    Senior Member
    Join Date
    Apr 2001
    Location
    UK
    Posts
    493
    cheers

    I'll have to go through all that and see what i come up with. If i get it, thank you wouldnt be enough..though cash it out of the question!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center