Jump to content
  • Advertisement
Sign in to follow this  
foxylady556655

2D front facing question

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

Okay, so I'm really really awful at math... I'm working on a little 2d project where I have quads and a light source and I'm trying to determine which of the quad's line segments are facing the light source. I'm afraid that I wouldn't understand if someone explained it to me but if anyone could provide me with sample code I'd really greatly appreciate it, thanks.

Share this post


Link to post
Share on other sites
Advertisement
Are the 4 Quad Points always defined in a Clockwise/Counter Clockwise manner?

If the answer is yes, something like this should work: (Note, I haven't tested it )


// P[4] being your Edges
// LightPos being your Light

Vector2D Edge[4];
Vector2D Normal[4];
Vector2D LightDir;
float DotResult;

Edge[0] = P[1]-P[0];
Edge[1] = P[2]-P[1];
Edge[2] = P[3]-P[2];
Edge[3] = P[0]-P[3];


for ( int k = 0; k<4; k++ )
{
Normal[k] = Vector2D( Edge[k].y, -Edge[k].x); // Depending on Counter Clockwise or Clockwise you might need to invert this
LightDir = LightPos - P[k];
DotResult = Normal[k].x * LightDir.x + Normal[k].y * LightDir.y; // Or use a DotProduct function if you have one

if ( DotResult > 0.0f )
{
.... Edge is facing your Light Do that whatever you want here
}
else
{
... Edge is not facing the Light
}
}


This will also work if your Quads aren't defined Clockwise or Counter Clockwise. You must simply determine if the Vector Normal is pointing inwards or Outwards.

By normalizing the Normal and LightDir you can even determin the Angle (arccos the DotResult)

Edit: Fixed an Error. Edited by Kayzaks

Share this post


Link to post
Share on other sites
Thanks a lot, that did the trick. One more question though....

Is there a test to determine whether or not a point lies on (or within?) a given line segment? Or a routine for determining whether a line segment lies within another line segment (though I guess you could determine the latter from the former).

Thanks.

Share this post


Link to post
Share on other sites
Yes, both are possible.

Line-Point Intersection is a bit Inaccurate though, you'll see why in a second. I wrote this one of the top of my head, so it's definatly not the fastest and there are
probably better methods out there.


// L1, L2 are your Start and Ending Points of the Line
// Point being your Point you're testing

Vector3D Dir, PVec;
float TestX, TestY;

if ( Point.x != L1.x && Point.y != L1.y )
{
Dir = L2 - L1;
PVec = Point - L1;

TestX = Dir.x / PVec.x;
TestY = Dir.y / PVec.y;

if ( TextX >= 1.0f && fabs( TestX - TestY ) < 0.01f ) // If you use == 0.0f, this might not work everytime due to Floating Point inaccuracy.
{ // Depending on your World Scale, you might need to adjust 0.01f as well
// Point is on the Line
}
else
{
// Point is not on the Line
}
}
else
{
// Point is the same as the Starting Point, so it's on the Line
}


Now for Line Line Intersection. I took the following from this website: http://local.wasp.uw...try/lineline2d/

Should be self explanatory. Here is a short breakdown:

Line 1 is made up of Point 1 (P1 with X1,Y1 as Coordinates) and Point 2 (P2 with X2 and Y2)
Line 2 the same just with P3 and P4.

Step 1
Calculate the Following:

lineline2.gif

Step 2
Check and see if ua AND ub are both bigger than 0 and smaller than 1 ( ua >= 0.0f && ub >= 0.0f && ua <= 1.0f && ub <= 1.0f )

If yes = The Lines Intersect!
If no = They don't

Optional Step 3
Calculate The point where they meet:

x = x1 + ua(x2 - x1)
y = y1 + ua(y2 - y1)

Finish!

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!