🔒 Locked
2D Mouse Selection
Advertisement
Original Post
How do I test to see if the mouse is within a triangle, no matter what angle the triangle is?
Google for "Inside half-space test" basically, you treat each edge as a vector, and use a dot product to determin if the point is on one side of this vector or the other. Walking your way around the triangle, if it lies on the same side of each edge, the point is inside the triangle.
Another thing to google for is "picking". The DX SDK has (or at least used to) have a picking sample in it.
I already know it's called picking. That's just another name for it. All the picking samples I came across though is 3D. I'm just after 2D in my case.
Quote:
Original post by ET3D
Picking in 2D and in 3D are exactly the same, except that 2D has one less dimension. In fact, 3D picking is done in 2D (by projecting the 3D to 2D). So you shouldn't have trouble using the 3D method.
Not quite, you actually form a 3D ray from the center of the 'camera' through the front of the viewing frustrum relative to the point clicked on the display. You could do the method you suggest, projecting the 3D scene to 2D, but you'd end up with a lot of extra calculations and logic to project the bounding information and handle multiple objects occupying the same (or overlapping) segments of the screen.
Quote:
Original post by Ravyne Quote:
Original post by ET3D
Picking in 2D and in 3D are exactly the same, except that 2D has one less dimension. In fact, 3D picking is done in 2D (by projecting the 3D to 2D). So you shouldn't have trouble using the 3D method.
Not quite, you actually form a 3D ray from the center of the 'camera' through the front of the viewing frustrum relative to the point clicked on the display.
And how do you think the pick ray is tested against the triangles in the scene? Typically the intersection of the ray and the triangle's plane is found, then the rest of the intersection testing continues in this 2d space.
You will also want to learn about Painter's Algorithm. While its not the best (fastest) it does give a simple way to detect per-pixel collision. Its a little hard to integrate into D3D, but it works great if you are rendering in Win32 (probably not).
When you render the scene, render each object in a unique (solid) color to another backbuffer. When you click on the screen, do a simple lookup into your backbuffer at that xy, and see what the color is. If the color is the background, you selected nothing. If the color is one of the unique colors that you rendered, well, you found the object.
Hope that helps. Its not the best option, but definately something worth considering.
When you render the scene, render each object in a unique (solid) color to another backbuffer. When you click on the screen, do a simple lookup into your backbuffer at that xy, and see what the color is. If the color is the background, you selected nothing. If the color is one of the unique colors that you rendered, well, you found the object.
Hope that helps. Its not the best option, but definately something worth considering.
Quote:
Original post by OrangyTang Quote:
Original post by Ravyne Quote:
Original post by ET3D
Picking in 2D and in 3D are exactly the same, except that 2D has one less dimension. In fact, 3D picking is done in 2D (by projecting the 3D to 2D). So you shouldn't have trouble using the 3D method.
Not quite, you actually form a 3D ray from the center of the 'camera' through the front of the viewing frustrum relative to the point clicked on the display.
And how do you think the pick ray is tested against the triangles in the scene? Typically the intersection of the ray and the triangle's plane is found, then the rest of the intersection testing continues in this 2d space.
fair enough, but the fact remains that the method still requires an initial ray-plane intersection calculation, as you stated, which must be calculated in 3 dimensions before you get back into two dimensions.
The post by ET3D to which I was replying sounded to me as if he was suggesting that the the scene simply be projected into 2 Dimensions (as it is to be presented to the display) and then simple 2-D intersection test would apply to the entire scene. Re-reading his post, I probably read to much into it. It was early.
Still having issues working out the maths with 2D polygon picking. Anyone got any source code to lead me in the right direction?
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement