I am currently working on a game where I need to calculate the line of sight of enemies to my character. Mostly I’m relying on raycasts, but when my character is on top of an enemy and the raycast would start within my character, the raycast doesn’t hit the character.
It is fairly easy to calculate whether the raycast origin is within my character when its collision shape is just an aabb, but it gets more complex with other shapes. So I was wondering whether there is a function I could use to find out if a point is within a collision shape or not.
I assume you are using Ammo? If so, then there isn’t, no. Some Bullet shapes have a method
shape.isInside() that can help detecting if point is inside the shape, but those are not available in Ammo. You could build your own Ammo and include those. Another option is to allow raycast hit a backface, but that is for ghost objects. PlayCanvas doesn’t use them, though, so you’d have to use Ammo API directly, if you are adventurous.
If all shapes are primitive, you could also calculate it yourself. For example, a box - using its world transform, convert the point to local space of the box and check if it is within its bounding box. For sphere - simply if the distance to origin is smaller than radius. And so on. For mesh colliders - I believe they report back faces by default on raycasts.
Thought of another option - shapecast. Your enemy could have an eyesight as a cone, so you could use a cone shapecast against player. I think it should work, even if the player is on top of the enemy (at the cone’s corner). PlayCanvas doesn’t support shapecast yet, but you could grab latest Ammo from its github repo and use this project as reference on how to make a shapecast:
Thanks for the quick reply
Yes, I’m using Ammo as it is imported via the editor.
If all shapes are primitive, you could also calculate it yourself.
Thats what I’m doing right now, currently we are using a very crude aabb for our character, so thats easy enough, although not very precise. I think we will stick with this solution for now though, until we need a better one. Thanks for the ideas