• 12
• 27
• 9
• 9
• 20

# Raycast with thickness

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

## 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 on other sites
What's the context? What information do you want the raycast to return? (E.g. Boolean result, collision point and normal, etc.)

##### 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 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 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 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 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 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 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.