Jump to content
  • Advertisement
Sign in to follow this  
ProblemBaby

Projecting a polygon onto 2D plane

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

Hello

I know that similar questions has been answered earlier but you can't seem to get it right.

if have a plane at (0,0,-1) with a normal (0,0,1) and try to project two points
(-0.1f, +0.1f, -1.0f ), (+0.1f, -0.1f, -1.0f ) I would expect them to come out the same or maybe am I missing something? anyway I get (which can't be correct):

-0.14,0.7,-1
+0.14,0.7,-1


Here is my code for testing...





struct VECTOR
{
float x, y, z;
};


VECTOR VecSub(VECTOR a, VECTOR b)
{
VECTOR v;

v.x = a.x - b.x;
v.y = a.y - b.y;
v.z = a.z - b.z;
return v;
}

VECTOR VecCross(VECTOR a, VECTOR b)
{
VECTOR v;

v.x=a.y*b.z-a.z*b.y;
v.y=a.z*b.x-a.x*b.z;
v.z=a.x*b.y-a.y*b.x;
return v;
}

VECTOR VecNormalize(VECTOR a)
{
float l = sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);

a.x /= l;
a.y /= l;
a.z /= l;


return a;
}


int main()
{
VECTOR p1 = { -0.1f, +0.1f, -1.0f };
VECTOR p2 = { +0.1f, -0.1f, -1.0f };
VECTOR n = { 0, 0, 1 };
VECTOR u, v;

VECTOR p1d, p2d;

u = VecSub(p2, p1);




v = VecCross(u, n);

u = VecNormalize(u);
v = VecNormalize(v);

printf("%0.5f %0.5f %0.5f\n", u.x, u.y, u.z);
printf("%0.5f %0.5f %0.5f\n", v.x, v.y, v.z);
printf("%0.5f %0.5f %0.5f\n", n.x, n.y, n.z);

p1d.x = p1.x * u.x + p1.y * u.y + p1.z * u.z;
p1d.y = p1.x * v.x + p1.y * v.y + p1.z * v.y;
p1d.z = p1.x * n.x + p1.y * n.y + p1.z * n.z;

p2d.x = p2.x * u.x + p2.y * u.y + p2.z * u.z;
p2d.y = p2.x * v.x + p2.y * v.y + p2.z * v.y;
p2d.z = p2.x * n.x + p2.y * n.y + p2.z * n.z;

printf("\n");

printf("%0.5f, %0.5f %0.5f\n", p1d.x, p1d.y, p1d.z);
printf("%0.5f, %0.5f %0.5f\n", p2d.x, p2d.y, p2d.z);



return 0;
}



Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
Corrections in underlined bold:

p1d.x = p1.x * u.x + p1.y * u.y + p1.z * u.z;
p1d.y = p1.x * v.x + p1.y * v.y + p1.z * v.z;
p1d.z = p1.x * n.x + p1.y * n.y + p1.z * n.z;

p2d.x = p2.x * u.x + p2.y * u.y + p2.z * u.z;
p2d.y = p2.x * v.x + p2.y * v.y + p2.z * v.z;
p2d.z = p2.x * n.x + p2.y * n.y + p2.z * n.z;


You should also have a dot product function to make this operation easier in the future:
VECTOR VecDot(VECTOR a, VECTOR b)
{
VECTOR v;

v.x=a.x*b.x;
v.y=a.y*b.y;
v.z=a.z*b.z;
return v;
}
However at the end of the day, you might be better off using an establised math library instead of writing your own, since mistakes like this can happen often and are difficult to track down. Plus unless you're writing a vector class for learning purposes, this wheel has been invented hundreds of times before :)

Regarding the transformed coordinates, they shouldn't turn out the same, since you're rotating the frame about the Z-axis by 45°. However the y-value should be 0 for both transformed coordinates, which it is with this correction.

Share this post


Link to post
Share on other sites
Thanks for the reply and the correction.


Since you told me that the values i thought was strange is correct I start to think that I don't what I thought i was doing=)

I thought it shouldn't rotate at all in this case? since its already looking down the Z-axis?

I want to be rotate a plane facing in any direction and have it facing towards positive Z.
How can I accomplish that?

Thanks

Share this post


Link to post
Share on other sites
How about planeNormal(0,0,1).

Where planeNormal is an unit length vector assigned by x,y,z values 0,0,1 making it aligned with world axis Z.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zipster


You should also have a dot product function to make this operation easier in the future:
VECTOR VecDot(VECTOR a, VECTOR b)
{
VECTOR v;

v.x=a.x*b.x;
v.y=a.y*b.y;
v.z=a.z*b.z;
return v;
}


Sorry, doesn't dot product return a scalar? as in A.B = A1*B1 + A2*B2 + An*Bn ? Just wondering if there's something here I'm not aware of.

Share this post


Link to post
Share on other sites
Quote:
Sorry, doesn't dot product return a scalar? as in A.B = A1*B1 + A2*B2 + An*Bn ?
Yes, that's right. (The code you're asking about might be called a member-wise product, or perhaps a modulation. I've never seen the term 'dot product' used to refer to a member-wise product though.)

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
Sorry, doesn't dot product return a scalar? as in A.B = A1*B1 + A2*B2 + An*Bn ?
Yes, that's right. (The code you're asking about might be called a member-wise product, or perhaps a modulation. I've never seen the term 'dot product' used to refer to a member-wise product though.)

Sorry, I wrote a member-wise product for some unknown reason while I was thinking dot product... oh well, just return v.x+v.y+v.z [wink]

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!