Collidable cone shape

I’m trying to implement a sight for units in my game. Currently, there’s a ghost control object with a cone shape to simulate the sight. I’m also using a ray cast to test if the units in the field of view are obstructed or not.

However, because the sight extends far and wide and also because bullet physics only uses AABB from ghost objects to test intersections, what the unit with sight sees is ultimately wrong.

I’d like to have a cone collidable to actually intersect units in the sight (if there were also previously intersected with the cone’s AABB).

Is there a way to do that?

The only way I could think of off the top of my head would be to create a cone shaped object in blender, and then use that geometry as your collision object and scale it by a Vector3f when you need to change the size.

Then you could also change the object’s cull hint to always since it’s just for collisions and doesn’t need visually rendered.

In practice raw math will beat physics. Substitute the cone for a triangle. It becomes basic simple now. Just check for intersects.

1 Like

I suppose if the sight should extend far, I would need multiple triangles to cover the gaps?

What gaps?

All you need is an equation/function that checks whether the coordinates/boundingvolume are inside the theoretical triangle in a given direction. It’s that simple. You don’t need bullet or anything except basic java. I guess the shape would now be described as a pyramid. It isn’t super difficult using a radius instead and actually having a mathematical cone.

1 Like

3D gaps. If there is a unit over the triangle, then it will not be detected. A workaround would be to add another triangle perpendicular to the first one.

Thank you very much! I now have at least one good alternative :slight_smile:

Oh, gotcha - I wasn’t sure what you were considering for your line of sight shape. Like @jayfella said, you’ll want a pyramid or cone to handle that.

if you treat the length as a scale you can work out the height.

float viewDistanceLength = 200f; // max distance an entity can view
float distFromPlayer = 132f; // a detected position in the X/Z planes.

float dist = distFromPlayer / viewDistanceLength; // convert distance to 0.0 - 1.0 scale

float viewDistanceMaxHeight = 10f; // the max vertical view distance
float height = viewDistanceMaxHeight * dist; // the farther away, the bigger the distance.

Now you have a volume of view, not a flat triangle.

2 Likes