Computer Science Canada

3D in turing??

Author:  Jonny Tight Lips [ Tue Nov 23, 2004 5:45 pm ]
Post subject:  3D in turing??

Yah, I have a very general question about 3D graphics in turing. OK, all I want to knwo if how I would render 3D stuff in turing. I know there are many different ways and any of them would be helpful. And don't tell me it can't de done becasue I've seen it b4. Any and all sugestions are good. I've been messing around with drawing a cube using Draw.Line and a lot of math to get it to rotate but I'm sure and would hope that there is an easyer way to do it.

There might be a post already about this so sry but I was unable to find it so if there is just link me plz. Thanks

Author:  Mazer [ Tue Nov 23, 2004 5:55 pm ]
Post subject: 

No, it's really not possible. Go make your Zelda clone.


(Haha, kidding. Seriously, go make the game).

Anyways...
An easy way to get 3D points on a 2D screen is shown here.

Author:  Tony [ Tue Nov 23, 2004 7:28 pm ]
Post subject: 

besides, if you've seen it done, just look over the source code - namely their draw function to see how exactly 3D image is being rendered.

Author:  Jonny Tight Lips [ Tue Nov 23, 2004 7:52 pm ]
Post subject: 

Well, I saw it done by Dan. SO Dan if you would please post your code for that 3D space game you made it would be nice. ty

Author:  Cervantes [ Tue Nov 23, 2004 8:09 pm ]
Post subject: 

Or you could just go to the Best of the Turing Submissions and scroll down a little bit until you get to the link to the Engines. Homer's and Zylum's are both listed there.

EDIT: I guess I should mention this, just in case. You can also use the Posted Image, might have been reduced in size. Click Image to view fullscreen. function.

Author:  Jonny Tight Lips [ Tue Nov 23, 2004 8:10 pm ]
Post subject: 

Also, Coutsos I need a lil help understanding that site you linked.
This is the code they give at the end of the page.
code:
procedure 3Dto2D (x, y, z, pan, centre, position)

 

x = x + position.x

y = y + position.y

z = z + position.z

 

new.x = x*cos(pan.x) - z*sin(pan.x)

new.z = x*sin(pan.x) + z*cos(pan.x)

new.y = y*cos(pan.y) - new.z*sin(pan.y)

z = new.y*cos(pan.y) - new.z*sin(pan.y)

x = new.x*cos(pan.z) - new.y*sin(pan.z)

y = new.x*sin(pan.z) + new.y*cos(pan.z)

 

if z > 0 then

    screen.x = x / z * zoom + centre.x

    screen.y = y / z * zoom + centre.y

end if


yah well here are my questions.
1) how can they have pan.x? Is that one var or is it some combination of the 2?
2) What would be the point of my point after all of these steps? x,y?
3) What is the different between screen.x and just x
4) What is the variable pan for? Its for paning yes but if it = 1 what would that mean?
5) What programing language is this meant for?

Author:  Mazer [ Tue Nov 23, 2004 8:51 pm ]
Post subject: 

OK, how about this: ignore pan. In fact, why don't you go ahead and ignore the whole thing. The point of that was this:
code:

var pointx, pointy, pointz : int
% give the above variables some value, I don't care

drawdot(pointx, pointy, 12) % uh-oh, the drawdot() command only takes an x and y coordinate!

% if only we had a way to fake depth!
% LIGHTBULB! Let's divide the x and y coordinates by the z value! Just because!

drawdot( round(pointx/pointz), round(pointy/pointz), 12)

% one small problem... now everything is over in the bottom left corner...
% so let's try this:

drawdot( maxx div 2 + round(pointx/pointz), maxy div 2 + round(pointy/pointz), 12)


Really, that was the main thing.

Author:  Jonny Tight Lips [ Tue Nov 23, 2004 9:07 pm ]
Post subject: 

ok.... thas easy.
Wow that site mad everything so complicated with the sin and cos and AHHHHHHHHHHHHhhhhhhhhhhhhhhhhhhhhhh
Thanx for the help

Author:  Mazer [ Tue Nov 23, 2004 9:12 pm ]
Post subject: 

