Thread: 3d Graphics.
View Single Post
Old Oct 16th, 2005, 2:57 AM   #29
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Okayyy then. Turns out that program was complete crap, it only looked like it was rotating properly, until I tried to make it do a full 360 turn.

So I scanned through a couple google pages, and found out you're supposed to do 3d rotations with 3 different matrxies: Rx, Ry, and Rz. I have no clue what the hell matrixes are, so I just made up my own way of rotating. I create a circle where the center is the middle of the object. Then for each (x,y) on the object, the arctangent is found (in relation to the circle), then converted to radians. Then the degree of rotation is added to the angle, which is converted back using sin() and cos() to extract new x and y values. Then this patterns is repeated twice more with 2 different circles representing the other axis combinations ([x,z],[y,z]).

It works beautifully. Even if it has been done before, I'm still happy because I figured it out on my own (same with the projection plane). It seems like that's the way 2d rotation would be done, so I found it quite apparent to do the same with 3d.

Here are the algorithms:

function flatten_object (vectors : array 1 .. points of array 1 .. 3
        of real, scale : int) : array 1 .. points of array 1 .. 3 of real
    var new_vectors : array 1 .. points of array 1 .. 3 of real
    for point : 1 .. points
        for i : 1 .. 3
            new_vectors (point) (i) := round (vectors (point) (i) * scale
                / (vectors (point) (3) + scale))
        end for
    end for
    result new_vectors
end flatten_object

function rotate (temp_vectors : array 1 .. points of array 1 .. 3 of
        real, rotX, rotY, rotZ : int, center : array 1 .. 3 of int) : array
        1 .. points of array 1 .. 3 of real
    var vectors : array 1 .. points of array 1 .. 3 of real := temp_vectors
    var new_vectors : array 1 .. points of array 1 .. 3 of real
    const pi := 3.1415926538
    var degree, radius, rad : real
    var s : array 1 .. 3 of
        record
            a : int
            b : int
            c : int
            d : int
            e : int
        end record
    s (1).a := 1
    s (1).b := 2
    s (1).c := 3
    s (1).d := 2
    s (1).e := rotX
    s (2).a := 2
    s (2).b := 3
    s (2).c := 1
    s (2).d := 3
    s (2).e := rotY
    s (3).a := 1
    s (3).b := 3
    s (3).c := 2
    s (3).d := 3
    s (3).e := rotZ
    for se : 1 .. 3
        for point : 1 .. points
            vectors (point) (s (se).a) -= center (s (se).a)
            vectors (point) (s (se).b) -= center (s (se).b)
            degree := arctan (vectors (point) (s (se).a) /
                vectors (point) (s (se).b)) / pi * 180
            rad := (degree + s (se).e) * pi / 180
            radius := sqrt (vectors (point) (s (se).a) ** 2 +
                vectors (point) (s (se).b) ** 2)
            new_vectors (point) (s (se).a) := sin (rad) * radius +
                center (s (se).a)
            new_vectors (point) (s (se).b) := cos (rad) * radius +
                center (s (se).b)
            if vectors (point) (s (se).d) < 0 then
                new_vectors (point) (s (se).a) := -
                    new_vectors (point) (s (se).a)
                new_vectors (point) (s (se).b) := -
                    new_vectors (point) (s (se).b)
            end if
            new_vectors (point) (s (se).c) := vectors (point) (s (se).c)
        end for
        vectors := new_vectors
    end for
    result vectors
end rotate

And here's an interactive demonstration of the program:

http://1v7.com/drsane/cube.exe

Sometimes an optical illusion occurs and the cube appears to have inverted. It looks messed.
Sane is offline   Reply With Quote