# GMS 2How can I determine distances in fake 3d?

#### helpless_coder

##### Member
[CLOSED]
Got help from the Math discord, and they came up with this solution:
GML:
``````if (relative != noone)
{
var start_point = argument1;
var end_point = argument2;
var input = argument3

if (input > 100) input = 100;
else if (input < -100) input = -100;

var distance = 400 * (power(input - 100, 2) - power(100, 2)) / (power(input - 100, 2) - power(200, 2)); // Thanks Ann

return lerp(start_point, end_point, distance * 0.01);
}``````
[OLD]
I'm sorry if I don't explain this well. Keep in mind that this is all 2D.

I want to have a formula for measuring the distance between two points in fake 3d space, and but I have no idea how to do it.

Let's say I want the cube to be 64 pixels thick. Using lerp I just change the last argument until it looks right. But that's not good enough.
How can I establish a uniform unit of length using the parallaxing background method, so that I could give an input of pixels and have it look right?

I imagine a script to look something like this:
`scr_fake3d( thickness, draw_length ) // where draw_length is the depth where the box is drawn`

Heres an image to hopefully illustrate the issue Last edited:

#### carlosagh

##### Member
That has to do with matrices but by measuring you mean whats on the 3D world or the view? If its the view you're talking about world to screen coordinates. There's a youtuber called Dragonite, he has a video explaining this and many other GMS2 3D tutorials he's been doing. The video covers drawing an overlay on the GUI depending on the object's coordinates in 3D space but the script he uses can also be used for that.

#### helpless_coder

##### Member
That has to do with matrices but by measuring you mean whats on the 3D world or the view? If its the view you're talking about world to screen coordinates.
I'm a bit confused by all these terms. All I'm doing is drawing lines and so there is no real 3d world.
I think it has to do with the view.

There's a youtuber called Dragonite, he has a video explaining this and many other GMS2 3D tutorials he's been doing. The video covers drawing an overlay on the GUI depending on the object's coordinates in 3D space but the script he uses can also be used for that.

#### carlosagh

##### Member
I'm a bit confused by all these terms. All I'm doing is drawing lines and so there is no real 3d world.
I think it has to do with the view.

Can't access youtube right now, sorry. You can't miss it just type "dragonitespam gms2 3d world to screen coordinates", it will sow up.

• helpless_coder

#### GMWolf

##### aka fel666
Fake 3D.
I hate that term.

If you have X, y, and z (width height, depth) then you are in 3D!
How you choose to render that 3D world is irrelevant to distances.

Or perhaps by fake 3D you mean not euclidian space? I'm which case, this will depend on your space.....

But really, drop the whole "fake 3D" idea. No such thing.
You are.just going to end up doing lots of slow 3D operations to render 'fake 3D'. All these 3D operations have been solved already and is what we call '3D'

Last edited:
• helpless_coder

#### dannyjenn

##### Member
I think I understand the question. I don't know the formula off the top of my head, but I think the length of that line is going to vary depending on whatever lens angle you're trying to simulate. (I'm not an expert, but I think a fisheye/wide-angle lens will result in a longer line, while a telephoto /narrow-angle lens will make it shorter.)

edit - To clarify, by "lens angle", I'm speaking of the lens's focal length. I don't mean "camera angle" which is unrelated.

• helpless_coder

#### dannyjenn

##### Member
Also note, in that picture all four lines are different lengths. So it not only has to do with lens angle but also the relative positioning of the point to the vanishing point.

• helpless_coder

#### helpless_coder

##### Member
Also note, in that picture all four lines are different lengths. So it not only has to do with lens angle but also the relative positioning of the point to the vanishing point.
That makes sense, so really I have to find the length between the front and back surface somehow. All of this is kind of melting my brain a little.

#### dannyjenn

##### Member
Here's an idea...

- First decide on some arbitrary number n such that there are n units between the xy-plane (at z=0) and the camera (at z=n).
- We know that stuff looks exactly twice as big when it's exactly half as far away. So since we already know what the xy grid is supposed to look like at z=0, we can also imagine a duplicate xy grid at z=n/2 that is scaled 200%.
- In the same way, we can figure out the scaling for the imaginary xy grids at every such z value. The one at z=n/4 should be 150% (because it's exactly halfway between z=0 and z=n/2), the one at z=3n/4 should be 400% (because it's exactly halfway between z=n/2 and z=n), the one at z=n/8 should be 175%, the one at z=7n/8 should be 800%, etc. (I think this is a logarithmic scale but I don't know off the top of my head. You probably just need to figure out the formula rather than do all the calculations yourself.)
- Then you can basically imagine each grid drawn scaled one on top of the other. Position them such that they all share the vanishing point in common. (This would be extremely easy if the vanishing point was in the top left corner. But unfortunately, you'll probably want the vanishing point to be in the center instead. So you'd need to come up with a formula determining the x and y offsets based on z.)
- Now it's just a matter of drawing the front face and connecting it to the back face using four lines (use the Pythagorean theorem or point_distance() to know how long these lines should be). Because the front face's position relative to the grid at z=whatever is exactly the same as the back face's position relative to the grid at z=0. The front face's scale as well as position are determined by its z, as explained above. (Hope that made sense)

edit - If this is confusing, maybe I'll try and make some diagrams or a sample file or something. I haven't actually tested any of this yet, but I think it could work.

Last edited:

#### helpless_coder

##### Member
Here's an idea...

- First decide on some arbitrary number n such that there are n units between the xy-plane (at z=0) and the camera (at z=n).
- We know that stuff looks exactly twice as big when it's exactly half as far away. So since we already know what the xy grid is supposed to look like at z=0, we can also imagine a duplicate xy grid at z=n/2 that is scaled 200%.
- In the same way, we can figure out the scaling for the imaginary xy grids at every such z value. The one at z=n/4 should be 150% (because it's exactly halfway between z=0 and z=n/2), the one at z=3n/4 should be 400% (because it's exactly halfway between z=n/2 and z=n), the one at z=n/8 should be 175%, the one at z=7n/8 should be 800%, etc. (I think this is a logarithmic scale but I don't know off the top of my head. You probably just need to figure out the formula rather than do all the calculations yourself.)
- Then you can basically imagine each grid drawn scaled one on top of the other. Position them such that they all share the vanishing point in common. (This would be extremely easy if the vanishing point was in the top left corner. But unfortunately, you'll probably want the vanishing point to be in the center instead. So you'd need to come up with a formula determining the x and y offsets based on z.)
- Now it's just a matter of drawing the front face and connecting it to the back face using four lines (use the Pythagorean theorem or point_distance() to know how long these lines should be). Because the front face's position relative to the grid at z=whatever is exactly the same as the back face's position relative to the grid at z=0. The front face's scale as well as position are determined by its z, as explained above. (Hope that made sense)

edit - If this is confusing, maybe I'll try and make some diagrams or a sample file or something. I haven't actually tested any of this yet, but I think it could work.
I think you're definitely on to something, I'm trying to understand it fully so just give me some time to digest it.
A formula is really the solution to this because then I could modify it to whatever 'base unit' and scale things proportionally.