You'll need cos and sin for rotating things. Check out rotation matrices.

Author:  Jonny Tight Lips [ Tue Nov 23, 2004 9:43 pm ]
Post subject: 

Yay I want to rotate a cube. So where would I go to find these Matrixes you speek of? And how would I use them?





Is there a 3d equation??
like for example if I have a cube and I want it to rotate, is there some equations that will tell me that the points are supposed to be?

Also those 3d engines are great but where can I get the source code for them and not just the exe

Author:  Dan [ Wed Nov 24, 2004 2:23 pm ]
Post subject: 

Jonny Tight Lips wrote:
Well, I saw it done by Dan. SO Dan if you would please post your code for that 3D space game you made it would be nice. ty


I made that 3D engion for C++ and not turing.......if u are looking for one in turing, there is the sorce code for them posts some where in the turing sections. try using the sreach thing.

Author:  rizzix [ Wed Nov 24, 2004 3:34 pm ]
Post subject: 

i can never understand these sites, for ex the one Mazer Twisted Evil pointed out.

#1:
Quote:
screen.x = x / z * zoom
screen.y = y / z * zoom
dosen't make sense. Razz

ha! dividing by z will NOT give you the 3D-normalized-perspective-projection of any point onto your screen

Author:  Mazer [ Wed Nov 24, 2004 8:13 pm ]
Post subject: 

Why not? Perhaps you're doing something wrong? don't forget to add half the screen width and half the screen height so that the origin is at the middle of the window.
Dividing by z:
I'm sure you know that z represents the depth of the point. For our purposes, a greater z value means a greater depth and therefore, a point that is further of the camera. Which kinda makes sense because dividing x by a greater number will give you a smaller result. Right?
Let z = 2, x = 6
x / z = 3. OK.

Now let z = 8, x = 6
x / z = 0.75. Quite a bit smaller, right? Just like objects further away from your viewpoint appear smaller.

Does that make sense now?

Author:  Jonny Tight Lips [ Wed Nov 24, 2004 9:41 pm ]
Post subject: 

Ok, but dividing by z will give you a one point perspective. So how would I make that into 2 point perspective so that I can draw a cube with it?

Author:  Mazer [ Wed Nov 24, 2004 10:11 pm ]
Post subject: 

I'm not sure what you mean. Try drawing a cube with this. It'll be hard to visualise, but just pick 8 points and draw them. Have a variable for the angle, and for each point do this:
pointz := round (15 * sind(angle))
pointx := round (15 * cosd(angle))

The 15 is just a scalar because sin and cos will give you small values (between -1 and 1). You can change the 15 to whatever you want so you can see the points better.

Author:  rizzix [ Wed Nov 24, 2004 10:43 pm ]
Post subject: 

yea but Coutsos, i don't believe u'll get accurate results this way.. and i think the perspective will be messed up if the object is too close to the edges of the screen.

Author:  Jonny Tight Lips [ Thu Nov 25, 2004 12:04 pm ]
Post subject: 

Ok, thatis helping a lot but I still con't get it into a prog. here is my code. Fix it if you can.
code:
var pointz, pointx, pointy : int
var x,y : int := 10
var angle : int := 90
loop
pointz := round (15 * sind(angle))
pointx := round (15 * cosd(angle))
pointy := round (15 * sind(angle))
if pointx not= 0 and pointz not= 0 then
x := round (pointx / pointz) + maxx div 2
end if
if pointy not= 0 and pointz not= 0 then
 y := round (pointy / pointz) + maxy div 2
end if
Draw.Oval (x ,y ,20,20,1)
angle += 1
cls
end loop


Thank You

Author:  Mazer [ Thu Nov 25, 2004 12:39 pm ]
Post subject: 

OK, here's an example for the perspective (not real perspective, just that objects get smaller as they get further away).
It's a bit long because I've added crap to let you move the triangle (arrow keys for x/y, w and s for z).

code:

var win := Window.Open ("graphics:500;500,offscreenonly")
var pointx, pointy, pointz : array 1 .. 3 of real
var pixelx, pixely : array 1 .. 3 of int
var chars : array char of boolean
var increment := 5

