1
\$\begingroup\$

So i have written a program to play a sound file when ever my RightHand.Joint touches the 3D model . . It goes like this . . even though the code works somehow but not very accurate . . for example it will play the sound when my hand is slightly under my 3D object not exactly on my 3D object . How do i make it more accurate? here is the code . . (HandX & HandY is the values coming from the Skeleton data RightHand.Joint.X etc) and also this calculation doesnt work with Animated Sprites..which i need to do

foreach (_3DModel s in Solar)
{
    float x = (float)Math.Floor(((handX * 0.5f) + 0.5f) * (resolution.X));
    float y = (float)Math.Floor(((handY * -0.5f) + 0.5f) * (resolution.Y));
    float z = (float)Math.Floor((handZ) / 4 * 20000);



    if (Math.Sqrt(Math.Pow(x - s.modelPosition.X, 2) + Math.Pow(y - s.modelPosition.Y, 2)) < 15)
    {
        //Exit();
        PlaySound("hyperspace_activate");

        Console.WriteLine("1" + "handx:" + x + "," + " " + "modelPos.X:" + s.modelPosition.X + "," + " " + "handY:" + y + "modelPos.Y:" + s.modelPosition.Y);
    }

    else
    {
        Console.WriteLine("2" + "handx:" + x + "," + " " + "modelPos.X:" + s.modelPosition.X + "," + " " + "handY:" + y + "modelPos.Y:" + s.modelPosition.Y);
    }
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Instead of testing for distance to can test if it is contained. The Problem with distance is that it forms a circle of acceptance around you model (with radius=15 with the given code) so either way you can decrease the radius or switch to containing test.

For this change the if statement to something like this (Pseudo)

if(x>=s.model.PosX && x<=s.model.PosX+s.model.Width && y>=s.model.PosY && y<=s.model.PosY+s.model.Height) 

This will test with a rectangular shape. If you want a really 100% accurate result you will have to look into picking (This may get you started)

Hope this helps ;)

floAr

\$\endgroup\$
9
  • \$\begingroup\$ floAr .. Is this for playing sound using animated sprite because there is no 3D model width \$\endgroup\$ Commented Oct 1, 2012 at 11:53
  • \$\begingroup\$ You can check this threat to extract width and height of your model: social.msdn.microsoft.com/Forums/en-US/xnagamestudioexpress/… \$\endgroup\$
    – floAr
    Commented Oct 1, 2012 at 11:54
  • \$\begingroup\$ floatArr . . i got something working already . . i have to modify the calculation of x and y differently for 2D and 3D object :) \$\endgroup\$ Commented Oct 1, 2012 at 12:27
  • \$\begingroup\$ But i will read over the bounding box spheres . . Thank you ..you are always at rescue for my kinect troubles \$\endgroup\$ Commented Oct 1, 2012 at 12:32
  • \$\begingroup\$ I tried the code above..for clicking buttons.. like if my skeletal x,y is on the start button then game state change.. but it doesnt work :( \$\endgroup\$ Commented Oct 1, 2012 at 12:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .