Log In  


Cart #zirusohika-1 | 2023-07-21 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
2

Well, its not a miracle.
Tutorial just shown how to make, not how to understand, but i really want to understand how 3d works and thats why im asking you guys. Does anyone have really good posts about 3d, maybe videos...?
I know that 3d graphics are actually projections of 3d coordinates onto screen, what i dont get is HOW they are projected. sometimes there is full camera concept, sometimes(like in 3d dots demo) there is just some magic functions that i dont a single idea how they are working. i know how DOOM's 3d works, more or less, its an easy concept, but "actual" 3d is just dark woods for me. Thanks for reading this.

2


Yeah there is this really helpful post in Pico.
How 3D works


@cheesemug thanks, but that exactly what i do know - a doom style 3d. this style of 3d cant be rotated up or down due to it being more of 2d than 3d.


3

So, first, let me dispel a few misconceptions. The style of 3d used in the post that cheesemug posted isn't the style that Doom used. It's a generalization of what Wolfenstein 3D used for non-grid maps. That may seem like what Doom was, but it's not. Doom did a lot more, including having a 3rd dimension in each corner and in its gameplay. The reason Doom is so often referred to as not real 3d is because the optimizations disallowed rooms being on top of each other (and also proper bridges), and then editors and the in-game map took advantage of this to make easy 2d views of the maps. This is particularly true of the N64 and PSX versions of Doom, as they only stored a bit of extra pre-calculations of the existing maps so that their own 3d rendering techniques could be used.

I'm not just ranting for no reason or because I really like game history. This is important. If you make anything 3d in pico-8, you will need to decide what level of optimization to use and what level of lag is acceptable. This is also important because the techniques used in that post are actual 3d rendering techniques. They're just simplified. In particular, the bits about dividing by distance are the fundamental concepts behind a perspective projection.

And here we get to the crux of the issue. What you're thinking of as "3d" probably is a perspective projection, because that's what our eyes use (with automatic adjustments in our brain applied, but that doesn't matter because they're applied even when looking at pictures). The easiest way to get 3d in pico-8 is just to roll out your own layered map representation and then adjust the y-position and/or size of sprites based on their z coordinate in game space just so the player can distinguish. Drop shadows help with that as well. I'm guessing you want what's in that cartridge though. To summarize, perspective projection just divides 2 chosen dimensions by distance from viewer, adjusted in a pleasing way, to get the 2d version.

So, briefly, here's what the cartridge does. The rot() function is a 2d rotation around the origin. It's the same formula listed in this wikipedia article under "in two dimensions". The "camera space" section is using the 2d rotation function applied to each axis to simplify the math. That way it can more easily get a 3d rotation. If I understand correctly, it's also implicitly assuming the camera is at the origin to keep the code even simpler. Otherwise it'd be subtracting the camera's position from each object's position first. The reason to do all this is to get where the circles look to be from the view of the camera. The "sort" section is re-ordering the objects based on distance from the camera so it can use the painter's algorithm (draw further things first so closer things can paint over them). The "screen space" section then shifts them so that all points with x and y values of 0 are at the coordinates of (64,64) (the center of the screen). It also calculates the radius based on distance from the camera and zoom level (the 6). Lastly, it only draws circles that have a z value in front of the camera by a threshold amount (so they don't cover the screen by being too big).

All those adjustments to position and rotation may be easier to understand if you know how the 2d version looks and what pico-8's camera() function actually does. In short, the view of an object from the perspective of another object is just the difference. Given o1 and o2 with (x,y) positions, o1's view of o2 would be (o2.x - o1.x, o2.y - o1.y). Thus pico-8's camera() function just sets amounts to be subtracted from the positions given by draw commands. Rotated cameras work the same way, just that "subtracting" rotations from each other is more complicated math.

Now, there is one other concept that the cartridge skips: stretching and cropping to fit the view. This is usually described as "aspect ratio" and "focal length" in more advanced code to fit with the camera analogy, but it's basically how much distance matters in each of the 2 screen dimensions.

So, can you use only the concepts in the cartridge to make an fps or something similar? yes. The hardest parts would be 1. filling spaces efficiently and 2. clipping lines in space to make sure you don't go behind the camera (because that invalidates your projection math causing weird results). Would you want to? Probably not. The reason is that it's really slow. This is why optimizations like in Wolfenstein 3d and Doom were necessary.

There's two main types of optimizations you can try to use: 1. optimizations based on known game limits (like Doom used) or 2. optimizations based on the math itself (what generalized renderers use). 1 would require more info that I feel like writing right now, so I'll skip that. 2 however just requires 1 concept for this scope of 3d rendering: linear transformations (with matrices).

The issue with just using the cartridge style is that all the trigonometric calculations add up to a lot of time. Instead, it's faster to just store how things change after doing the trig stuff once or twice. Then you can apply the change to every point that it's relevant to. In practice, this means the vast majority of the trig function calls get replaced with multiplication and addition. Matrix representations of the changes allow doing that and even chaining multiple changes together, as long as each change can be represented with only addition and multiplication. This last condition matters because in practice it means perspective projections need to be 4 dimensional, with the 4th coordinate (usually w) being used to divide the other coordinates. Now, I'm not going to give the actual math here, partially because it's too much for an already long post but also because I honestly am terrible with it. The handed-ness of 3d coordinate representation always throws me off. If you're interested in using that sort of approach, I recommend googling a linear algebra primer so you have some of the math basis then googling the needed transformation matrices (translation, rotation, scaling, perspective, and viewport).


@kimiyoribaka thanks for the answer. Your could skip the first part of the reply thus im aware of doom having 3rd dimension, i didnt reject the fact that doom is a 3d game, it just renders a bit differently.
My bad i didnt wrote what i do know in initial post, i just was execpting a simplier answer. Y and X are divided by distance, and adjusted to center( same as camera(0, 0) function would do). Sorts are also easy conception, its in a lot of games rendering routine. But rot function... Well, thats where i start having questions. I was thinking that its kind of trick to make things look 3d, that will only work in this specific cart. I might be wrong tho.
So if im understanding this right, this cart is written to work... Just in that cart(who would thought?), and using its 3d is not really efficient. I have heard of projection matrices and all that trig stuff. I even tried to make 3d cube by Javidx tutorial but i really quickly stopped understanding things. Thank you very much, you made me understand a lot. Maybe not quite the time for 3d yet.


the cheesemug's cart and the commentary are really interesting and informative. Great.


Hey, ioil13. Don't give up on 3D if it's something you really want to understand, it's mostly just a question of what level of detail you're interested in and how deep down the rabbit hole you want to dive.

Are you looking to understand 3D projection in a general way with all (or most) of the math stripped away so you're just getting the concepts? Or do you want to get into the nitty-gritty details of the mathematics behind it? Or something in between? Or do you just want to understand the code in your cart specifically or even just what exactly that rot function is doing? Any or all of that is possible it just depends what you're looking for.

> "Does anyone have really good posts about 3d, maybe videos...?"

https://www.youtube.com/watch?v=p4Iz0XJY-Qk (It's another spinning cube tutorial)

I haven't watched this particular video myself so I can't promise it will help. But I do know the channel and things are usually explained fairly well and enthusiastically so maybe give that one a try. He'll be coding in either Java or javascript but if you don't know either of those don't worry too much about the code and just focus on the concepts.



[Please log in to post a comment]