pointx (1) := -40
pointx (2) := 40
pointx (3) := 0
pointy (1) := -40
pointy (2) := -40
pointy (3) := 40
pointz (1) := 1
pointz (2) := 1
pointz (3) := 1

loop
    Input.KeyDown (chars)
    if chars (KEY_LEFT_ARROW) then
        for i : 1 .. 3
            pointx (i) -= increment
        end for
    elsif chars (KEY_RIGHT_ARROW) then
        for i : 1 .. 3
            pointx (i) += increment
        end for
    end if
    if chars (KEY_UP_ARROW) then
        for i : 1 .. 3
            pointy (i) += increment
        end for
    elsif chars (KEY_DOWN_ARROW) then
        for i : 1 .. 3
            pointy (i) -= increment
        end for
    end if
    if chars ('w') then
        for i : 1 .. 3
            pointz (i) += .2
        end for
    elsif chars ('s') then
        for i : 1 .. 3
            pointz (i) -= .2
            if pointz (i) < 1 then
                pointz (i) := 1
            end if
        end for
    end if

    for i : 1 .. 3
        pixelx (i) := round (pointx (i) / pointz (i)) + maxx div 2
        pixely (i) := round (pointy (i) / pointz (i)) + maxy div 2
    end for

    for i : 1 .. 3
        drawline (maxx div 2, maxy div 2, pixelx (i), pixely (i), 23)
    end for
    drawline (pixelx (1), pixely (1), pixelx (2), pixely (2), 12)
    drawline (pixelx (2), pixely (2), pixelx (3), pixely (3), 12)
    drawline (pixelx (1), pixely (1), pixelx (3), pixely (3), 12)

    View.Update
    delay (30)
    cls

    exit when chars ('q')
end loop

Window.Close (win)

Author:  Jonny Tight Lips [ Thu Nov 25, 2004 10:04 pm ]
Post subject: 

Yes, That is a very nice pergrom and it does explain a lot about how the perspective works but I am still wondering how to rotate things?

Author:  Jonny Tight Lips [ Thu Nov 25, 2004 10:49 pm ]
Post subject: 

Ok, here is my prototype rotating cude. Use left and right arrows to rotate it. Still havn't been able to work in the perspective tho. Also any sugestions on how to make it rotate verticaly?

code:
setscreen ("offscreenonly")
var a, b, c, d : array 1 .. 2 of int
var e, f, g, h : array 1 .. 2 of int
var angle : array 1 .. 8 of real
angle (1) := 270
angle (2) := 270
angle (3) := 1
angle (4) := 1
angle (5) := 90
angle (6) := 90
angle (7) := 180
angle (8) := 180


