Troubleshooting this TutorialSometimes, even though you try hard to understand the information in a tutorial, things don't work out quite like you want it to. This page is here to help you resolve any problems you might be having with "picking" with the mouse. The Common Mistakes section describes common problems that people have when doing the things in this tutorial, and how to resolve them. The Frequently Asked Questions section describes questions that people have that aren't related to mistakes, but rather, trying to understand the stuff better or exploring how it can be used. If your problem or concern isn't addressed here, feel free to add a comment below, so that I know where you're having trouble. I like to keep these pages fairly clean, so I may remove comments that I felt like have been addressed. If I remove your comment and you don't feel like the problem has been fixed, repost the question and we'll take another look at it. If a tutorial has a mistake in it, I will fix the mistake and reply to the comment with a brief explanation. However, after a couple of weeks I'll likely go back and remove the original comment as well as my reply, because, hopefully, the problem will have been fixed, and it won't be a concern any more. |
Common MistakesNone listed yet… |
Frequently Asked QuestionsNone listed yet… |
Hi! I was wondering if the same method using Rays can be implemented in a 2D world aswell. If not, how would you recommend detecting intersections between the mouse and 2D textures?
By the way, I really appreciate your work and the effort you put into this. Thank you.
I also happend to notice that there are some parts when the program states that the mouse is over the small ship when it isn't. And I realized that is because we are using a sphere bounding box. So I was wondering if theres a way to create a bounding box shaped the way I want it?
Yeah, there is a way, though the math or algorithm involved starts to get a lot more complicated. I really need to do more physics/math/collision detection tutorials…
Let me summarize collision detection in a nutshell. This won't give you all the details you need, but might give you some ideas.
There are two broad categories/ways of doing it. One is to basically render the image in some form or fashion, then compare the pixel at the mouse's location against what it ought to be and make a decision based on that. Usually, using this approach, you don't draw the normal 3D model with the shading and everything, or the original picture. Instead, you might draw the object all in a single color, and have every unique object use a different color. Then you don't draw to the screen, but to a render target so that you can poll the rendered results for the color at a specific pixel coordinate.
One of the catches of this approach is that graphics cards are optimized for sending data to it, and not the reverse. So any time you want to pull data off of the graphics card to do check the pixel color at the mouse location, you're going to take a performance hit. (Or you'll have to do intersection/collision detection on the graphics card, which is a whole other set of problems.)
The reality is, sometimes this approach is useful, but most people do it a different way.
This second approach uses math, like with this picking tutorial. In most cases, we use an approximation of the shape of the underlying object. Usually, these 3D models (or 2D pictures) are complicated enough that going with a fully 100% accurate collision mesh is a big performance hit. So usually, a simplification is used. A sphere, like in this case, is about as simple of an approximation as they come. The math is a piece of cake. But obviously, you can get quite a bit more complicated, both in 2D and in 3D. If you go hunt down my physics tutorials, I've got a couple of other samples that might get you going in this direction.
To answer your original question about doing picking in 2D, you don't actually have to follow the whole projected ray thing. It's simpler than that. You just use the Vector2 that is the mouse's position on the screen. You don't need a ray.
I know that doesn't give you everything you need to move forward, but it's hopefully a start, and something to think about.
Post preview:
Close preview