It is currently Wed Jun 28, 2017 10:55 pm

All times are UTC - 5 hours




 Page 1 of 1 [ 3 posts ] 
Author Message
 Post subject: Tricky Subject: Cross Product of Two Vectors That Are 2D!
PostPosted: Sun Oct 30, 2011 6:29 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
Ok I added two functions to the Vector2 class from the game engine. I am trying to take the cross product of vector A with vector B in 2D. After doing the research many said that it can not be done in 2D, but I found this and it makes sense. A<i,j> x B<i,j> = signed Area of Parallelogram created by A & B. Even though this is area, it can have a negative value, we will get to this in a minute. If we compare to 3D vector cross product we know that AxB yeilds a vector C that is orthogonal to both A & B and that the magnitude of C is defined as the Area of the parallelogram of A & B. We see similarities here. The cross in 2D yeilds the same area as the magnitude of C in 3D. The sign represents which direction. I will show the functions below. One function is overloaded which is where I have a question about it.

// -----------------------------------------------------------------------
   // Cross2Area()
   // Return The Cross Product Of Two Vectors In 2D This Represents The
   // Signed Area  Of The Parellelogram Formed By The Extension Of One
   // Vector By Another
   inline float Vector2::Cross2Area( Vector2 v2 ) {

      return ( _fX*v2._fY - _fY*v2._fX );

   } // Cross

   // -----------------------------------------------------------------------
   // Cross2Vector3()
   // This Function Returns A 3D Vector Passing In A Value Where This
   // Value Represents The Direction And Magnatiude Of The Z Component
   // The X And Y Values Always Equal 0
   inline Vector3 Vector2::Cross2Vector3( float fValue ) {

      return Vector3( 0.0f, 0.0f, fValue );

   } // Cross2Vector3

   // -----------------------------------------------------------------------
   // Cross2Vector3()
   // This Function Returns A 3D Vector Passing In 2 Vector2 And Returning
   // A Resultant Vector3
   inline Vector3 Vector2::Cross2Vector3( Vector2 v2A, Vector2 v2B ) {
      
      Vector3 v3Result;
      v3Result._fX = 0.0f;
      v3Result._fY = 0.0f;
      v3Result._fZ = v2A.Cross2Area( v2B );

      return v3Result;

   } // Cross2Vector3


The first function Cross2Area takes in a vector 2 does the cross product on the vector2 you are working with and returns a float. The second function Cross2Vector3 takes in a float and just simply sets that to the z-component and returns a Vector3. The 3rd function Cross2Vector3 takes in two Vector2 sets the X,Y components to the resultant Vector3 to 0 and uses the first function on the two Vector2 passed in to find the direction and magnitude of Z. The question is which of these two overloaded functions is the better choice to use? Or does it depend on the given vs. unknown in that particular problem. Secondly I would apprieate it if someone could verify if the work is correct and these functions work right or are implemented correctly, or show me a more efficient way to implement them thank you. I will be using these extensively in the project im working on.

Would the second overload function be equivalant to doing this.
// -----------------------------------------------------------------------
   // Cross2Vector3()
   // This Function Returns A 3D Vector Passing In 2 Vector2 And Returning
   // A Resultant Vector3
   inline Vector3 Vector2::Cross2Vector3( Vector2 v2A, Vector2 v2B ) {
      
      Vector3 v3A, v3B, v3Result;

      v3A._fX = v2A._fX; v3A._fY = v2A._fY;
      v3B._fX = v2B._fX; v3A._fY = v2B._fY;

      v3Result = v3A.Cross( v3B );

      return v3Result;

   } // Cross2Vector3


Offline
 Profile  
 
 Post subject: Re: Tricky Subject: Cross Product of Two Vectors That Are 2D
PostPosted: Sun Oct 30, 2011 6:39 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
It is always best to reuse code that you already have so that you don't end up with code that does the same thing but is located in multiple places.

If you need this 2D cross product function I'd do the calculation all with the following one line of code:

inline Vector3 Vector2::Cross2Vector3( Vector2 v2A, Vector2 v2B ) {
   return Vector3( v2A._fX, v2A._fY, 0).Cross( Vector3( v2B._fX, v2B._fY, 0 ) );     
}// Cross2Vector3


Offline
 Profile  
 
 Post subject: Re: Tricky Subject: Cross Product of Two Vectors That Are 2D
PostPosted: Sun Oct 30, 2011 9:05 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
Ahh so, im guessing my implementations where right, but yours is a simplification of the last func I showed. Thanks Marek :)!


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron