Thread: Collision detection. does not work!

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    Question Collision detection. does not work!

    Hello,

    I am Brian Washechek, a 28 year old student at Casper College. Please look at this game! It's Force Disruptor 2, a sequel to a game i wrote a long, long time ago (i'm thinking like 7 years). I'll keep on attempting to write code for this if you would take a look at it! Basicly 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. Seriously, Thank you for the help

    Code:
    bool intersectionOfLines(double x1,
                        double y1,
                        double x2,
                        double y2,
                        double x3,
                        double y3,
                        double x4,
                        double y4,
                        double *x,
                        double *y) 
    
    {
    
    double Ax,Bx,Cx,Ay,By,Cy,d,e,f,num,offset;
    short x1lo,x1hi,y1lo,y1hi;
    
    Ax = x2-x1;
    Bx = x3-x4;
    
    if(Ax<0) { 
    x1lo=(short)x2; x1hi=(short)x1;
    } else {
    x1hi=(short)x2; x1lo=(short)x1;
    }
    if(Bx>0) {
    if(x1hi < (short)x4 || (short)x3 < x1lo) return false;
    } else {
    if(x1hi < (short)x3 || (short)x4 < x1lo) return false;
    }
    
    Ay = y2-y1;
    By = y3-y4;
    
    if(Ay<0) { 
    y1lo=(short)y2; y1hi=(short)y1;
    } else {
    y1hi=(short)y2; y1lo=(short)y1;
    }
    if(By>0) {
    if(y1hi < (short)y4 || (short)y3 < y1lo) return false;
    } else {
    if(y1hi < (short)y3 || (short)y4 < y1lo) return false;
    }
    
    
    Cx = x1-x3;
    Cy = y1-y3;
    d = By*Cx - Bx*Cy; 
    f = Ay*Bx - Ax*By; 
    if(f>0) { 
    if(d<0 || d>f) return false;
    } else {
    if(d>0 || d<f) return false;
    }
    
    e = Ax*Cy - Ay*Cx; 
    if(f>0) { 
    if(e<0 || e>f) return false;
    } else {
    if(e>0 || e<f) return false;
    }
    
    //compute intersection coordinates
    
    if(f==0) return false;
    num = d*Ax;  
    offset = SAME_SIGNS(num,f) ? f/2 : -f/2;
    *x = x1 + (num+offset) / f;
    
    num = d*Ay;
    offset = SAME_SIGNS(num,f) ? f/2 : -f/2;
    *y = y1 + (num+offset) / f; 
    
    return true;
    }
    //....................later in the c++ file...................................
    
               if (intersectionOfLines(
               FDX-cos(FDA)+0,
               FDY-sin(FDA)+9,
               FDX+cos(FDA)+1,
               FDY+sin(FDA)+.8,
               wallX[i]-cos(wallRot[i])*wallRot[i],
               wallY[i]-sin(wallRot[i])*wallRot[i],
               wallX[i]-cos(wallRot[i])*wallRot[i],
               wallY[i]-sin(wallRot[i])*wallRot[i],
               &FDX,&FDY))
               {
                       bigX = true;
                       // A collision occured, but for now it gets a BIG  X...
               }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    It's too much work to analyze the algorithm, but I can give you another piece of advise: look into unit testing.
    Unit testing allows you to write tests - expected outputs - for certain functions. All you have to do is write a few easy cases (but not so easy that you don't test all different cases), and the unit tests will tell you for what inputs the algorithm didn't work. After that, all you have to do is look at all the values the algorithm uses and where it is different from what you expect.
    Calculate it by hand; make sure you get it right. Then let the algorithm calculate the same and see where it goes wrong.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Why would you reveal your real name on a forum and/or your age? There is far too much code there to sift through. Post the portion you feel is the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Collision Detection with DirectX9
    By chris2307 in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2011, 12:41 PM
  2. Breakout Collision Detection Algorithm Help
    By xmltorrent in forum Game Programming
    Replies: 8
    Last Post: 08-24-2006, 02:32 PM
  3. Collision Detection
    By Grantyt3 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2005, 03:21 PM
  4. good collision detection routines
    By cozman in forum Game Programming
    Replies: 1
    Last Post: 10-19-2001, 01:05 PM
  5. collision detection
    By tHaPuTeR in forum Game Programming
    Replies: 11
    Last Post: 09-22-2001, 10:53 AM