How to test if point collides with collision object? [SOLVED]

Is there a way to see if a vector3 point lies within a collision object? I currently am using physics.raycast to determine the closest point on an enemy collision object. However, I want to test to see if this point actually intersects the collision object of the enemy prior to enabling firing upon the enemy. See the below image for explanation:

enemy_raycast

In the picture above, the raycast is hitting a wall in front of the enemy instance. I would like to detect this by checking the point the ray hits against the collision object to determine a “hit.”

Any suggestions?

Why don’t you have the raycast detect both walls and enemies (two different collision groups)? If it hits an enemy, you can fire. If it hits a wall or there are no hits, don’t.

3 Likes

Groups don’t work for me. I have to use all default group or else the collisions no longer register properly. Not sure why. I’m working around that by using a lookup table to register the types and then retrieve them as a property.

EDIT: Ah thanks, this suggestion led me to the solution. I can simply get the property of the raycast result id and then check the type from there. This does mean that the “projectile” objects my player shoots will likely be simply points or 1 pixel diameter circle collision objects. Then it should be easy to know that they won’t collide with something on the way to the enemy. Thanks :slight_smile:

It would be worth you figuring out groups so you can use them effectively.

Group is the one the object belongs to.

Mask are the ones the object’s group should collide with. It is a comma separated list. You have to have references between every group you want collisions to happen.

To mask between groups they need to reference each other, which means if you want a kinematic hero and a box to collide the hero needs to mask for the box group and the box needs to mask for the hero group. It’s not enough to have a hero group reference box group or only box group to reference hero.

2 Likes

Yeah I need to figure out what is going wrong. I’ve tried what feels like everything but the groups/masks don’t behave as described in any of the tutorials. If I set any object group to something other than the default (unless I change all the objects to have the same group name) then no collision events are registered for that object even if I list the other objects in the mask for both of the colliding objects. Not sure why this is…

I’m sure you’re missing something very trivial. Why not start with an empty project, then add two game objects with collision components. The first game object is assigned group foo with mask bar. The second is assigned group bar with mask foo. Try that please because that is all there is to it. Collisions should be detected between the two objects.

1 Like

Hmm, I retested it just now. I did exactly the same thing as yesterday but now it’s working. Strange, not sure what happened :stuck_out_tongue:

Good to hear you got it working at least!

1 Like

Let’s not forget that you can also do such calculations yourself.
You have a point, and a circle.

-- translate the point into the space of the circle
local p = point - circle_pos
-- calculate the distance from the origin
-- a squared distance if sufficient here
local distsq = vmath.length_sqr(p)
-- compare with the squared radius of the circle
local inside = distsq <= (circle_radius * circle_radius)
4 Likes

I have a related question, but possibly a little harder to solve; I’ve started playing with a racing game prototype, where the track is made up of various parts. This is an example of two of the parts making up a track:

These are made up of collision objects with custom collision shapes:

I want to spawn pickups on the track, but not outside it, which means I will need a way to detect whether a point is inside the collision area or not. I’m thinking of using raycasts, but so far I haven’t come up with a foolproof way of dealing with the arbitrary nature of the shapes, without going too brute force-y.

Maybe there is a simpler way I’m missing?

Edit: I’ve decided to go for a more hack-ish approach: Spawning a collision object in a random spot, wait for the collision response to arrive, or reposition it each frame until it does. This means it could take a few frames for the pickups to appear, but this is kind of ok in my case.

1 Like

I think doing the “brute forcey” way during startup, once per piece is a fairly good way to do it. E.g. ray cast from outside into the center of the track piece.

Another way is to add empty game objects where potential spawn points are suitable.
E.g. “spawn1”, “spawn2” etc. Then you can check for a random position when you’ve spwned the track piece.
Remember, game objects are cheap.

4 Likes

Ah yes. Those are good options, thanks. Now I don’t know which approach to go for, darn! I think it’s coffee time.

3 Likes