Jump to content
  • Advertisement
Sign in to follow this  
imsomniac

detect mouse over 3d model

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am programming wheel of fortune.You know that user select letter to solve puzzle.My problem is to detect which letter is clicked.I generate Ray with mouse corrdinates.Then for 26 letters,I tried to detect collision using BoundingSphere. this is the piece of code.Please help. private bool mouseOverModel(MouseState mouseState) { Vector3 nearScreenPoint = new Vector3(mouseState.X, mouseState.Y, 0); Vector3 farScreenPoint = new Vector3(mouseState.X, mouseState.Y, 1); Vector3 near3DWorldPoint = graphics.GraphicsDevice.Viewport.Unproject(nearScreenPoint, cameraProjectionMatrix, cameraViewMatrix, Matrix.Identity); Vector3 far3DWorldPoint = graphics.GraphicsDevice.Viewport.Unproject(farScreenPoint, cameraProjectionMatrix, cameraViewMatrix, Matrix.Identity); Vector3 pointerRayDirection = far3DWorldPoint - near3DWorldPoint; pointerRayDirection.Normalize(); Ray pointerRay = new Ray(near3DWorldPoint, pointerRayDirection); for(int i=0;i<alphabet.Length;i++) { BoundingSphere collision = new BoundingSphere(); collision.Radius = alphabet.scale; collision.Center = alphabet.position; Nullable<float> dist = pointerRay.Intersects(collision); if (!dist.HasValue) { return false; } if (dist.Value <= pointerRayDirection.Length())//check whether it is the closest object intersected so far. { selectedLetterIndex = i; alphabet.isclicked = true; Console.WriteLine(selectedLetterIndex); return true; } } return false; }

Share this post


Link to post
Share on other sites
Advertisement
What exactly is your problem? For letters it would probably be better to use a bounding box the size of your characters. Spheres may intersect with eachother.

Share this post


Link to post
Share on other sites
you are right.I use BoundingSphere.I don't know how to use BoundingBox.I want to find which letter is clicked.Can you see any fault in my code.

Share this post


Link to post
Share on other sites
if (dist.Value <= pointerRayDirection.Length())//check whether it is the closest object intersected so far. 

Nope. The only thing you are checking with this test is whether the intersection point is closer to the viewer than the length of the ray. Since you normalized the ray this distance is 1.0 in world space. Do not confuse this with the depth ranging from 0.0 to 1.0 of the 2d points you unproject.

You have to store the first dist that has a value and compare all following valid distances against that one distance. Each time you have a closer distance then store that one. Assuming that all of your letters are next to each other it is valid to return true upon the first hit because it will then be the only one possible:


float best_hit = MAX_FLOAT;

for(int i=0;i<alphabet.Length;i++)
{

BoundingSphere collision = new BoundingSphere();
collision.Radius = alphabet.scale;
collision.Center = alphabet.position;
Nullable<float> dist = pointerRay.Intersects(collision);
if (dist.HasValue && dist.Value <= best_hit)//check whether it is the closest object intersected so far.
{
selectedLetterIndex = i;
alphabet.isclicked = true;
Console.WriteLine(selectedLetterIndex);
best_hit = dist.Value; // if you do not want to return upon first hit this is required
return true;
}

Share this post


Link to post
Share on other sites
Waterwalker your answer is logical.It can work.But I could't understand what can I wrote for MAX_FLOAT value.Can you help.

Share this post


Link to post
Share on other sites
Um ... sorry that is actually FLOAT_MAX as defined in <limits> that you need to include. This represents infinity in this case and stands for "I have not found any intersection yet".

Share this post


Link to post
Share on other sites
I don't want to seem lazy.But I am searching for FLOAT_MAX in XNA and <limits>.But I couldn't related things.Can these library can be c++ or anything else.I couldnt find in XNA.

Share this post


Link to post
Share on other sites
Assuming you're using C# you can use Single.MaxValue to get the maximum value a float can hold. If not using C#, please mention what language you are using.

Share this post


Link to post
Share on other sites
How do you render the letters?

If they are textured quads then it should be strait forward: as you print the letters just store coordinates for each quad in a array and then use that array to search for the quad mouse cursor is inside (in screen space).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!