#### Archived

This topic is now archived and is closed to further replies.

# Mouse input in 3D

This topic is 5271 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m just wondering in general how mouse clicks would be taken.. Lets say that your looking at a wall thats at a small angle to you.. and there are a bunch of buttons on that wall.. so you want your mouse cursor to be able to click on the screen where the buttons are How do you convert screen co-ords to where that is on the wall? I''m guessing 1 way would be to draw a line straight out from the point that was clicked to the point where it hits the wall.. then you can take that point to find what button If you wondering I''m thinking of making a 3D display for my current project which you can see in my sig.. so basically Imagine that the blocks are at a 30 degree angle from the screen and thats what this question is about.. how would I find what block they are trying to click.. Please visit Turt99 Productions

##### Share on other sites
This must be something that needs explaining alot I''m surprized that no one has a cut and paste answer to this one.. Maybe someone could point me to a tutorial that covers something like that

##### Share on other sites
If you are using OpenGL try looking for a tutorial on picking.

##### Share on other sites
Directx also have an example on triangle picking.

##### Share on other sites
The way I''ve done it is somewhat unortodox, but it works for my RTS at least. =)

I simply draw a mousepicture in 3d space (Depth testing disabled), and translating it according to the mousemovements... and use some "fancy" math to make sure it''s smooth all over the place...

for clicking, I check what the original MouseX,Y is when the 3Dcursor is where I wanna click. =)

"Game Maker For Life, probably never professional thou." =)

##### Share on other sites
I think the best way is as you said, draw a line from the view point along the cursor and find out where it intersects with that wall. What is going to realy mess with your brain is finding that intersection in O(1) time. Sure you could run a for loop taking realy small steps along that line untill the distances from the plane start getting larger instead of smaller, but that will slow your game down and no body wants that.

So here is some psudo code to find where in 3 space your line intersecs that wall:

// First what you will need to calculate this:

// The distance from the view point to any point on the wall
// use a corner since you already know where thay are
FLOAT X, Y, Z

// The unit vector of the line you are using
// (these are lower case)
FLOAT x, y, z

// The Unit Normal Vector of the plane you are trying to hit
FLOAT P, Q, R

// The Location of your POV (Point Of View)
FLOAT POV_X, POV_Y, POV_Z

// Now we begin the calculations
// I have reduced a lot of redundant opperations out of the
// formulas I used to get this

TRAVEL_DIST = ( P*X + Q*Y + R*Z ) / ( P*x + Q*y + R*z )

// multiply your lines unit vector by the travel distance
// and add that to the POV''s position to get a point that is
// both in your line and in the plane.

X_INTERSECT = x*TRAVEL_DIST + POV_X
Y_INTERSECT = Y*TRAVEL_DIST + POV_Y
Z_INTERSECT = Z*TRAVEL_DIST + POV_Z

I copied this out of an old test I took, but it should work. Good luck and happy programming.

##### Share on other sites
The way I did it was to transform every 3D click point to screen co-ords and pick the closest one to the mouse pointer. My math isn''t that strong so I found it to be the simplest solution.

Mark