Ray Intersect with arbitrary volume...

Started by
3 comments, last by WarAmp 23 years, 3 months ago
Hello all you fine folks! My question is, how do I find the interesection point of a ray cast into a volume bounded by n vertical ("wall") planes, and 2 "floor" and "ceiling" planes? Any help that you could provide would be most appreciated. /************************************** * Hookt on Fonix relly werked fer mee! * WarAmp ***************************************/
Waramp.Before you insult a man, walk a mile in his shoes.That way, when you do insult him, you'll be a mile away, and you'll have his shoes.
Advertisement
So basically you just need a 2d ray-ray intersection, if you have constant floor and ceiling.



(xa1=1st x-coordinate for line A,
yb2=2nd y-coordinate for line B..)

    bool 2dintersect(int xa1,int ya1,int xa2,int ya2,int xb1,int yb1,int xb2,int yb2){  int jako,luku; //just some temp variables  //trivial cases  if (ya1 < ya2)  {    if (yb1 < ya1 && yb2 < ya1) return 0;    if (yb1 > ya2 && yb2 > ya2) return 0;  }  else  {    if (yb1 > ya1 && yb2 > ya1) return 0;    if (yb1 < ya2 && yb2 < ya2) return 0;  }    if (xa1 < xa2)  {    if (xb1 < xa1 && xb2 < xa1) return 0;    if (xb1 > xa2 && xb2 > xa2) return 0;  }  else  {    if (xb1 > xa1 && xb2 > xa1) return 0;    if (xb1 < xa2 && xb2 < xa2) return 0;  }    jako = (xa2 - xa1) * (yb2 - yb1) - (xb2 - xb1) * (ya2 - ya1);  //parallel or point  if (jako == 0) return 0;  luku = (xb2 - xb1) * (ya1 - yb1) - (yb2 - yb1) * (xa1 - xb1);  if (jako > 0)  {    if (luku >= 0 && luku <= jako)    {      luku = (xa2 - xa1) * (ya1 - yb1) - (ya2 - ya1) * (xa1 - xb1);          if (luku >= 0 && luku <= jako) return 1;    }  }  else  {    if (luku <= 0 && luku >= jako)    {      luku = (xa2 - xa1) * (ya1 - yb1) - (ya2 - ya1) * (xa1 - xb1);          if (luku <= 0 && luku >= jako) return 1;    }  }  return 0;}    


That code should work. At least it''s quite optimized.

-Hans [home]
Wow, thanxs Hans! I have just a few questions about it tho: Could you please your function parameters a bit more? Way is it a ray-ray intersect?

My ray is in the following form: vector3 Source, vector3 Direction.
How would I decompose that into a 2d ray?

Thank you for your time.


/**************************************
* Hookt on Fonix relly werked fer mee!
* WarAmp
***************************************/
Waramp.Before you insult a man, walk a mile in his shoes.That way, when you do insult him, you'll be a mile away, and you'll have his shoes.
(Source.x = x component of the vector)

xa1=Source.x;
ya1=Source.y;
xa2=Source.x+Direction.x;
ya2=Source.y+Direction.y;

And same for both rays (intersection ray + wall ray).

You can call the funciton like this:

2dintersect(Source.x,Source.y,Source.x+Direction.x,Source.y+Direction.y,SourceWall.x,SourceWall.y,SourceWall.x+DirectionWall.x,SourceWall.y+DirectionWall.y) 


Now I just assume that you want to eliminate the z component. But of course you can similarly eliminate any component.

-Hans [home]
Thanx again. I''ll see if I can make that work



/**************************************
* Hookt on Fonix relly werked fer mee!
* WarAmp
***************************************/
Waramp.Before you insult a man, walk a mile in his shoes.That way, when you do insult him, you'll be a mile away, and you'll have his shoes.

This topic is closed to new replies.

Advertisement