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 / Help with "tracking" a 3D object in 3D space.

Author
Message
spike 132000
18
Years of Service
User Offline
Joined: 7th Nov 2007
Location: Nottingham, UK
Posted: 21st Sep 2011 00:31
Hi guys, im having a few issues trying to get my head around the math of trying to detect where a 3D object is in my 3D space. The object is quite a few "units" away from my player and cannot be seen from where I am. So what I would like to do is have a sort of HUD. Essentially, i would just like to have a red dot on the page that tells me, if I head towards that red dot, I will eventually reach my object in 3D place.

Any help?

Maybe also, if it is behind me, put an arrow around the edges? If you get what I mean?

Thanks

http://feedit.me - Live feed platform, useful for events and shows!
Da_Rhyno
15
Years of Service
User Offline
Joined: 25th May 2011
Location:
Posted: 21st Sep 2011 01:34 Edited at: 21st Sep 2011 03:10
You mean like a radar?

Here's a solution: For simplicity sake, say we're dealing with a First Person Shooter where Y is up.

You get the X and Z position coordinates of you and of your 3D object.

Then you have a circular area on the screen (your radar.) Set yourself to the origin of that radar (the center of it). Then use that same system, to a certain scale of everything to place the 3D object's position with respect to you on that radar.

So say you have a radar with a radius of 10 units. You are at a world position of <10, 5, 30>. Your object is at a world position of <20, 5, 50>. In this scenario, you throw out the 5 (the Y-coordinate) and have 2D coordinates of <10, 30> and <20, 50>. You want the center of your radar (let's say, point 0,0 on your HUD) to be your position. What do you do? You subtract <10, 30> from your position to accomplish that. To get the 3D object in the same coordinate system (radar coordinates) you subtract the same values from it, so it's left with <10, 20>.

Now you scale the positions to fit the radius. "How do I do that?" you may wonder. Well, it's as easy as dividing the coordinates of you and your object by the radius of your radar. Since you're at <0, 0> in this example, then you'd end up with <0/10, 0,10> = <0, 0> still. For your object, it would be <10/10, 20/10>, which is <1, 2> as the radar coordinates.

I hope this helps.

EDIT:
I feel like I should go more in-depth with this. For one, chances are that the center of your circle (radar) won't be at <0,0> on your screen. The same concepts from above apply though, no matter where the radar is in effect at. Say it's at <50,50> on your screen. What you do with what you have above is add <50,50> to both of those 2D points you got at the end of our formula there.

Another thing is that the object might be so far away that when you do the conversion it would lie outside the radius. To test for this, you get the distance between the two points to see if it's at most the radius of our circle (10, in this case). The distance formula is

Sqr((object x - player x)^2 + (object y - player y)^2)

If that formula returns a value greater than 10, then we want to set the object's point drawn on the radar so that it fits inside the circle. One method of doing this is by taking the vector from the origin of our radar (our player position) to the dot (where the object is), and making it a length of 1 (also called "normalizing" the vector), then multiplying that by 10.

How we can do that is take that distance we just got, and dividing that vector by it:



Another thing you'd need to do is get the angle between your camera's direction and the object and apply that same angle relation to your radar. What you can do is get the Y angle of the camera with respect to angle 0 using CAMERA ANGLE Y() and then get make three more vectors, one going from the camera to some point on the Y angle 0 (let's call it vector A), one going from the tip of the Y angle 0 vector we just made to the position of the 3D object (let's call it vector C), and one going from the camera position to the position of the 3D object (let's call it vector B). Then what you do is get the distances of each vector (let's call them a, b, and c respectively)and apply the following formula from the Law of Cosines:

angle = arccos((a^2+b^2-c^2)/(2*a*b))

Add those angles together, and that will give us the angle we then need to apply from the angle 0 of our radar to the point of our 3D object on the radar. How do you do this? Just multiply the x coordinate of our 2D point on our radar by the cosine of the angle, and the y coordinate of our 2D point on our radar by the sine of the angle.

If you need some code tips, I'll be happy to help, though it may not be right away.
spike 132000
18
Years of Service
User Offline
Joined: 7th Nov 2007
Location: Nottingham, UK
Posted: 21st Sep 2011 02:02
Thanks for the help, not exactly what I am after though.

Say I am at 0,0,0

My 3D Object is at 123,456,789

On my screen of WIDTH/HEIGHT (example 640,480)
on that screen I would like it to just show a red dot to say, if you head towards me, you will find a 3d object here.

http://feedit.me - Live feed platform, useful for events and shows!
Da_Rhyno
15
Years of Service
User Offline
Joined: 25th May 2011
Location:
Posted: 21st Sep 2011 03:01 Edited at: 21st Sep 2011 03:14
Check my post again.

You can use the same concept of getting the angle from the Y Angle origin to the object, and the Y Angle of the camera, and compare them to see if the object is within the angle range you want for it.

Login to post a reply

Server time is: 2026-07-10 17:18:58
Your offset time is: 2026-07-10 17:18:58