3D world using 2D commands by Libervurto20th Jul 2007 17:16
|
---|
Summary This program uses perspective to simulate three dimensions while only using 2D commands. WARNING: THIS IS NOT ACCURATE 3D, IT IS MERELY IMITATING PERSPECTIVE. CONTROLS LEFT/RIGHT - Description Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com remstart -------------------------------- THREE DIMENSIONS IN A 2D WORLD -------------------------------- The main obstacle for creating a 3D world on a 2D screen is obvious; the screen is 2D! There is no depth. So how do we create a third dimension? We need to add perspective. Perspective can be at any angle but for this example I am using the most simple, 1:1 (X:Y). To understand what this means, think of the classic 2D drawing of a cube. The cube is made up of 2 offset squares and four diagonal connecting lines. {image} Because this is a cube, we know that all lines MUST be equal in length, but in the drawing the connecting lines are half the length of the others. This is because of perspective (1:1), half of the cube's depth was added to the X axis, and the other half added to the Y axis, resulting in a perfectly diagonal line. Now let's take this on another step. Lets use our cube as the boundaries to our 3D world, and lets now try and imagine how another 3D object would be displayed in this world. Firstly, it would be positioned according to its X and Y position, and then these would both be increased by half its Z position. So by moving the object towards and away from us, its really travelling along a diagonal line, left/down and right/up. remend `-------------------------------- ` SETUP `-------------------------------- `world Boundaries Wx1 = 15 : Wx2 = 320 Wy1 = 200 : Wy2 = 30 Wz1 = 0 : Wz2 = 160 `Cube position Cx = 0+Wx1 Cy = 0+Wy1 Cz = 0+Wz1 `Cube volume Cvx = 10 Cvy = 10 Cvz = 5 : `half actual size for simplicity (no need to divide by 2) `-------------------------------- ` MAIN LOOP `-------------------------------- sync on DO cls gosub Axis gosub Cube gosub control gosub bind_cube sync LOOP `-------------------------------- ` GOSUBS `-------------------------------- axis: `x-line ink rgb(255,0,0),0 line 15,200,320,200 `y-line ink rgb(0,255,0),0 line 15,200,15,20 `z-line (X+Y/2) ink rgb(0,0,255),0 line 15,200,160,55 RETURN `------ cube: ink rgb(255,255,255),0 `* FG Square `Fore Bottom Horizontal line (Cx+Cz),(Cy-Cz), (Cx+Cz)+Cvx,(Cy-Cz) `Fore Top Horizontal line (Cx+Cz),(Cy-Cz)-Cvy, (Cx+Cz)+Cvx,(Cy-Cz)-Cvy `Fore Left Vert line (Cx+Cz),(Cy-Cz), (Cx+Cz),(Cy-Cz)-Cvy `Fore Right Vert line (Cx+Cz)+Cvx,(Cy-Cz), (Cx+Cz)+Cvx,(Cy-Cz)-Cvy `* BG Square `Fore Bottom Horizontal line (Cx+Cz)+Cvz,(Cy-Cz)-Cvz, (Cx+Cz)+Cvx+Cvz,(Cy-Cz)-Cvz `Fore Top Horizontal line (Cx+Cz)+Cvz,(Cy-Cz)-Cvy-Cvz, (Cx+Cz)+Cvx+Cvz,(Cy-Cz)-Cvy-Cvz `Fore Left Vert line (Cx+Cz)+Cvz,(Cy-Cz)-Cvz, (Cx+Cz)+Cvz,(Cy-Cz)-Cvy-Cvz `Fore Right Vert line (Cx+Cz)+Cvx+Cvz,(Cy-Cz)-Cvz, (Cx+Cz)+Cvx+Cvz,(Cy-Cz)-Cvy-Cvz `* Connectors `LB line (Cx+Cz),(Cy-Cz), (Cx+Cz)+Cvz,(Cy-Cz)-Cvz `RB line (Cx+Cz)+Cvx,(Cy-Cz), (Cx+Cz)+Cvx+Cvz,(Cy-Cz)-Cvz `LT line (Cx+Cz),(Cy-Cz)-Cvy, (Cx+Cz)+Cvz,(Cy-Cz)-Cvy-Cvz `RT line (Cx+Cz)+Cvx,(Cy-Cz)-Cvy, (Cx+Cz)+Cvx+Cvz,(Cy-Cz)-Cvy-Cvz `* Y height line (ground up) ink rgb(0,255,0),0 line (Cx+Cz)+(Cvx/1.6),(Wy1-Cz)-(Cvy/1.6), (Cx+Cz)+(Cvx/1.6),(Cy-Cz)-(Cvy/1.6) RETURN `------ control: if rightkey() = 1 then inc Cx,2 if leftkey() = 1 then dec Cx,2 if upkey() = 1 then dec Cy,2 if downkey() = 1 then inc Cy,2 if shiftkey() = 1 then inc Cz,1 if controlkey() = 1 then dec Cz,1 RETURN `------ bind_cube: if Cx < Wx1 then Cx = Wx1 if Cx > Wx2 then Cx = Wx2 if Cy > Wy1 then Cy = Wy1 if Cy < Wy2 then Cy = Wy2 if Cz < Wz1 then Cz = Wz1 if Cz > Wz2 then Cz = Wz2 RETURN |