Sides of intersection

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?
Quote this message in a reply
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.
Quote this message in a reply
Posts: 522
Joined: 2002.04
Post: #3
Is there a collision mask on the tiles, or is it just a rectangle?

Quote this message in a reply
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.
Quote this message in a reply
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

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

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);
                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;
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Ray-Triangle Intersection mikey 2 6,133 Aug 15, 2010 05:11 AM
Last Post: mikey
  Triangle Intersection Tests merrill541 1 4,065 Feb 6, 2009 12:13 PM
Last Post: scgames
  2D Polygon intersection testing unknown 6 11,014 Aug 15, 2006 04:17 PM
Last Post: unknown
  Box to Plane Intersection With Quaternions KiroNeem 6 8,235 Jun 25, 2006 05:44 PM
Last Post: KiroNeem
  OBB Intersection volume hangt5 0 4,163 Aug 5, 2005 04:22 PM
Last Post: hangt5