This is my function for triangular collision checking.
ship.x1
ship.x2
ship.x3
ship.y1
ship.y2
ship.y3
Are all integers

missile[j].x
missile[j].y
Are doubles

PI is a constant equal to 3.14159265


Everything compiles correctly, but for some reason a1, a2, and a3 all end up being equal to 0
Is there something wrong with my math? Do I have some kind of syntax error?


void CollisionCheck()
{
     double a1, a2, a3;
     for (int j=0; j<254; j++)
     {
       if (missile[j].visible)
       {
         for (int i=0; i<2; i++)
         {
              a1=atan2(ship[i].y1-missile[j].y, ship[i].x1-missile[j].x);
              a1+=-atan2(ship[i].y2-missile[j].y, ship[i].x2-missile[j].x);
     	      a1=a1*(PI/180);
              a1=abs((int)a1);
			  
				  
	      a2=atan2(ship[i].y2-missile[j].y, ship[i].x2-missile[j].x);
              a2+=-atan2(ship[i].y3-missile[j].y, ship[i].x3-missile[j].x);
	      a2=a1*(PI/180);
              a2=abs((int)a1);
			  
				  
	      a3=atan2(ship[i].y3-missile[j].y, ship[i].x3-missile[j].x);
              a3+=-atan2(ship[i].y1-missile[j].y, ship[i].x1-missile[j].x);
              a3=a1*(PI/180);
              a3=abs((int)a1);
              
              if (a1>180) {a1+=-180;}
              if (a2>180) {a2+=-180;}
              if (a3>180) {a3+=-180;}
      
              
              if (a1+a2+a3>=360)
              {
                                //ship[i] is colliding with missile[j]
                                ship[i].health+=-25;
                                ship[i].visible=false;
                                missile[j].active=false;
                                missile[j].visible=false;
              }
         }
       }
     }
}

Do you really want to be casting away the precision when you call abs? Maybe you should be using fabs instead so that it gives you the absolute value without losing the precision.

Ok, thanks for that tip.
Tried it and now a1, a2, and a3 are all between 0 and 1
(Which is not good because they should add up to around 360)

PI/180 turns degrees into radians.
But since they're already in radians, don't you want to do the inverse of this?

Ok, i found a number of errors in my code. I corrected all of the ones that I found, however, something is still not right.

Corrections:
1. Changed abs() to fabs() so as to not lose presision

2. Changed (PI/180) to (180/PI)
PI/180 converts degrees to radians
180/PI converts radians to degrees (this is what i meant to do)

3. Changed line a2=fabs(a1); to a2=fabs(a2); Changed line a3=fabs(a1); to a3=fabs(a3); That is what I meant to do, but I guess I forgot to change that when I copied and pasted that from my previous lines of code.

4. Changed if (missile[j].visible) to if (missile[j].active) This change is unimportant for debugging purposes, it was just an improvement for the game. It checks if the missile is armed rather than if it is visible.

void CollisionCheck()
{
     double a1, a2, a3;
     for (int j=0; j<254; j++)
     {
       if (missile[j].active)
       {
         for (int i=0; i<2; i++)
         {
              a1=atan2(ship[i].y1-missile[j].y, ship[i].x1-missile[j].x);
              a1+=-atan2(ship[i].y2-missile[j].y, ship[i].x2-missile[j].x);
	      a1=a1*(180/PI);
              a1=fabs(a1);
			  
				  
			  a2=atan2(ship[i].y2-missile[j].y, ship[i].x2-missile[j].x);
              a2+=-atan2(ship[i].y3-missile[j].y, ship[i].x3-missile[j].x);
			  a2=a1*(180/PI);
              a2=fabs(a2);
			  
				  
	          a3=atan2(ship[i].y3-missile[j].y, ship[i].x3-missile[j].x);
              a3+=-atan2(ship[i].y1-missile[j].y, ship[i].x1-missile[j].x);
			  a3=a1*(180/PI);
              a3=fabs(a3);
			  
              
              if (a1>180) {a1+=-180;}
              if (a2>180) {a2+=-180;}
              if (a3>180) {a3+=-180;}
       
              if (a1+a2+a3>=360)
              {

                                //ship[i] is colliding with missile[j]
                                ship[i].health+=-25;
                                ship[i].visible=false;
                                missile[j].active=false;
                                missile[j].visible=false;
              }
         }
       }
     }
}

My code still compiles fine with no warnings.
There is still a problem however.
Now the sum of a1, a2, and a3 is always >= 360

write PI/180.0 instead of PI/180...
it may work... Else the result will get rounded off to zero..

I changed it to 180/PI so even if it did do integer division, it won't get rounded to 0.
But i dont think c++ does that. I think you may be thinking in Java. But even then if it was Java, PI=3.14159265 so it would assume a double.
(I think..)


But just to be safe, I tried adding a .0 to no avail. It Changed nothing.

ok, found a few more errors. a2=a1*(180/PI); changed to a2=a2*(180/PI); a3=a1*(180/PI); changed to a3=a3*(180/PI); Still not functioning correctly.

if I remove the lines:

if (a1>180) {a1+=-180;}
if (a2>180) {a2+=-180;}
if (a3>180) {a3+=-180;}

Then it works fine but only for the most recently fired missile

Alright,
I have ALMOST everything figured out and working perfectly

Only the last missile in the array visible was colliding with the ship because of an if/else statement I added.


Now I have a different problem:
If I comment out the lines:

if (a1>180) {a1+=-180;}
if (a2>180) {a2+=-180;}
if (a3>180) {a3+=-180;}

Then it works almost perfectly. When a missile collides with ship, the ship loses health and becomes invisible, just like I programmed it to do.
HOWEVER, sometimes a missile underneath the ship or to the left of the ship registers as a collision.
Those three lines that are commented out are designed to prevent that from happening. So why do they not work?
If I leave those three lines in then nothing ever gets registered as a collision.

I don't know what is wrong, it makes no sense to me. Any help will be appreciated.

By the way this is my 14th post. 14 is my lucky number, YAY

Ok, I answered my own question:

I should have been looking for a co-terminal angle the whole time.
Instead of subtracting 180, I should have been taking the absolute value of a1 minus 360 (and likewise with a2 and a3 also)

New code reads:

if (a1>180) {a1=fabs(a1-360);}
if (a2>180) {a2=fabs(a2-360);}
if (a3>180) {a3=fabs(a3-360);}

Works perfectly.

This article has been dead for over six months. Start a new discussion instead.