Computer Science Canada

[Contest] Cubular

Author:  Catalyst [ Sun May 25, 2003 12:13 am ]
Post subject:  [Contest] Cubular

Ill give +50 bits to the first person who can make a 3d spinning cube (ill give another +25 bits to the person that can do it in <50 lines without stacking commands)

rules:
cant use any 3d engine (including mine)
must spin on all axes AND have perspective

No end date on this one

Author:  Asok [ Sun May 25, 2003 12:40 am ]
Post subject: 

I'm sure one has allready been posted somewhere on the forum by you.

Author:  Catalyst [ Sun May 25, 2003 12:44 am ]
Post subject: 

nope, not in source form

Author:  Tony [ Sun May 25, 2003 12:51 am ]
Post subject: 

could you clarify having perspective requirement?

Author:  Catalyst [ Sun May 25, 2003 12:59 am ]
Post subject: 

it has to have a perspective deformation
i.e. it gets smaller as it goes back,etc

Author:  Homer_simpson [ Sun May 25, 2003 11:43 am ]
Post subject: 

i'm almost done and everything works so perfectly but one question... how do i caculate the origin of my cube to be rotated on ?

Author:  Homer_simpson [ Sun May 25, 2003 11:46 am ]
Post subject: 

this is what it looks like so far...
code:
procedure rotate (OriginX, OriginY : real, var secondpartX, secondpartY : real, Rotaion : real)
    var tempx := (((OriginX - secondpartX) * cosd (Rotaion)) + ((OriginY - secondpartY) * sind (Rotaion)))
    var tempy := (((OriginY - secondpartY) * cosd (Rotaion)) - ((OriginX - secondpartX) * sind (Rotaion)))
    secondpartY := OriginY - tempy
    secondpartX := OriginX - tempx
end rotate
type dot :
    record
        x, y, z : real
    end record
type cube :
    record
        d : array 1 .. 10 of dot
        center : dot
    end record
var cube1 : cube
procedure movecube (var c : cube, x, y, z : real)
    for i : 1 .. 8
        c.d (i).x += x
        c.d (i).y += y
        c.d (i).z /= z
    end for
    c.center.x += x
    c.center.y += y
    c.center.z /= z
end movecube
procedure draw3dline (d1, d2 : dot, c : int)
    drawline (round (d1.x / d1.z), round (d1.y / d1.z), round (d2.x / d2.z), round (d2.y / d2.z), c)
end draw3dline
procedure drawcube (var c : cube, col : int)
    draw3dline (c.d (1), c.d (2), col)
    draw3dline (c.d (2), c.d (3), col)
    draw3dline (c.d (3), c.d (4), col)
    draw3dline (c.d (4), c.d (1), col)
    draw3dline (c.d (5), c.d (6), col)
    draw3dline (c.d (6), c.d (7), col)
    draw3dline (c.d (7), c.d (8), col)
    draw3dline (c.d (8), c.d (5), col)
    draw3dline (c.d (1), c.d (5), col)
    draw3dline (c.d (2), c.d (6), col)
    draw3dline (c.d (3), c.d (7), col)
    draw3dline (c.d (4), c.d (8), col)
end drawcube
procedure rotatecubexy (var c : cube, d : real)
    for i : 1 .. 8
        rotate (c.center.x, c.center.y, c.d (i).x, c.d (i).y, d)
    end for
end rotatecubexy
procedure rotatecubexz (var c : cube, d : real)
    for i : 1 .. 8
        rotate (c.center.x, c.center.z, c.d (i).x, c.d (i).z, d)
    end for
end rotatecubexz
procedure rotatecubeyz (var c : cube, d : real)
    for i : 1 .. 8
        rotate (c.center.y, c.center.z, c.d (i).y, c.d (i).z, d)
    end for
end rotatecubeyz
procedure assign3d (var d : dot, x, y, z : real)
    d.x := x
    d.y := y
    d.z := z + 10
end assign3d
assign3d (cube1.d (1), -1, 1, -1)
assign3d (cube1.d (2), 1, 1, -1)
assign3d (cube1.d (3), 1, 1, 1)
assign3d (cube1.d (4), -1, 1, 1)
assign3d (cube1.d (5), -1, -1, -1)
assign3d (cube1.d (6), 1, -1, -1)
assign3d (cube1.d (7), 1, -1, 1)
assign3d (cube1.d (8), -1, -1, 1)
assign3d (cube1.center, cube1.d (1).x, cube1.d (1).y, cube1.d (1).z)
movecube (cube1, 10, 10, 200)
loop
    rotatecubexy (cube1, 10)
    drawcube (cube1, 9)
    delay (100)
    cls
end loop

Author:  bugzpodder [ Sun May 25, 2003 12:00 pm ]
Post subject: 

Nice!
using the formulas i showed you a while back? Wink

code:
drawline (round (d1.x / d1.z), round (d1.y / d1.z), round (d2.x / d2.z), round (d2.y / d2.z), c)

Author:  Catalyst [ Sun May 25, 2003 1:03 pm ]
Post subject: 

