# How to calculate “real” distance between cursor and distant object?

I don't really know how to formulate the title, but the problem is the following:

I'm using a scripting language called "SQF" which has been created by a company named "Bohemia Interactive" (no, I do not work for them) and it's for modding their games. So I won't fill up these lines with code. If somebody else knows it and wants to see, comment and I'll add what I've tried so far.

The issue anyway is rather abstract, so I'm going to explain it with an archer and his target. Ratios may not be correct as they are only for explanation.

Say I (as the player) am the archer (first person), the target is 10m away, has a diameter of 2m and appears 200px in diameter on the screen. Now I aim 50px left of the target edge, execute my desired function and and get the info that this is 1m away from the target (2m from its center).

Now, I move 90m back, so the target is 100m away and appears 100px in diameter (don't know if that's correct, but you get the idea). Now I am aiming 25px left of the target and again, execute my desired function and it should tell me again that the distance is 1m.

Now, the "real" distance was 1m in both cases, but on my screen it was 50px at the first aim and 25px at the second.

So, what I am looking for is a (mathematical/programmatical) model to get the "real" distance between the target and where I aimed, no matter how far away I was from the target. I do have the positions of both archer and target in 3D coordinates. Though SQF is a very special language, it does provides a function to calculate the distance in meters and to dertermine certain geometrical values:

https://community.bistudio.com/wiki/Category:Command_Group:_Math

... so there's no need for that.

I know, this is very abstract, but I am hoping, somebody could give a hint or knows a mathematical procedure to do that.

If somebody knows how to do it in C, C++ or Java, I can write these languages, so that would be great!

• What do you mean by real distance? Do you mean just the distance between points in 3D? – Anko Aug 30 '14 at 21:05
• I mean the distance in meters, so yeah. The problem is that I don't know the second point. All I've got is a right triangle, the adjacent side and a vector (direction) representing the hypotenuse. What I want to calculate is the opposite side which I described in the explanation above. By "real distance", I mean the length of the opposite side in meters, cm or so and not in pixels or some other screen-based calculation. – Stacky Aug 31 '14 at 1:41

You cannot count with px, px are irrelevant because of different resolutions.
Cast a ray onto plane at your target location perpendicular to the direction(target loc - actor loc) vector. Then calculate the distance between ray(aiming direction) intersection with that plane and your target location on the plane.
Note, this is one way of doing it. If you dont need exact results or have only one resolution or have the situation simplified in any other ways, there could be easier solution.

tl;dr you will have to do math and read some tutorials on ray casting. Code and step-by-step explanation would not fit few paragraphs.

EDIT: another solution could be using trigonometric functions. Because you know hypotenuse is distance / cos(th). Where th is angle between direction to target and aiming direction and distance is...distance. Now you only calculate the length of opposite: sqrt(dist^2 - hypotenuse ^2), and that is your result.

• I know I have to do math. That's why I had to move my question from StackOverflow to MathOverflow and from there again to gamedev.stackexchange. I'm afraid, I can't make the situation easier since it's even more complex in the game. But I was hoping at least for a direction or "category" or a starting hint I'd have to investigate since I only had very basic 3D geometry in school, but non during my studies. I just need something as a basis to start searching from. Right now, my basis is "3D geometry" and that's waaaay to unspecific. – Stacky Aug 29 '14 at 12:28
• @Stacky what exactly are you programming? I think I gave you very specific things to google, any articles related to ray casting, ray tracing or 3d picking are related to this and will contain more specific answers. By the way, the second solution, though easier, will not help with any other trajectory then line. – wondra Aug 29 '14 at 12:46
• You're right, I oversaw that, nevermind. As I wrote, I'm scripting SQF, which is not really comparabe to other scripting languages like JavaScript since SQF is very limited to what the game engine can do and what scripters should be able/allowed to do. And since it's only known in the "scene", I didn't bother filling my question with code, unreadable to 95+% of the users here. But your last answer edit might have made my day! ;-) – Stacky Aug 29 '14 at 12:55
• I finally solved this by creating a vector on the "shortest path" (a), another on the view direction of the player (b) and then I used the language given function "vectorCos" to get the cosine between vector a and b. This data can now be used to calculate the length of the opposite side using the formula posted by @wondra above. – Stacky Sep 1 '14 at 7:28

The solution you spoke of in your comment on wondra's answer is unnecessarily complex, the engine has a fairly trivial solution to this problem:

// 3D Distance to object cursor is pointing at.
_distToCursorObject = player distance cursorObject;

// Same problem, using map distances.
_mapDistToCursorObject = player distance2D cursorObject;

hint format ["3D Distance: %1\nMap Distance: %2", _distToCursorObject, _mapDistToCursorObject];


Note that if the engine doesn't recognize an object (sky, water, non-object terrain features) you'll get a distance of 1e+10.