Ray Intersect with arbitrary volume...
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
***************************************/
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..)
That code should work. At least it''s quite optimized.
-Hans [home]
(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
***************************************/
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
***************************************/
(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:
Now I just assume that you want to eliminate the z component. But of course you can similarly eliminate any component.
-Hans [home]
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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement