Trail Collision Detection

o ok
but i mean if what you are doing works for your situation you shouldn’t really do any extra unless you are doing it for yourself to learn / to use in the future idk because as my friend’s mom always says, don’t fix something if it isnt broken (i speak from experience with this)

Well, I definitely need this collision detection to be as optimal as possible, so I will keep adjusting it until it is the best I can get.

On a side note… Any idea why HumanoidRootPart:GetPropertyChangedSignal(“Position”):Wait() yields infinitely (even when moving), unless I change it manually through the Properties window?

changed only fires when it is changed from a script (so your own scripts or roblox’s scripts whenever they want / on lua side idk) and i guess roblox doesnt want to fire changed for position since it changes so often that it would be inefficient
at least thats why motor6d.Transform doesnt fire changed - even when changing from your own lua script i think

1 Like

Hmph, so I’ll nest this in a while wait() do loop and constantly check for position changes, I suppose

1 Like

I am in no way a scripter but I am HYPED!

a faster way would be to use a hashtable (roblox dictionary) or your own implementation of a list

So upon converting the Trail.Parts table to use a dictionary instead of an array, FindPartsInRegion3WithWhitelist no longer returns any parts. It seems the function does not support dictionaries, only arrays. Any ideas?

with FindPartsInRegion3WithWhitelist make the whitelist be a folder and parent all the trailer parts to that folder

1 Like

Ah, smart. I’ve already got them going into a folder anyways :cool:

I’m going to be honest here, using something very heavy like FindPartsInRegion3WithWhiteList every frame is a very bad idea.

Here’s a way to find if it intersects with the trails:

Store each trail as a {Origin Vector3, Lookvector Vector3, Length Number, Width Number} in a table called Trails.

Loop through each trail and do:

local RelativeLength = (Position-Trail.Origin):Dot(Trail.Lookvector) --Position being the head
if RelativeLength > 0 and RelativeLength < Trail.Length then
 if (RelativeLength*Trail.Lookvector-Position).magnitude < Trail.Width then
  return true --There is collision
 end
end

The above is fast cylinder collision detection and below is slower capsule collision detection.

local RelativeLength = (Position-Trail.Origin):Dot(Trail.Lookvector) --Position being the head
RelativeLength = (RelativeLength<0 and 0) or (RelativeLength>Trail.Length and Trail.Length) or RelativeLength --This method is 3 times faster than math.clamp
if (RelativeLength*Trail.Lookvector-Position).magnitude < Trail.Width then
 return true --There is collision
end
4 Likes

What happens if the trails aren’t straight segments?

Also FindPartsInRegion3WithWhiteList is O(lg N + k) in complexity with N being number of trail parts and k being output size (1 in this scenario)

That is PRETTY FAST

How can they not be straight segments? Even Roblox’ Trail object uses straight segments.

A*O(log(N)+k) and B*O(N) and here in this case A is much bigger than B. I think my method would be faster if you don’t put more than 10 thousand objects and at that point probably both of the equations would lag too much anyway. I suppose you would want to run this on mobile. There is also the point of FindPartsInRegion3 being not actually an accurate way of collision detection since the region orientation is fixed. There is also the point of you actually creating and deleting those parts which is a cause of lag on its own. With my method, you wouldn’t need to put any parts.

1 Like

My bad about the trail segments, I thought you were storing one segment per trail not multiple segments

idk I still think it might be difficult to get good segments though because of non straight player movement and you run the risk of it turning into OP’s scenario 2 (perhaps even more inefficient though because of the math you are doing)

For your collision detection you can use region3 to get an estimate and then something more specific to get more accurate collision detection (although your k would increase) - but you have flexibility with this solution and you can tune it to whatever is optimal for your use case, idk if you really have that for an inherent O(n) solution

Yes it’s nearly the same as scenario 2 but since we’re checking a whole segment not points, you can just listen to it every 0.5 seconds or something and create the segments requiring much less calculations total.

Actually you don’t have to listen to it. Just do previous position to the new position that’s already known. You don’t actually have to send trail info as it is already available to you by knowing just the current position of all the other characters which Roblox already makes readily available.

If a player moves zig zag it will be practically like solution 2 unless you want to completely lose accuracy

I’m not sure what you mean, you still have to look back at previous trail segments that haven’t expired (been in existance for longer than trail.lifetime)

The whole point of it is to get rid of region3.

I’m trying to say that you don’t have to fire remote events for trials at all.

sorry I meant for the region3 idea, you use region3

why would you need remote events for any scenario?

You don’t mean using remote events to send player position for trails, right?

I mean I would imagine either the server handling collisions and telling clients or clients handling their own collision and telling server (telling = sending through remote events)

But that is only when there is a collision and would be the exact same as your scenario

Checking position would obviously not be done via remote events lol

Alright yeah, that’d be the good way to do it. There’s also the fact that you can combine segments. While adding a new segment, check if their lookvectors are very close (product the two) to each other and if they are don’t create a new entry but just update their tick time and length. Even though you would lose disappearance accuracy doing it maximum of twice per segment wouldn’t cause any trouble and it might even give you a 20% increase in performance on average.

1 Like