# Point inclusion test (collision detection)

This topic is 3993 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Having a bit more trouble here. If I have a rectangle and I rotate it using glRotatef say 45 degrees using the center point, how can I then check if a point lies within the newly rotated shape?

##### Share on other sites
a rectangle or a cube?

if it is a cube,
you could use a AABB for checking it, or you can check to see if the point is in front of all of the planes that makes the cube....

if you only want to check a point and a poly intersection,
you should first see if that point intersects wiath a plane, if it has, you can: 1.- divide that poly in triangles,
2.-get the center of the poly,
3.-get 2 vectors, each one from the center to 2 vertices (this is for ALL the verts)
4.- add all their angles, and if they sume 360 or more, it will be inside the poly...

the algorithm code looks like this:
for ( int i = 0; i < verticeCount; i++ )	{		vVerticeA = GetVector ( vPolygon, vIntersectionPoint );		vVerticeB = GetVector ( vPolygon[ (i+1)%verticeCount ], vIntersectionPoint );		angle += AngleBetweenVectors( vVerticeA, vVerticeB );			}	if( angle >= (2.0*PI)  )		return true;	return false;

it should be a more efficiente way...but that works...

##### Share on other sites
yeah but the problem is that when you rotate using glRotatef the vertices don't actually change, so you that won't work?

##### Share on other sites
then, you might want to implement your own rotation function, that way you can track the new vertices pos, that's how i do it in my current project (although, by now, i think i have no objects that need this )

you can google for "Rotate a point about an arbitrary axis", its simple to implement it, but the equations aren't intuitive....

maybe there is a more advanced/optimized way to do it.... but i dont know how....

we can wait for a more experienced user to tell us how....

##### Share on other sites
I wish I was better at Math then I would be able to figure out what I need to know to manually rotate each vertice of a rectangle around any given point :(

I tried this, but it doesn't work:

[codebox]
for(int i = 0; i < 4; i++)
{
//center of polygon
float xx = vertices[0] + xpos;
float yy = vertices[1] + ypos;

//current angle between vector and horizontal
float angle = getAngle(y,768/2,x,1024/2);
//vector from point to center of polygon
float d = sqrt((float)((1024/2-xx)*(1024/2-xx)) + ((768/2-yy)*(768/2-yy)));

if (angle > 360) angle -= 360;
if (angle < 0) angle += 360;

//set x and y position
}

float Grid::getAngle(float x1,float x2,float y1,float y2)
{
float angle = 0.0;
angle =(atan2((float) x1 - x2,(float) y1 - y2) * 57.29578049f);
if (angle < 0)
angle += 360.0f;
return angle;
}
[/codebox]

##### Share on other sites
well, i'm gonna try to explain a little.

you have an struct like this:

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

first of all, you need to get the normal of the polygon, for that, you need to know a few things...

1.-to get a vector between 2 points, you need this:
(rember a vector is a direction with a magnitude)
GetVector( C3DVector vPoint1, C3DVector vPoint2 ){	C3DVector newVector;        newVector = vPoint1 - vPoint2; //(for x,y,z) }

you also need the cross product for getting the normal of the plane...
CrossProduct(C3DVector vV1, C3DVector vV2 ){        C3DVector vNormal;	        // cross product equation	vNormal.x = ( vV1.y * vV2.z ) - ( vV1.z * vV2.y );	vNormal.y = ( vV1.z * vV2.x ) - ( vV1.x * vV2.z );	vNormal.z = ( vV1.x * vV2.y ) - ( vV1.y * vV2.x );}

you need to now how to get the magnitude (length) of a vector
Magnitude( C3DVector vVector ){float magnitude = float)sqrt( (vVector.x * vVector.x) +							 (vVector.y * vVector.y) +  (vVector.z * vVector.z) ) ;}

to Normalize a vector, you need to divide the vector by its magnitude:
Normalize( C3DVector vNormal ){vNormal = vNormal / Magnitude(vNormal);}

now, this will be the entire process for get the Normal of a polygon (the normal is the direction the polygon is looking at:
C3DVector GetPolygonNormal(C3DVector vPolygon[] ){        C3DVector vNormal, vVector1, vVector2 = CVector();	vVector1 = GetVector( vPolygon[2], vPolygon[0] );	vVector2 = GetVector( vPolygon[1], vPolygon[0] ),			vNormal = CrossProduct(vVector1, vVector2);	vNormal = Normalize(vNormal);			return vNormal;}

you need to know how to get the normal of a polygon in order to rotate it (at least, that's the way i do it, you can also use quaternions,or there may be another way.... but dont know how...)

now that you have all that..... you can use this tutorial to guide your self....
http://local.wasp.uwa.edu.au/~pbourke/geometry/rotate/

it has also some source code at beginning of th article (in example.c and source.c )