Clicking on 3d object - positon of click

Started by
9 comments, last by MrProper 13 years, 7 months ago
Hi, I am trying to make 3d strategy game. I am using mouse to click on gameboard, but problem is that i cant determine where I am clicking on board, all I can get is possition of cursor on screen. I guess theres some equation, but cant find it or make it out.
Any help is appreciated.
Advertisement
Most APIs provide a function called something like unproject for this kind of thing. For example XNA has Viewport.Unproject and GL has gluUnProject().
You can also calculate it yourself.
Get the distance between the screen plane and the camera.
That will be the z component of your ray.
then the x and the y coordinates of your mouse will be the x and y of the ray.
Then you multiply the ray by the camera matrix and you should have a ray that can be used to check mouse object intersections.
Quote:Original post by Hedanito
Then you multiply the ray by the camera matrix and you should have a ray that can be used to check mouse object intersections.
Can you clarify what you mean by 'camera matrix'?
Quote:Original post by jyk
Quote:Original post by Hedanito
Then you multiply the ray by the camera matrix and you should have a ray that can be used to check mouse object intersections.
Can you clarify what you mean by 'camera matrix'?


The orientation matrix of the camera.
If it's 3x3 matrix, don't forget to add the translation(position of the camera).
Check this sample if your working with XNA.
http://creators.xna.com/en-US/sample/picking
It's very straightforward.
It helped me a lot with the RTS game I'm programming right now!
__________________________________________My Blog
Quote:Original post by Hedanito
The orientation matrix of the camera.
If it's 3x3 matrix, don't forget to add the translation(position of the camera).
I believe your solution is incorrect. Have you actually tried the method you describe? Did it work correctly? (If the answer is yes, then I'm guessing that you haven't accurately described whatever method you're using.)
If your using OpenGL you can use glUnproject(). Theres lots of tutorials on the internet on how to use that function correctly
Quote:Original post by jyk
Quote:Original post by Hedanito
The orientation matrix of the camera.
If it's 3x3 matrix, don't forget to add the translation(position of the camera).
I believe your solution is incorrect. Have you actually tried the method you describe? Did it work correctly? (If the answer is yes, then I'm guessing that you haven't accurately described whatever method you're using.)


Yes I have tried it out. I actually used it in a ray tracer once, which basically send a ray for every pixel. So instead of sending it for every pixel you send it for the pixel that the mouse is on. It's really straightforward. The harder part is the collision with whatever object you want it to hit. Though since he is talking about a gameboard, a simple line plane collision would suffice.

I did notice I forgot one thing. You have to substract half the screen width from the x position and half the screen height from the y position.

It would also be nice if you state why you think it's wrong, then I can go either like "no way!" or "aaah silly of me".
Quote:It would also be nice if you state why you think it's wrong, then I can go either like "no way!" or "aaah silly of me".
Ok:
Quote:I did notice I forgot one thing. You have to substract half the screen width from the x position and half the screen height from the y position.
:)

In short, there was nothing in your description to account for any sort of viewport transform, without which you're unlikely to get the correct results. (You can account for the viewport in a more or less ad hoc fashion as you described, or use an additional 'viewport' transform along with the model, view, and projection transforms.)

This topic is closed to new replies.

Advertisement