Best Approach to Detecting Multiple Collisions on an Object

I’m working on a custom physics engine for small, basic objects, and set up a ray cast into my update function (fired every RunService.Stepped) to detect if the object is on a surface or not, and use that to either continue its current motion or go into free fall.

The problem with this is that when the object slides off a surface, its fall looks unnatural since the ray is cast from the center of the object, so it doesn’t start falling until the center of the object slides off the surface, and when it does it just falls straight down.

To make this more realistic I need to make it tip over in the direction it’s falling, and I don’t know if firing a ray cast for each side of the object (to determine which side is going over the edge) every frame is the optimal way of going about this. Especially if I have a disc object, then I’d need 8 ray casts every frame: one for each side and one for each diagonal.

And on top of that: to detect collisions on the top and sides of the object, I would need even more ray casts in those directions, too.


So basically what I’m asking is how many ray casts can you safely fire each frame before performance is significantly affected? because I feel like having around 13 ray casts for an object every frame is excessive. And how else could I go about figuring out in which direction the objects must tip over and when, as well as when it collides with other objects on its sides?

I don’t think the ray being at the center would be a problem, not many masses will fall over until more than 50% of it’s base is over an edge, so that seems resonable to me.

Find the direction the part is heading in when the raycast hit changes (presuming at an edge), give it a little bump from behind to ensure it’s hanging over the edge, just over 50% (51-60%ish), then let the Roblox physics take control (even for just a few s or ms) it should tip over and start to fall as expected.

Material types/friction/mass/force applied may need to be tweaked/adjusted unitl you find that sweet spot where it “tips over” nicely.

Oh yeah, I forgot about center of mass lol. In my head I assumed that any object would immediately tip over if a fraction of its body is over the edge.

Oh, and I forgot to ask: would it be optimal to have additional rays on the sides of the objects to detect if it collides with other objects? That would just be four additional raycasts per frame.

It all depends on how many objects are going to be active, doing raycasts at once. A couple objects, no big deal, 100+ could get rough, 500+ not likely to go very well. This all really depends on the scene and what else is going on but in general try to keep them to as few as possible. I can’t tell you exactly how your game will be affected, you’ll just have to see if it works resonably with what you have.

You could do something like, only fire the rays when the part has a velocity, otherwise disable the rays on that part. That should let you get away will having more potential rays without having to have them all active at once.

if part.AsseblyLinearVelocity > Vector3.new(0.01, 0.01, 0.01) then --you'll have to compare each vector3 value vs the whole vector3 but same idea.
    --fire raycasts
else
    --disable raycasts
end
1 Like

Thanks. Luckily there’s only one active part moving at a time so I think I should be good

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.