Mathematic/Geometric Question: Picking a object in 3D

Smart people ~ Great Scripts
User avatar
DesignDevil
Master Chief Petty Officer
Posts: 500
Joined: 22 May 2009, 08:52
Type the number ten into the box: 0
Location: Neuruppin, Germany, Earth.

Mathematic/Geometric Question: Picking a object in 3D

Post by DesignDevil »

hi together,

i need a bit help. I post here instead of the developer plugin to (hopefully) get more answers. I want to know how i detect which object the mouse is picking in the 3d scene.

I don't need the API command for this - i know this. I want to write my own function for this. From mouse click until selection.

What i found out so far is that i need the position of the mouse on screen (x, y) and the aspect ratio of the screen. With this informations i could "create" a ray from near to far by adding 0 and 1 to this coordinates.

Code: Select all

near = ( screen_space_x, screen_space_y, 0 )
far  = ( screen_space_x, screen_space_y, 1 )
Now i had to convert this into world coordinates by applying the inverse of the view and the projection matrices.

Here comes my first problem: where can i get this matrices?

But i found a easier way because there is a function in the API which gives me the origin and the direction of the mouse directly from the screen position:

Code: Select all

tsxAViewGetMouseRay( mx, my, pRayOrigin, pRayDirection )
So i could forget the first problem. But the next one comes quickly ;)

Ok, pRayOrigin is the point of the eye in the world. pRayDirection is the normalized vector from this point into the world thrue the mouse point.

What i now need is a method to check the bounding box of each visible object in the scene against this ray.

Here i found a few solutions in the net but i don't understand all of them.

I hope anybody can help me out.
I still use TrueSpace 6.6
Link: My Youtube Channel
Link: www.DesignDevil.de - Plugins, Tutorials and more about TrueSpace 6.6
froo
Captain
Posts: 2554
Joined: 22 May 2009, 12:13

Re: Mathematic/Geometric Question: Picking a object in 3D

Post by froo »

Hi DD. I think there is a function in the 6.6 SDK getting bounding box data of an object.
I am not sure if there is a function which returns a list of objects visible from your camera or viewing angle.
It definitely would not be efficient to check every object in the scene.

But if you have the positions of your objects, and can get the bounding box info, you could calculate the
angles spanned by each object of interest, and of course calculate the distance to each object.
By 'angles spanned', I mean, absolute azimuth (horizontal angle) and elevation (vertical angle).
You can also calculate the mouse's 'look angle' or, really, just use the view angle of your view.
With this information, you can determine whether the mouse's angle (ray angle, I suppose) is within
the object's bounding box angles (azimuth and elevation). If it is, you can add that object to the 'short list'
of possible objects. Then you look at the short list, comparing distance from the mouse to the object,
and select the closest object.
User avatar
DesignDevil
Master Chief Petty Officer
Posts: 500
Joined: 22 May 2009, 08:52
Type the number ten into the box: 0
Location: Neuruppin, Germany, Earth.

Re: Mathematic/Geometric Question: Picking a object in 3D

Post by DesignDevil »

Get the bounding box of the objects is not a problem, there is a function in the API to do that.

Checking just the visible objecs which are visible thrue the cam is faster as to check all objects in the scene - but this is for tweaking the function if i know that he works ;)

Back to your description. I don't really understand how the angles could help to find out a intersection between the ray of the mouse and the object - or his bounding box.

(Maybe i'm to stupid or my translation is to bad). Could you please draw this for a better understanding? This would be great - thanks a lot.

Well, my mathemetic experience (or geometric) is not very deep. I read a lot of tutorials but the most stuff is to much for my brain. Understanding the idea of vectors and matrices etc. is not so difficult but to put all this stuff together with tS API (in a practicle way) is to difficult for me. Here i need a good teacher. If i could understand all these 3D stuff better i had such a lot of ideas (plugins) that i would write.
I still use TrueSpace 6.6
Link: My Youtube Channel
Link: www.DesignDevil.de - Plugins, Tutorials and more about TrueSpace 6.6
froo
Captain
Posts: 2554
Joined: 22 May 2009, 12:13

Re: Mathematic/Geometric Question: Picking a object in 3D

Post by froo »

Hi DD. Here you go.
This could be done by accessing the object's transformation matrix, and getting the angle value
from that, probably. Or there may be a function to get the angles directly.

I will send you a Private Message in a sec as well.
mouse interection.JPG

Return to “Scripts and SDK”