a (1) := round (20 * sind (angle (1)) + maxx div 2)
a (2) := round (20 * cosd (angle (1)) + maxy div 2)
b (1) := round (20 * sind (angle (2)) + maxx div 2)
b (2) := round (20 * cosd (angle (2)) + maxy div 2 - 20)
c (1) := round (20 * sind (angle (3)) + maxx div 2)
c (2) := round (20 * cosd (angle (3)) + maxy div 2 - 20)
d (1) := round (20 * sind (angle (4)) + maxx div 2)
d (2) := round (20 * cosd (angle (4)) + maxy div 2)
e (1) := round (20 * sind (angle (5)) + maxx div 2)
e (2) := round (20 * cosd (angle (5)) + maxy div 2)
f (1) := round (20 * sind (angle (6)) + maxx div 2)
f (2) := round (20 * cosd (angle (6)) + maxy div 2 - 20)
g (1) := round (20 * sind (angle (7)) + maxx div 2)
g (2) := round (20 * cosd (angle (7)) + maxy div 2)
h (1) := round (20 * sind (angle (8)) + maxx div 2)
h (2) := round (20 * cosd (angle (8)) + maxy div 2 - 20)
loop
    cls
    var chars : array char of boolean

    Input.KeyDown (chars)

    if chars (KEY_RIGHT_ARROW) then
        for i : 1 .. 8
            angle (i) += 1
        end for
        a (1) := round (20 * sind (angle (1)) + maxx div 2)
        a (2) := round (20 * cosd (angle (1)) + maxy div 2)
        b (1) := round (20 * sind (angle (2)) + maxx div 2)
        b (2) := round (20 * cosd (angle (2)) + maxy div 2 - 20)
        c (1) := round (20 * sind (angle (3)) + maxx div 2)
        c (2) := round (20 * cosd (angle (3)) + maxy div 2 - 20)
        d (1) := round (20 * sind (angle (4)) + maxx div 2)
        d (2) := round (20 * cosd (angle (4)) + maxy div 2)
        e (1) := round (20 * sind (angle (5)) + maxx div 2)
        e (2) := round (20 * cosd (angle (5)) + maxy div 2)
        f (1) := round (20 * sind (angle (6)) + maxx div 2)
        f (2) := round (20 * cosd (angle (6)) + maxy div 2 - 20)
        g (1) := round (20 * sind (angle (7)) + maxx div 2)
        g (2) := round (20 * cosd (angle (7)) + maxy div 2)
        h (1) := round (20 * sind (angle (8)) + maxx div 2)
        h (2) := round (20 * cosd (angle (8)) + maxy div 2 - 20)
    end if
    if chars (KEY_LEFT_ARROW) then
        for i : 1 .. 8
            angle (i) -= 1
        end for
        a (1) := round (20 * sind (angle (1)) + maxx div 2)
        a (2) := round (20 * cosd (angle (1)) + maxy div 2)
        b (1) := round (20 * sind (angle (2)) + maxx div 2)
        b (2) := round (20 * cosd (angle (2)) + maxy div 2 - 20)
        c (1) := round (20 * sind (angle (3)) + maxx div 2)
        c (2) := round (20 * cosd (angle (3)) + maxy div 2 - 20)
        d (1) := round (20 * sind (angle (4)) + maxx div 2)
        d (2) := round (20 * cosd (angle (4)) + maxy div 2)
        e (1) := round (20 * sind (angle (5)) + maxx div 2)
        e (2) := round (20 * cosd (angle (5)) + maxy div 2)
        f (1) := round (20 * sind (angle (6)) + maxx div 2)
        f (2) := round (20 * cosd (angle (6)) + maxy div 2 - 20)
        g (1) := round (20 * sind (angle (7)) + maxx div 2)
        g (2) := round (20 * cosd (angle (7)) + maxy div 2)
        h (1) := round (20 * sind (angle (8)) + maxx div 2)
        h (2) := round (20 * cosd (angle (8)) + maxy div 2 - 20)
    end if

    Draw.Line (a (1), a (2), b (1), b (2), 1)
    Draw.Line (a (1), a (2), d (1), d (2), 1)
    Draw.Line (a (1), a (2), g (1), g (2), 1)
    Draw.Line (b (1), b (2), c (1), c (2), 1)
    Draw.Line (b (1), b (2), h (1), h (2), 1)
    Draw.Line (c (1), c (2), d (1), d (2), 1)
    Draw.Line (c (1), c (2), f (1), f (2), 1)
    Draw.Line (d (1), d (2), e (1), e (2), 1)
    Draw.Line (e (1), e (2), f (1), f (2), 1)
    Draw.Line (e (1), e (2), g (1), g (2), 1)
    Draw.Line (f (1), f (2), h (1), h (2), 1)
    Draw.Line (g (1), g (2), h (1), h (2), 1)
    View.Update
end loop

Author:  Jonny Tight Lips [ Sat Nov 27, 2004 7:58 pm ]
Post subject: 

So, anyone have any ideas how to make it roate verticaly? Or how to mark there be some perspective? Anyone at all? Well I'll keep trying...

Author:  rizzix [ Sat Nov 27, 2004 11:55 pm ]
Post subject: 

well.. if u know a little bit of Geometry, Vectors in pericular.. then it's a whole other story.

Author:  Jonny Tight Lips [ Sun Nov 28, 2004 1:32 pm ]
Post subject: 

Well I don't know them particularly. But I'm willing to learn Very Happy Maybe you could just make a sample code and then I could go from there? Well thanx anyway.


: