Jump to content
  • Advertisement
Sign in to follow this  
Josh Klint

Raycast with thickness

This topic is 2705 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

I am trying to implement a raycast with thickness for testing the intersection of a line and a triangle. I suppose this would take the form of a moving sphere / triangle intersection. Does anyone know where I can find some simple code to do this?

Share this post


Link to post
Share on other sites
Advertisement
What's the context? What information do you want the raycast to return? (E.g. Boolean result, collision point and normal, etc.)

Share this post


Link to post
Share on other sites
It's more for graphics than physics. This would be used for stuff like a projectile being shot or AI line-of-sight tests.

The normal returned can simply be the normal of the triangle. It should return a boolean and fill in values to a Vec3 for the position the collision occurs at.

p0 and p1 are the ray start and end points. The triangle is defined by points a, b, and c. The position and normal get filled into the last two parameters.

bool TriangleIntersectsRay(const Vec3& p0, const Vec3& p1, const Vec3& a, const Vec3& b, const Vec3& c, const float& radius, Vec3& position, Vec3& normal)
{
}

Share this post


Link to post
Share on other sites
You can certainly perform a swept-sphere vs. triangle test (which is essentially what this is). The full test is fairly complex though, and depending on what you need this for exactly, there might be a more lightweight solution that would work.

As for the sphere vs. triangle test, it was recently touched on here.

Share this post


Link to post
Share on other sites
I'm also thinking about ways to make the triangle bigger and just test a ray intersection with this bigger triangle, but there are so many problems...

Share this post


Link to post
Share on other sites

I'm also thinking about ways to make the triangle bigger and just test a ray intersection with this bigger triangle, but there are so many problems...

May I ask what the purpose of the 'thick' ray is? In other words, what is it you need that a simple ray-triangle test won't give you?

Share this post


Link to post
Share on other sites
If you shoot a rocket and use raycasts to determine collision, you want some thickness to it so it doesn't go right by a wall too closely. A cylinder / triangle intersection test would work just as well for this.

Share this post


Link to post
Share on other sites

If you shoot a rocket and use raycasts to determine collision, you want some thickness to it so it doesn't go right by a wall too closely. A cylinder / triangle intersection test would work just as well for this.

Ah, I see.

For the sphere-triangle test, I'd recommend checking out the thread I mentioned, and also perhaps taking a look at geometrictools.com (you should be able to find code and/or documentation there).

The sphere-triangle test can be expressed in a few different ways, one of which is raycasting against the CSO (configuration space obstacle) of the sphere and the triangle.

A straightforward (but inefficient) way to implement this is to raycast against the extruded triangle face, and against the three cylinders and three spheres corresponding to the edges and vertices, and then take the closest intersection point (if any). The test can be implemented more efficiently, but the implementation will be a little more involved.

There are of course other options as well, such as using an existing physics engine, or using something other than a triangle soup to represent the environment.

Share this post


Link to post
Share on other sites
If your worried about the rocket case then how about a few rays instead of just one? 1 ray on the nose as usual then say 3 around it, shorter but angled out slightly. I use just one ray for my rocket (ray length = distance it will travel that frame) and it works fine. When I am generating my pathing graphs, I use a sphere sweep test for visibility (others have posted links), rays alone weren't good enough.

Share this post


Link to post
Share on other sites
Instead of extruding the sphere over time, why not extrude the triangle in the opposite direction and make a solid shape you test for intersection with the sphere at the ray starting point? This would result in a 5-sided convex hull, which is fairly simple to test against a sphere. You just push the faces of the hull out by the radius of the sphere and do a point intersection test.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!