still need to rotate on 2 more axes if u want the bits

Author:  Homer_simpson [ Sun May 25, 2003 1:12 pm ]
Post subject: 

ya i know...
Quote:
using the formulas i showed you a while back?

are there any other formulas?


how do i caculate the origin of my cube to be rotated on ?

Author:  Catalyst [ Sun May 25, 2003 1:21 pm ]
Post subject: 

same way ud find the center of a 2d polygon

Author:  Homer_simpson [ Sun May 25, 2003 1:33 pm ]
Post subject: 

how da hell do u do that?

Author:  JSBN [ Sun May 25, 2003 1:42 pm ]
Post subject: 

well... I loose at this contest.....

Author:  Homer_simpson [ Sun May 25, 2003 10:53 pm ]
Post subject: 

whooooooohooooooooo!!!!
i did it(still have some bugs tho
code:
const norm := 1000
procedure rotate (OriginX, OriginY : real, var secondpartX, secondpartY : real, Rotaion : real)
    var tempx := (((OriginX - secondpartX) * cosd (Rotaion)) + ((OriginY - secondpartY) * sind (Rotaion)))
    var tempy := (((OriginY - secondpartY) * cosd (Rotaion)) - ((OriginX - secondpartX) * sind (Rotaion)))
    secondpartY := OriginY - tempy
    secondpartX := OriginX - tempx
end rotate
type dot :
    record
        x, y, z : real
        xD, yD : int
    end record
type cube :
    record
        d : array 1 .. 10 of dot
        center : dot
    end record
var cube1 : cube
procedure movecube (var c : cube, x, y, z : real)
    for i : 1 .. 8
        c.d (i).x += x
        c.d (i).y += y
        c.d (i).z += z
    end for
    c.center.x += x
    c.center.y += y
    c.center.z += z
end movecube
procedure assign3d (var d : dot, x, y, z : real)
    d.x := x
    d.y := y
    d.z := z + 10
end assign3d
procedure draw3dline (d1, d2 : dot, c : int)
    drawline (round (d1.x / (d1.z / norm)) + 320, round (d1.y / (d1.z / norm)) + 200, round (d2.x / (d2.z / norm)) + 320, round (d2.y / (d2.z / norm)) + 200, c)
end draw3dline
procedure drawcube (var c : cube, col : int)
    assign3d (cube1.center, cube1.d (1).x, cube1.d (1).y, cube1.d (1).z)
    draw3dline (c.d (1), c.d (2), col)
    draw3dline (c.d (2), c.d (3), col)
    draw3dline (c.d (3), c.d (4), col)
    draw3dline (c.d (4), c.d (1), col)
    draw3dline (c.d (5), c.d (6), col)
    draw3dline (c.d (6), c.d (7), col)
    draw3dline (c.d (7), c.d (8), col)
    draw3dline (c.d (8), c.d (5), col)
    draw3dline (c.d (1), c.d (5), col)
    draw3dline (c.d (2), c.d (6), col)
    draw3dline (c.d (3), c.d (7), col)
    draw3dline (c.d (4), c.d (8), col)
end drawcube
procedure rotatecubexy (var c : cube, d : real)
    for i : 1 .. 8
        rotate (c.center.x, c.center.y, c.d (i).x, c.d (i).y, d)
    end for
end rotatecubexy
procedure rotatecubexz (var c : cube, d : real)
    for i : 1 .. 8
        rotate (c.center.x, c.center.z, c.d (i).x, c.d (i).z, d)
    end for
end rotatecubexz
procedure rotatecubeyz (var c : cube, d : real)
    for i : 1 .. 8
        rotate (c.center.y, c.center.z, c.d (i).y, c.d (i).z, d)
    end for
end rotatecubeyz
assign3d (cube1.d (1), -10, 10, -10)
assign3d (cube1.d (2), 10, 10, -10)
assign3d (cube1.d (3), 10, 10, 10)
assign3d (cube1.d (4), -10, 10, 10)
assign3d (cube1.d (5), -10, -10, -10)
assign3d (cube1.d (6), 10, -10, -10)
assign3d (cube1.d (7), 10, -10, 10)
assign3d (cube1.d (8), -10, -10, 10)
%assign3d (cube1.center, cube1.d (1).x + 1, cube1.d (1).y - 1, cube1.d (1).z + 1)
assign3d (cube1.center, cube1.d (1).x, cube1.d (1).y, cube1.d (1).z)

movecube (cube1, 10, 10, 200)
loop
    if hasch then
        cls
        case getchar of
            label "a" :
                rotatecubexy (cube1, 1)
            label "s" :
                rotatecubexz (cube1, 1)
            label "d" :
                rotatecubeyz (cube1, 1)
            label "q" :
                rotatecubexy (cube1, -1)
            label "w" :
                rotatecubexz (cube1, -1)
            label "e" :
                rotatecubeyz (cube1, -1)
            label :
        end case
    end if
    drawcube (cube1, 9)
end loop

I'll be fixing the bugs shortly
controls are a,s,d,q,w,e

Author:  JSBN [ Sun May 25, 2003 10:56 pm ]
Post subject: 

nice, very nice
+15 bits

Author:  Catalyst [ Sun May 25, 2003 11:25 pm ]
Post subject: 

See 3d isnt hard...

Very nice work

+50 bits

Author:  Catalyst [ Sun May 25, 2003 11:27 pm ]
Post subject: 

oh btw

to find the origin of any 3d object all you have to do is sum up all the values from each axis (x,y,z) then average them

Author:  Homer_simpson [ Mon May 26, 2003 7:38 am ]
Post subject: 

aaaaaaaahhhh... i wanted to do that but then i thought it wouldn't work... =/...
i mean wouldn't the negative numbers in x,y,z mess it up?

Author:  Homer_simpson [ Mon May 26, 2003 7:45 am ]
Post subject: 

fixed out the origin finding problem as well =)

