• Advertisement
Sign in to follow this  

please check my converted transformation from 2d

This topic is 3321 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

Hi This is from the steering behavior code that I transformed to 3d. Can you guys check if i have done it right? Because I'm not getting the correct behavior. THere's 2 function, here is the first:
inline Vector2D PointToLocalSpace(const Vector2D &point,
                             Vector2D &AgentHeading,
                             Vector2D &AgentSide,
                              Vector2D &AgentPosition)
{

	//make a copy of the point
  Vector2D TransPoint = point;
  
  //create a transformation matrix
	C2DMatrix matTransform;

  double Tx = -AgentPosition.Dot(AgentHeading);
  double Ty = -AgentPosition.Dot(AgentSide);

  //create the transformation matrix
  matTransform._11(AgentHeading.x); matTransform._12(AgentSide.x);
  matTransform._21(AgentHeading.y); matTransform._22(AgentSide.y);
  matTransform._31(Tx);           matTransform._32(Ty);
	
  //now transform the vertices
  matTransform.TransformVector2Ds(TransPoint);

  return TransPoint;
}

And here's my 3d version:
        Vector3 PointToLocalSpace(Vector3 point, Vector3 forward, Vector3 right, Vector3 position)
        {
            //make a copy of the point
            Vector3 TransPoint = point;

            //create a transformation matrix
            Matrix mat = Matrix.Identity;

            float Tx = Vector3.Dot(position, forward);
            float Ty = Vector3.Dot(position, right);

            //create the transformation matrix
            mat.M11 = right.X;
            mat.M12 = right.Y;
            mat.M13 = right.Z;

            mat.M21 = 0.0f;
            mat.M22 = 1.0f;
            mat.M23 = 0.0f;

            mat.M31 = forward.X;
            mat.M32 = forward.Y;
            mat.M33 = forward.Z;

            mat.M41 = Tx; mat.M43 = Ty;
            mat.M42 = 0.0f;

            //now transform the vertices
            TransPoint = Vector3.Transform(TransPoint, mat);

            return TransPoint;
        }

Here's the other one.
//--------------------- VectorToWorldSpace --------------------------------
//
//  Transforms a vector from the agent's local space into world space
//------------------------------------------------------------------------
inline Vector2D VectorToWorldSpace(const Vector2D &vec,
                                     const Vector2D &AgentHeading,
                                     const Vector2D &AgentSide)
{
	//make a copy of the point
  Vector2D TransVec = vec;
  
  //create a transformation matrix
	C2DMatrix matTransform;

	//rotate
	matTransform.Rotate(AgentHeading, AgentSide);

  //now transform the vertices
  matTransform.TransformVector2Ds(TransVec);

  return TransVec;
}

Here's mine
        Vector3 VectorToWorldSpace(Vector3 vec, Vector3 forward)
        {
            //make a copy of the point
            Vector3 TransVec = vec;

            Matrix matRot = Matrix.Identity;

            if (forward == Vector3.Zero)
            {
                forward.X = 1.0f;
            }
            matRot = Matrix.CreateLookAt(Vector3.Zero, forward, Vector3.Up);
            matRot = Matrix.Transpose(matRot);


            //now transform the vertices
            TransVec = Vector3.Transform(TransVec, matRot);

            return TransVec;
        }

thanks so much for your time. [Edited by - mickeyren on January 16, 2009 8:39:21 PM]

Share this post


Link to post
Share on other sites
Advertisement
Perhaps these functions does the same thing:

// Convert from point in local to world
Vector3 newTarget = Vector3.Transform(wanderTarget, entity.matWorld);

// Convert from point in world to local
Matrix invert = Matrix.Invert(entity.matWorld);
Vector3 LocalPos = Vector3.Transform(obstacles.sphere.Center, invert);

// Convert vector in local to world space
Matrix invertOrient = Matrix.Invert(entity.matRot);
invertOrient = Matrix.Transpose(invertOrient );
SeeringForce = Vector3.Transform(SteeringForce, invertOrient );


Should those be correct?

Share this post


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

  • Advertisement