Collision Detection Against Lines

Started by
9 comments, last by Jinroh 15 years, 10 months ago
Not meaning to bump my post or anything, but I was now wanting to use the aformentioned plane-distance algorithm to do a sort of backface culling with the wall line segments.

I have a function made out, but hasn't worked quite well enough for me.

I have the following:

PlayerX/PlayerY
PlayerAngle

LineSegment x1, y1, x2, y2

RayHitX/RayHitY //Where the ray hits the line segment

float calculateBackface(int x1, int y1, int x2, int y2, int camX, int camY){	float vx, vy, vz;	if(abs(x1 - x2) < abs(y1 - y2))	{	   float vx = x2 - x1;	   float vy = y2 - y1;	   float vz = 0;	}else{	   float vx = x1 - x2;	   float vy = y1 - y2;	   float vz = 0;	}	float nx = 0;	float ny = 0;	float nz = 1.0f;	float nXvx = (y1 * nz) - (y2 * vz); //TODO: Always going to be 0 for the second part	float nXvy =  -((vx * nz) - (vz * nx));	float nXvz = ((vx * ny) - (vy * nx));	//We now have the normalized vector of the line now let's make a plane	float A = nXvx;	float B = nXvy;	float C = nXvz;	float D = ((-nXvx * x1) + (-nXvy * y1) + (-nXvz * 0));	//NOW WE HAVE A PLANE	//Calculate the Distance of the target point if it is + then it is on the correct side of the wall.	//Use the normal vector of n (dot) target point <x, y, 0>	float dist = ((nXvx * camX) + (nXvy * camY) + (nXvz * 0)) + D;	return dist;}


I load in the x's and y's respectively of the line segment x1 y1, x2, y2 and the camX and camY are the pX and pY.

The thing is I don't rotate the walls, I only cast rays from the player and the angle so I'm trying to figure out how I can plug this in to take into account the angle at which the player is facing.

My instinct is to take playerX - line.x1 and playerY - line.y1 and make a vector out of them and plug them into the function's CamX and CamY.

Anyone have any thoughts on this?

EDIT: All right, I was doing some reading and since my Camera is not always at 0,0,0 like a 'normal' 3d engine. I have to find the vector from the pX/pY to a point on the line vector to use as my camera vector. Since the pX/pY Camera will not be at 0, 0, 0.

Thanks.

[Edited by - Jinroh on June 9, 2008 7:01:15 AM]
The wolf and his mate howl, taking solace in the silver moon. Pressing ever foreward to see what the future holds.

This topic is closed to new replies.

Advertisement