Author:  Catalyst [ Tue May 27, 2003 11:04 pm ]
Post subject: 

since no one went for the 50-line version, here it is

note: the drawline code should only be 2 lines long

code:
View.Set ("offscreenonly,graphics:640;640")
var x : array 1 .. 8 of real := init (100, 100, 100, 100, 500, 500, 500, 500)
var y : array 1 .. 8 of real := init (100, 100, 500, 500, 100, 100, 500, 500)
var z : array 1 .. 8 of real := init (-250, -750, -250, -750, -250, -750, -250, -750)
var px, py, pz,X, Y, Z, relZ, relX, relY : real := maxx div 2
var x0, y0, z0, x9, y9, diffx, diffy : int := 0
loop
    X := 0
    Y := 0
    Z := 0
    x9 := x0
    y9 := y0
    mousewhere (x0, y0, z0)
    diffx := x0 - x9
    diffy := y0 - y9
    for i : 1 .. 8
        X += x (i)
        Y += y (i)
        Z += z (i)
    end for
    X /= 8
    Y /= 8
    Z /= 8
    for i : 1 .. 8
        relZ := (((Z - z (i)) * cosd (-diffx * z0)) + ((X - x (i)) * sind (-diffx * z0)))
        relX := (((X - x (i)) * cosd (-diffx * z0)) - ((Z - z (i)) * sind (-diffx * z0)))
        x (i) := X - relX
        z (i) := Z - relZ
        relZ := (((Z - z (i)) * cosd (-diffy * z0)) + ((Y - y (i)) * sind (-diffy * z0)))
        relY := (((Y - y (i)) * cosd (-diffy * z0)) - ((Z - z (i)) * sind (-diffy * z0)))
        y (i) := Y - relY
        z (i) := Z - relZ
        relX := (((X - x (i)) * cosd (-diffy * z0)) + ((Y - y (i)) * sind (-diffy * z0)))
        relY := (((Y - y (i)) * cosd (-diffy * z0)) - ((X - x (i)) * sind (-diffy * z0)))
        y (i) := Y - relY
        x (i) := X - relX
    end for
    for i : 1 .. 8
        for k : 1 .. 8
            drawline (round ((100 - (z (i) - (((z (i) - pz) / (x (i) - px) + 0.001) * x (i))) / ((z (i) - pz) / (x (i) - px) + 0.001))),round ((100 * ((y (i) - py) / (z (i) - pz) + 0.001)) + (y (i) - (((y (i) - py) / (z (i) - pz) + 0.001) * z (i)))),
            round ((100 - (z (k) - (((z (k) - pz) / (x (k) - px) + 0.001) * x (k))) / ((z (k) - pz) / (x (k) - px) + 0.001))),round ((100 * ((y (k) - py) / (z (k) - pz) + 0.001)) + (y (k) - (((y (k) - py) / (z (k) - pz) + 0.001) * z (k)))), black)
        end for
    end for
    View.Update
    cls
end loop

Author:  Homer_simpson [ Wed May 28, 2003 4:35 pm ]
Post subject: 

lol i could do the under 50 line thing...or could i?!
hmmmm i'll give it a try

Author:  Asok [ Wed May 28, 2003 4:36 pm ]
Post subject: 

Homer_simpson wrote:
lol i could do the under 50 line thing...or could i?!
hmmmm i'll give it a try


catalyst already posted the solution.

Author:  JSBN [ Wed May 28, 2003 5:07 pm ]
Post subject: 

Catalys scares me.....

Author:  Catalyst [ Wed May 28, 2003 5:23 pm ]
Post subject: 

y is that?

Author:  Asok [ Wed May 28, 2003 5:31 pm ]
Post subject: 

he probably meant to say "Catalyst owns me"

Author:  DJ [ Wed May 28, 2003 5:54 pm ]
Post subject: 

that crazy ass code went over my head

Author:  Homer_simpson [ Wed May 28, 2003 6:54 pm ]
Post subject: 

welll whadaya know i just created my own 3d engine...

Author:  Catalyst [ Wed May 28, 2003 6:57 pm ]
Post subject: 

well wasnt that easy?
try to add some features:
- hidden polygon removal (culling)
- lighting
- painter's algo
- optimizations


: