Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);
    }
}
share|improve this question

1 Answer 1

up vote 2 down vote accepted

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

share|improve this answer
    
floAr .. Is this for playing sound using animated sprite because there is no 3D model width –  Sweta Dwivedi Oct 1 '12 at 11:53
    
You can check this threat to extract width and height of your model: social.msdn.microsoft.com/Forums/en-US/xnagamestudioexpress/… –  floAr Oct 1 '12 at 11:54
    
floatArr . . i got something working already . . i have to modify the calculation of x and y differently for 2D and 3D object :) –  Sweta Dwivedi Oct 1 '12 at 12:27
    
But i will read over the bounding box spheres . . Thank you ..you are always at rescue for my kinect troubles –  Sweta Dwivedi Oct 1 '12 at 12:32
    
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 :( –  Sweta Dwivedi Oct 1 '12 at 12:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.