# Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

### DarkBASIC Professional Discussion / Need help with distance using sqrt()

Message
Posted: 9th Oct 2016 21:12 Edited at: 9th Oct 2016 21:26
I need to find the distance from an object in 3D space, but I only need the X and Z dimensions.

I've tried these 2 formulas but the problem is that in 3D it calculates the width, height and depth and in 2D it calculates the width and height. What I need is the width and depth and not the height. So I need the 2D formula but in 3D space.

Any suggestions? It might be that I just have to set the Y dimension to a small number but I've tried that and it doesn't seem to work.

I need it for this game that I've been programming. It's a 3D game with a top-down view and I need to calculate a distance above the player (X, Z dimensions) so I can hide objects that are above the player
Posted: 9th Oct 2016 21:32 Edited at: 9th Oct 2016 21:33
In 2D use the z dimension not the y.
Posted: 9th Oct 2016 21:40 Edited at: 9th Oct 2016 21:40
that's what I thought too but it didn't work, the circle that it creates is still width and height
Posted: 9th Oct 2016 21:52
How are you creating the circle. Some brief code demonstrating the problem would help.
Posted: 9th Oct 2016 22:00
like this

In 2D: sqrt((x1 - x2)^2 + (y1 - y2)^2)
In 3D: sqrt((x1 - x2)^2 + (y1 - y2)^2 + (z1 - z2)^2)

but if you use the 2D one it creates a circle in 2D, I think I need to use the 3D formula and somehow squash the Y dimension very small but I don't know how to do that
Posted: 9th Oct 2016 22:05
Quote: " the circle that it creates is still width and height"

How are you creating the circle using the distance formula?
Posted: 9th Oct 2016 22:13
I'm not creating a circle, the formula creates a radius that it uses, in 3D it creates a sphere and in 2D it creates a circle.

So if you wrote:
if sqrt((x1 - x2)^2 + (y1 - y2)^2 + (z1 - z2)^2)<50
end
endif

This will create a sphere of detection and if you are within 50 pixels in this sphere then the program will end.

In 2D it has a circle of detection
Posted: 9th Oct 2016 22:40
Like a hidden box or cylinder with the X and Z dimensions of the player that extends above and follows the player used for collision detection to hide the object above.
Posted: 9th Oct 2016 22:49
due to my code I can't use a simple object collision command because a lot of other things are based on object collision being 0. I have to do it through ray casting and distance
Posted: 9th Oct 2016 22:52
what I've got is a ray cast from the player up towards the camera view, then it detects the object that is between the player and the camera, then I need to detect all other objects on that same height in an area around it. I've done the ray casting part, can't work out the distance part because I don't want to hide objects that are underneath this height level or above it, so I need just X and Z dimensions.
Posted: 9th Oct 2016 23:19
Use the x, y and z values returned by the ray cast, then hide any object above and outside x and z distance. Probably easier said then done. Sorry.
Posted: 9th Oct 2016 23:41
thanks, I'll keep playing around with it.
Posted: 10th Oct 2016 00:12 Edited at: 10th Oct 2016 00:15
I've figured it out, it was actually quite simple. I just had to put if object position y() is same as the object that is directly above the player.

I'll post a video later tonight on the games forum page here which showcases this.