0
\$\begingroup\$

I have written a program to record the x,y,z coordinated of the Hand joint and I want to animate my models 2D or 3D according to these coordinates. . .However the output of the x,y,z coordinates are fluctuating from -0 to 1 but not more than that..

So i assume I will need to multiply them back with the screen width and height, however it still doesnt seem to animate according to the original x,y,z points

Any transformations I might be missing out?

        protected override void Update(GameTime gameTime)
        {

            position += spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

            //// TODO: Add your update logic here
            using (StreamReader r = new StreamReader(f))
            {
                string line;
                Viewport view = graphics.GraphicsDevice.Viewport;
                int maxWidth = view.Width;
                int maxHeight = view.Height;

            //    int i = 0;
                while ((line = r.ReadLine()) != null)
                {


                    string[] temp = line.Split(',');

                int x = (int) Math.Floor((float.Parse(temp[0]) * 0.5f) + 0.5f) maxWidth);
              int y = (int) Math.Floor((float.Parse(temp[1]) * -0.5f) + 0.5f) * maxHeight);



                    motion.Add(new Point(x, y));
            //        Console.WriteLine(temp[0] + "," + temp[1] + "," + motion[i].X+ "," + motion[i].Y);
            //        i++;

                }

            }


//            foreach (Point s in motion)
            {

                position.X = motion[i].X;
                position.Y = motion[i].Y;

            }

  //


            i++;

            a_butterfly_up.Update(gameTime);

            a_butterfly_side.Update(gameTime);

            G_vidPlayer.Play(mossV);

            base.Update(gameTime);
        }
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

It may be 0 most of the time, as you cast to int before you multiply (btw. your brackets are somehow messed up anyway). Maybe try this code instead:

   int x = (int) (float.Parse(temp[0])* maxWidth);
   int y = (int) (float.Parse(temp[1]) * maxHeight);

Regards!

EDIT:

Your code looks a bit messed up: At the beginning you use position += spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; which make not really sense to me as you overwrite position later on. I am not able to test much on my current machine, so I will give you a pseudo code snippet of how I would address this problem:

int your objects add: Vector2D position; Vector2D target;

public void Update(GameTime gameTime)
{
     if position!= target
        position+=target-position*gameTime*speed;
}

In the main loop do the following:

protected override void Update(GameTime gameTime)
{
    Get the target location from your stream
    Set the target of the desired object to the retrieved point
    Update every object
    base.Update(gameTime);
}

This is full pseudo code but might give you a headstart to implement the movement.

Regards!

\$\endgroup\$
7
  • \$\begingroup\$ I had tried this before but its not correct! Yes i'm sorry for the brackets its supposed to be one less \$\endgroup\$ Commented Sep 21, 2012 at 11:39
  • \$\begingroup\$ Have you tried debugging the values you receive for x and y? Just to make sure the error happens here and not in the animation itself \$\endgroup\$
    – floAr
    Commented Sep 21, 2012 at 11:43
  • \$\begingroup\$ yes i have added a breakpoint and the values are fine. . .im guessing there is smth more i have to do before i can get the actually x,y,z values \$\endgroup\$ Commented Sep 21, 2012 at 11:53
  • \$\begingroup\$ Maybe add the animation logic to the topic. If your values are fine, then the error is clearly not rooted in the above snippet \$\endgroup\$
    – floAr
    Commented Sep 21, 2012 at 11:55
  • \$\begingroup\$ I have added the logic above. . just changed the x,y param \$\endgroup\$ Commented Sep 21, 2012 at 12:06
0
\$\begingroup\$

This seems to work well for now . .however I dont understand the Why need to multiply or add 0.5f to the maxWidth or maxheight. . .

    using (StreamReader r = new StreamReader(f))
    {
        string line;
        Viewport view = graphics.GraphicsDevice.Viewport;
        int maxWidth = view.Width;
        int maxHeight = view.Height;

        while ((line = r.ReadLine()) != null)
        {


            string[] temp = line.Split(',');

            int x = (int) Math.Floor(((float.Parse(temp[0]) * 0.5f) + 0.5f) * maxWidth);
            int y = (int) Math.Floor(((float.Parse(temp[1]) * -0.5f) + 0.5f) *maxHeight);


          motion.Add(new Point(x, y));

        }

    }

        position.X = motion_2[i].X;
        position.Y = motion_2[i].Y;
        i++;
\$\endgroup\$
10
  • \$\begingroup\$ I dont think you need to do this. It will stretch your movements in a wrong way. Tryo to multiply y with -1 and keep the 0.5 out of the calculation \$\endgroup\$
    – floAr
    Commented Sep 24, 2012 at 7:54
  • \$\begingroup\$ I'll try this .. but what about the Z values ? ? Do i need any transformations for that too \$\endgroup\$ Commented Sep 24, 2012 at 12:55
  • \$\begingroup\$ I just tried it out . . What happens is the if i remove the 0.5f . . the smiley (sprite) just goes out of the screen . . \$\endgroup\$ Commented Sep 24, 2012 at 13:15
  • \$\begingroup\$ int x = (int) Math.Floor(((float.Parse(temp[0])*1f) * maxWidth); int y = (int) Math.Floor(((float.Parse(temp[1]) * -1f) *maxHeight); this doesn´t work ? \$\endgroup\$
    – floAr
    Commented Sep 25, 2012 at 7:45
  • \$\begingroup\$ I'm sorry to reply after really long . . but 1f doesnt work apparent 0.5f is more accurate somehow . . which doesnt even make sense to me :-/ \$\endgroup\$ Commented Sep 28, 2012 at 21:05

You must log in to answer this question.

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