## Sides of intersection

KiroNeem
Unregistered

Post: #1
My situation is this... I have a line in the game that represents a laser, when the laser hits one of the tiles on the game I need to figure out the point of intersection. This is easy enough to do, although I need to know what side of the tile it intersects on. I have been unable to figure this out, any ideas?
Member
Posts: 153
Joined: 2004.12
Post: #2
Hmm maybe find the angle between the plan normal, and the laser, if its more than 90Âº the laser hit on the backside... less than 90Âº frontside.

Seems like it should work.

There was a long silence...
'I claim them all,' said the Savage at last.
Moderator
Posts: 522
Joined: 2002.04
Post: #3
Is there a collision mask on the tiles, or is it just a rectangle?

-Jon
KiroNeem
Unregistered

Post: #4
Actually I just figured out a full-proof way to calculate it... although It will need some optimization. I will post the code up later tonight when I get some time.
KiroNeem
Unregistered

Post: #5
Sitting down with my trust pad of paper I continued to draw a box with a line going through it, thinking long and hard how I can determine if the line intersects with what edge. Eventually I figure out how to do it.

The information I have is this.

float posX //The first point of the line
float posY
float posX2 //Actually the vector of posX, so to really get the next point you would posX+posX2
float posY2

int tileX //The tile position that the line intersects
int tileY

//Note this is not the actual code

Code:
```float slopeY = posY2/posX2; if( frontTileMap[(int)(posY/32)][(int)(posY/32)] == solid ) //32 being the tile size {     If( posX < tileX && slopeY*(tileX-posX)+posY > tileY && slopeY*(tileX-posX)+posY > tileY+32 )     {         //It collided with the left edge         posY2 = slopeY * (posX2 = tileX-posX);     }     else if... do the same for the other sides }```

My real code looks like this, it needs to be re written cleaner

Code:
```int tempX = (weapons[4].rounds->posX+weapons[4].rounds->posX2)*kFrontTileMulti; int tempY = (weapons[4].rounds->posY+weapons[4].rounds->posY2)*kFrontTileMulti; if( frontTileMap[tempX][tempY] == tSolid)         {             float slopeY = weapons[4].rounds->posY2/weapons[4].rounds->posX2;             float slopeX = weapons[4].rounds->posX2/weapons[4].rounds->posY2;             tempX *= kFrontTileSize;             tempY *= kFrontTileSize;                          if( weapons[4].rounds->posX < tempX && (slopeY*(tempX-weapons[4].rounds->posX))+weapons[4].rounds->posY > tempY && (slopeY*(tempX-weapons[4].rounds->posX))+weapons[4].rounds->posY < (tempY+kFrontTileSize) )                 weapons[4].rounds->posY2 = slopeY * (weapons[4].rounds->posX2 = tempX-weapons[4].rounds->posX);             else if( weapons[4].rounds->posX > tempX+kFrontTileSize && (slopeY*(-(weapons[4].rounds->posX-(tempX+kFrontTileSize)))  )+weapons[4].rounds->posY > tempY  && (slopeY*(-(weapons[4].rounds->posX-(tempX+kFrontTileSize)))  )+weapons[4].rounds->posY < (tempY+kFrontTileSize) )                 weapons[4].rounds->posY2 = slopeY * (weapons[4].rounds->posX2 = -(weapons[4].rounds->posX-(tempX+kFrontTileSize)));             else if( weapons[4].rounds->posY < tempY && (slopeX*(tempY-weapons[4].rounds->posY))+weapons[4].rounds->posX > tempX && (slopeX*(tempY-weapons[4].rounds->posY))+weapons[4].rounds->posX < tempX+kFrontTileSize )                 weapons[4].rounds->posX2 = slopeX * (weapons[4].rounds->posY2 = tempY-weapons[4].rounds->posY);             else                 weapons[4].rounds->posX2 = slopeX * (weapons[4].rounds->posY2 = -(weapons[4].rounds->posY-(tempY+kFrontTileSize)));                                           weapons[4].rounds->posX += timeElapsed*weapons[4].rounds->velocityX;             weapons[4].rounds->posY += timeElapsed*weapons[4].rounds->velocityY;         }```