[gameprogrammer] Math Problem

  • From: "Edilson Vasconcelos de Melo Junior" <dirso@xxxxxxxxxxxxxxxxxx>
  • To: <gameprogrammer@xxxxxxxxxxxxx>
  • Date: Fri, 18 Jul 2008 16:21:02 -0300

Hi!

 

I'm almost there with my array design to indicate the path. 

My solution was: reduce the problem to a 2d issue, then draw two small line
segments attached to the target point. It was not that hard, I just found
the dimension with less difference between pointA (source) and pointB
(target), ignore it and the rotate a normalized -(pointB - pointA) by PI/6
and -PI/6, so I was suppose to have the triangle faces, but I didn't, both
faces looks the same, and I think it's a sin/cos issue that I could solve.
Here is my code.

 

            public static Vector2 rotateVector(Vector2 v, double angle)

            {

                  double ca = Math.Cos(angle), sa = Math.Sin(angle);

                  return new Vector2((float)(v.X*ca + v.Y*sa),
(float)(v.X*(-sa) + v.Y*ca));

            }

 

 

            public void calcDwTriangle(int index, Vector3 p1, Vector3 p2)

            {

                  #region dwTriangles

                  int icode;

                  if(Math.Abs(p1.X - p2.X) < Math.Abs(p1.Y - p2.Y))

                  {

                        if(Math.Abs(p1.X - p2.X) < Math.Abs(p1.Z - p1.Z))

                             icode = 0;

                        else icode = 2;

                  }

                  else

                  {

                        if(Math.Abs(p1.Y - p2.Y) < Math.Abs(p1.Z - p2.Z))

                             icode = 1;

                        else icode = 2;

                  }

                  Vector2 pontoA, pontoB;

                  switch(icode)

                  {

                        case 0: // take off X

                             pontoA = new Vector2(p1.Y, p1.Z);

                             pontoB = new Vector2(p2.Y, p2.Z);

                             break;

                        case 1: // take off Y

                             pontoA = new Vector2(p1.X, p1.Z);

                             pontoB = new Vector2(p2.X, p2.Z);

                             break;

                        default: // case 2 // take off Z

                             pontoA = new Vector2(p1.X, p1.Y);

                             pontoB = new Vector2(p2.X, p2.Y);

                             break;

                  }

                  Vector2 dir = pontoB - pontoA;

                  float len = dir.Length();

                  float arrowHeadWidth = 10.0f;

                  float arrowHeadLength =
(float)Math.Sqrt(Math.Pow(arrowHeadWidth,2.0f) + Math.Pow(arrowHeadWidth *
0.5f, 2.0f)); 

                  dir.Normalize();

                  Vector2 midPoint = pontoB - scalarMultiply(dir,
arrowHeadLength); 

                  Vector2 newDir = -dir;

                  double a = Math.Atan2(dir.Y, dir.X);

                  Vector2 a1 = rotateVector(newDir, Math.PI/6),

                        a2 = rotateVector(newDir, -Math.PI/6);

 

                  a1.Normalize();

                  a2.Normalize();

                  a1 = scalarMultiply(a1, arrowHeadWidth * 0.5f); 

                  a2 = scalarMultiply(a2, arrowHeadWidth * 0.5f); 

                  a1 += midPoint;

                  a2 += midPoint;

 

                  dwTriangles[index][1].Position = p2;

                  switch(icode)

                  {

                        case 0: // tira o X

                             dwTriangles[index][0].Position = new
Vector3(p2.X, a1.X, a1.Y);

                             dwTriangles[index][2].Position = new
Vector3(p2.X, a2.X, a2.Y);

                             break;

                        case 1: // tira o Y

                             dwTriangles[index][0].Position = new
Vector3(a1.X, p2.Y, a1.Y);

                             dwTriangles[index][2].Position = new
Vector3(a2.X, p2.Y, a2.Y);

                             break;

                        default: // case 2 // tira o Z

                             dwTriangles[index][0].Position = new
Vector3(a1.X, a1.Y, p2.Z);

                             dwTriangles[index][2].Position = new
Vector3(a2.X, a2.Y, p2.Z);

                             break;

                  }

                  #endregion

            }

 

Does anyone know a workaround for that?

 

Thanks,

Dirso

Other related posts:

  • » [gameprogrammer] Math Problem