Sign in to follow this  
Sckuz

Hit detection problem

Recommended Posts

Hello,

I am Brian Washechek, a 28 year old student at Casper College. Please look at this game! It's called Force Disruptor 2, a sequel to a game i wrote a long, long time ago (I'm thinking like 7 years). Something is "odd" about the hit detection for the walls. By "odd", i mean i can't get it to work! At all. Well, I'll keep on attempting to write code for this if you would take a look at it! basically the walls only display(no hit detection) and i can't get it right! This is a sequel to Force Disruptor (the original), that got lost a long time ago.

bool lineSegmentIntersection(
double x1,double y1,
double x2,double y2,
double x3,double y3,
double x4,double y4)
{
// Return false if either of the lines have zero length
if (x1 == x2 && y1 == y2 ||
x3 == x4 && y3 == y4){
return false;
}
// Fastest method, based on Franklin Antonio's "Faster Line Segment Intersection" topic "in Graphics Gems III" book (http://www.graphicsgems.org/)
double ax = x2-x1;
double ay = y2-y1;
double bx = x3-x4;
double by = y3-y4;
double cx = x1-x3;
double cy = y1-y3;

double alphaNumerator = by*cx - bx*cy;
double commonDenominator = ay*bx - ax*by;
if (commonDenominator > 0){
if (alphaNumerator < 0 || alphaNumerator > commonDenominator){
return false;
}
}else if (commonDenominator < 0){
if (alphaNumerator > 0 || alphaNumerator < commonDenominator){
return false;
}
}
double betaNumerator = ax*cy - ay*cx;
if (commonDenominator > 0){
if (betaNumerator < 0 || betaNumerator > commonDenominator){
return false;
}
}else if (commonDenominator < 0){
if (betaNumerator > 0 || betaNumerator < commonDenominator){
return false;
}
}
if (commonDenominator == 0){
// This code wasn't in Franklin Antonio's method. It was added by Keith Woodward.
// The lines are parallel.
// Check if they're collinear.
double y3LessY1 = y3-y1;
double collinearityTestForP3 = x1*(y2-y3) + x2*(y3LessY1) + x3*(y1-y2); // see http://mathworld.wolfram.com/Collinear.html
// If p3 is collinear with p1 and p2 then p4 will also be collinear, since p1-p2 is parallel with p3-p4
if (collinearityTestForP3 == 0){
// The lines are collinear. Now check if they overlap.
if (x1 >= x3 && x1 <= x4 || x1 <= x3 && x1 >= x4 ||
x2 >= x3 && x2 <= x4 || x2 <= x3 && x2 >= x4 ||
x3 >= x1 && x3 <= x2 || x3 <= x1 && x3 >= x2){
if (y1 >= y3 && y1 <= y4 || y1 <= y3 && y1 >= y4 ||
y2 >= y3 && y2 <= y4 || y2 <= y3 && y2 >= y4 ||
y3 >= y1 && y3 <= y2 || y3 <= y1 && y3 >= y2){
return true;
}
}
}
return false;
}
return true;
}

void fixCollisions()
{


for (int i =0;i<wallUbound;i++)
{

if (lineSegmentIntersection(FDX-sin(0*FDA),FDY-cos(9*FDA),
FDX+sin(1*FDA),FDY+cos(.8*FDA),
wallX[i]-sin(wallRot[i]),
wallY[i]-cos(wallRot[i]),
wallX[i]+sin(wallRot[i]),
wallY[i]+cos(wallRot[i])))
{

bigX=true;
}
}

}

Seriously, Thank you for the help (If you'll offer it, that is)

Share this post


Link to post
Share on other sites
Considering that you do not factor your code out at all and write all your code in one function, this is unfathomably hard to read. I suggest creating a Vector class that would hold x, y and all necessary functions. And then create a segment class. Then, the pseudocode would be:

Vector lineLineIntersection(Segment seg_1, Segment seg_2)
{
// code for line intersection, inet is full of algorithms
// if it happends that the segments are collinear, throw an exception
}

bool pointIsInsideSegment(Vector point, Segment segment)
{
// code for testing if a point lies on a line passing through the segment and is between the two segment ends
}

bool segmentsAreCollinear(Segment seg_1, Segment seg_2)
{
//test if the segments overlap, but only if they are collinear
//throw exception otherwise
}
bool lineSegmentIntersection(Segment segment, Segment line)
{
Vector intersection;
try
{
intersection=lineLineIntersection(segment, line);
return pointIsInsideSegment(intersection, segment);
}
catch(Exception /*whatever type you have set it to*/)
{
try
{
return segmentsAreCollinear(segment, line);
}
catch(Exception)
{
cout<<"Something is wrong with the code."<<std::endl;
}
}
}





Try organizing your code this way, it's very hard to understand in its current form, especially because we're most likely not familiar with the algorithms that you use.

[Edited by - Ratslayer on October 10, 2010 8:30:26 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this