Game Development Community

Math Vector 2d->3d problem

by Gary "ChunkyKs" Briggs · in Torque Game Engine · 04/27/2007 (2:53 pm) · 7 replies

I have a plane in 2d. Points are expressed on this point as (x,y)

I want points on this plane to appear in torque, so I need to convert the co-ordinates to 3d.

In torque, I have the point on this 3d plane that represents the origin on the 2d plane, expressed as (x0,y0,z0).
I also have a vector representing the normal to this 3d plane, expressed as (x1,y1,z1).

How do I map the one onto the other? It seems like this should be a simple thing, but I'm having a hard time wrapping my head around it.

Gary (-;

#1
04/27/2007 (3:27 pm)
Hmmm i sorta think you're talking about Project and UnProject.. taking 3d coordinates and converting them to 2d onscreen coordianates, and vice-versa. But it seems like you might be wanting something else...
#2
04/27/2007 (3:30 pm)
Dang! i wrote a whole reply but lost it.

in short, you don't have enough information about the plane to decide where to put the point.
this is because you don't actually want to describe a plane, but actually a plane with a coordinate system.

ie, a plane is rotationally symmetric about the normal.

if, however, you had two vectors in the plane which represented the X and Y vectors in the plane, then yuo're in business:
vPlaneX = 3D vector representing the X axis of the plane.
                     vPlaneY = 3D vector representing the Y axis of the plane.
                     planeO  = 3D point  representing the origin of the plane.
                     
                     
                     point2D = your 2D point.
                     
                     point3D = (point2D.x * vPlaneX) + (point2D.y * vPlaneY).
                     // the above is the sum of two scalar-times-vector mutliplications.

note that if you only have say vPlaneX, you can get vPlaneY by vPlaneY = vPlaneX CROSS vPlaneNormal.

hope that helps.
#3
04/27/2007 (3:40 pm)
.. if you really, seriously, don't have anything but the plane origin and the normal,
you can get vPlaneX by something like:
if (abs(vPlaneNormal DOT [1, 0, 0]) < abs(vPlaneNormal DOT [0, 1, 0]))
      vPlaneX = vPlaneNormal DOT [1, 0, 0].
   else
      vPlaneX = vPlaneNormal DOT [0, 1, 0].

but then as the plane takes different orientations,
the coordinate system will shift.
#4
04/27/2007 (3:54 pm)
Oh, hm.

*ponders*

Ah. "Up" in torque [positive Z] will always be negative Y on my 2d plane. If the 3d plane is vertical so that Y [in the torque world] always equals zero, then the vector (1,-1) in the original 2d plane will translate to the vector (1,0,1) in the 3d plane

Does that help? Or did I leave off something else obvious?

Thanks for the help!
Gary (-;
#5
04/27/2007 (4:11 pm)
Quote:Ah. "Up" in torque [positive Z] will always be negative Y on my 2d plane. If the 3d plane is vertical so that Y [in the torque world] always equals zero, then the vector (1,-1) in the original 2d plane will translate to the vector (1,0,1) in the 3d plane

O_o Now I'm confused. I was going to suggest this:

Point3F planeOrigin(x0, y0, z0);
VectorF planeNormal(x1, y1, z1);
VectorF planeX, planeY;

// If the plane normal is straight up...
if (planeNormal.equal(VectorF(0, 0, 1))) {
   // Then X and Y can be defined simply as (1, 0, 0) and (0, 1, 0) respectively. 
   planeX.set(1, 0, 0);
   planeY.set(0, 1, 0);

} else {
   // We have a bit more work to do. I'd start with an X as a "flat" vector
   // perpendicular to "up" and to the plane normal. 
   mCross(VectorF(0, 0, 1), planeNormal, &planeX);
   planeX.normalize();
   // Now Y is a cross between the normal and X
   mCross(planeNormal, planeX, &planeY);
}

// generate a transform matrix
MatrixF planeTransform;
planeTransform.setColumn(0, planeX);
planeTransform.setColumn(1, planeY);
planeTransform.setColumn(2, planeNormal); // doesn't really matter
planeTransform.setColumn(3, planeOrigin);

// Now transforming your 2d points to 3d world space is simple
Point3F aPoint(x, y, 0);
planeTransform.mulP(aPoint);

... but that's not going to map negative 2D Y to positive 3D Z... :-/
#6
04/27/2007 (4:42 pm)
Quote:
Ah. "Up" in torque [positive Z] will always be negative Y on my 2d plane. If the 3d plane is vertical so that Y [in the torque world] always equals zero, then the vector (1,-1) in the original 2d plane will translate to the vector (1,0,1) in the 3d plane

ah!
well then it's easy!

point3D.x  = point2D.x.
point3D.y  = 0.
point3D.z  = point2D.y * -1.
point3D   *= scale factor. // just thought you might want to add this in.
point3D   += planeOrigin.
#7
04/27/2007 (4:56 pm)
Oooo cool. Thank-you very much!

Gary (-;