point in polygon testing is the way to go and gives you a perfect result. it goes like this given that you only want to intersect convex polys:
you have your list of vertices ( points ) of the rectangle in 2d
Code:
p1---------p2
^ |
| |
| |
p4<---------- p3
as you can see the points have to be given in clockwise or counterclockwise order. if you store them in an array you can't have points(0) being p1 and points(2) being p2 for example.
you now can calculate the line segments between adjacend points that is the segment p1 to p2, p2 to p3, p3 to p4 and p4 to p1. if you have a look at the ascii art obove you see they form a circle. now let's take the segment p2 to p3
i also drew a point p here which is the point in question. i also created a new segment from p2 to p. we now have to vectors. with our the following friendly formula we now can check on which side of the segment p2 to p3 point p lies.
(p.y - p2.y)*( p3.x * p2.x) - ( p.x - p2.y )*(p3.y * p2.y )
looks like a dirty hybrid of a dot and crossproduct doesn't it? i won't derive that formula here. basically it gives you this:
+ if the result is < 0 then the point is on the right site of the linesegment
+ if the result > 0 then the point is on the left site of the line segment
+ if the result = 0 then the point is on the line segment
now have a look at the first figure again. the line segments are arranged in a circular order. all you have to do is to calculate the value of the above formula for all line segments for the point in question and check wheter the sign of them is the same for every result which boils down to this pseudo code.
Code:
dim points( 0 to NUM_POINTS ) as Point
dim p as Point
dim is_in_poly as integer
dim side as single
enum Side
LEFT_SIDE
RIGHT_SIDE
ON_LINE
end enum
is_in_poly = -1
for i = 0 to NUM_POINTS
if( i = 0 ) then
tmp = ( p.y - points(i).y )*( points(i+1).x * points(i).x) - ( p.x - points(i).x ) * ( points(i+1).y -points(i )
if( tmp < 0 ) then site = RIGHT_SIDE
if( tmp > 0 ) then site = LEFT_SIDE
if( tmp = 0 ) then is_in_poly = -1: exit for
else if i = NUM_POINTS then
tmp = ( p.y - points(i).y )*( points(0).x - points(i).x) - ( p.x - points(i).x ) * ( points(0).y -points(i )
if( tmp < 0 ) then tmp = RIGHT_SIDE
if( tmp > 0 ) then tmp = LEFT_SIDE
if( tmp = 0 ) then is_in_poly = -1: exit for
if( tmp <> side ) then is_in_poly = 0: exit for
else
tmp = ( p.y - points(i).y )*( points(i+1).x * points(i).x) - ( p.x - points(i).x ) * ( points(i+1).y -points(i )
if( tmp < 0 ) then tmp = RIGHT_SIDE
if( tmp > 0 ) then tmp = LEFT_SIDE
if( tmp = 0 ) then is_in_poly = -1: exit for
if( tmp <> side ) then is_in_poly = 0: exit for
end if
next i
if is_in_poly then
print "point is in poly
else
print "point is not in poly"
end if
this code might or might not work and can be written by far smaller. but it's more clear that way. so this will tell you wheter a point lies within an n-sided convex polygon. to collide one poly with another you only have to check wheter one of the second polygons points lies in the other polygon and you have perfect pixel collision.
hope that works.