Follow
yellowbaron

Hi,
I would like to add a basic 3d outside view to my flight sim project Tiny Sim. For this I started with the code below, which is just showing a simplified runway (essentially, a rectangle with some points). I am trying to get the projection function right and while it works for z<0 it fails miserably for z>=0: the runway is somehow mirrored in the upper half of the screen.

 ```rwy = {{{-1,0,0}, --points {-1,0,30}, {1,0,30}, {1,0,0}}, {{1,2}, --lines {2,3}, {3,4}, {4,1}}, 4} cam = {0,1,-4} mult = 64 sp = 0.01 function _update() if btn(0) then cam[1] -= 0.1 end if btn(1) then cam[1] += 0.1 end if btn(2) then cam[2] += 0.1 end if btn(3) then cam[2] -= 0.1 end if(cam[2]<0) cam[2]=0 if btn(4) then cam[3] -= 0.1 end if btn(5) then cam[3] += 0.1 end end function _draw() cls() print("x="..cam[1],0,6*1) print("y="..cam[2],0,6*2) print("z="..cam[3],0,6*3) draw_shape(rwy) for i=1, 14 do draw_point({0,0,i*2},7) end draw_point({-0.5,0,2},7) draw_point({0.5,0,2},7) end function draw_shape(s,c) for l in all(s[2]) do draw_line(s[1][l[1]], s[1][l[2]], c) end end function draw_line(p1,p2,c) x0, y0 = project(p1) x1, y1 = project(p2) line(x0, y0, x1, y1, c or 6) end function draw_point(p,c) x, y = project(p) pset(x, y, c or 11) end function project(p) x = (p[1]-cam[1])*mult/(p[3]-cam[3]) + 127/2 y = -(p[2]-cam[2])*mult/(p[3]-cam[3]) + 127/2 return x, y end --functions for roll, pitch and yaw```

Runway being displayed correctly (z<0):

Runway with wrong projection (z>=0):

I split up the projection function for z<0 and z>=0 but did not find the right formula for z>=0 yet:

 ```function project(p) if cam[3]<=0 then x = (p[1]-cam[1])*mult/(p[3]-cam[3]) + 127/2 y = -(p[2]-cam[2])*mult/(p[3]-cam[3]) + 127/2 else x = (p[1]+cam[1])*mult/(p[3]+cam[3]) + 127/2 y = -(p[2]+cam[2])*mult/(p[3]+cam[3]) + 127/2 end return x, y end```

Any help is much appreciated!

P#59388 2018-11-27 14:46

 100

Cart #tenkizoti-4 | 2020-04-10 | Code ▽ | License: CC4-BY-NC-SA
100

### Hi Aviator!

This is a tiny flight simulator based on a Cessna-172R with a G1000 glass cockpit. (This is the plane I fly in real life, so I thought it would be a challenge to see if it can be done with Pico-8.)

The flight model is not based on theory but on my observations as a pilot. I have to say that it's quite good ;-) Within the limits of Pico-8 it quite feels like the real thing.

Disclaimer: while I created the instruments and the flight model, the majority of the programming work was done by @freds72, including 3D outside view, code structure, eye candy, music, etc. Many thanks Fred, this would not have been possible without your ninja programming skills!!

If you want to go flying right away, the cheatsheet will get you in the air in less than 1 minute:

If you are more the serious type, there is a 34-page flight manual, which explains everything in detail and features 20+ flight lessons.

We also created an entire area you can fly in (with a mountain, an island, ships and much more):

Just as a sneak preview, here are some of the things you can do:

Smooth landings

High speed buzzing

Illegal low-level flying at night

Instrument flying in the clouds

Here is the real cockpit on a flight in the Swiss alps to compare:

### Update 1.1

• fixed polygon gaps
• added collision detection for wind turbines

### Update 1.2

• sub-pixel polygon rasterization
• faster textured clouds
• fixed ugly crash

### Update 1.3

• fixed: crash when geometry is fully clipped
• added: HUD-mode (player 2 down key to toggle)
P#20854 2016-05-19 08:30 ( Edited 2020-04-10 12:40)