Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

WarAmp

Ray Intersect with arbitrary volume...

This topic is 6514 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 ***************************************/

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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
***************************************/

Share this post


Link to post
Share on other sites
(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]

Share this post


Link to post
Share on other sites
Thanx again. I''ll see if I can make that work



/**************************************
* Hookt on Fonix relly werked fer mee!
* WarAmp
***************************************/

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!