Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do you check that an object wouldn't be intersecting with another object?

Asked by 4 years ago

I am making a script that spawns tree models on a platform - the objective is for the script to spawn as many trees (in random positions) without the trees intersecting any other trees. For the spawning, I used cframes and :SetPrimaryPartCFrame() - (I used :MoveTo before, but I had trouble with that shifting the trees upward.)

Currently, the script spawns fine, but I'm not sure how'd I'd check if any of the other trees would be intersecting. And you'd think that "oh, use a touched event" or something. The problem is I don't want to put the tree model in the workspace until I know it won't intersect any other trees. The touched event probably requires the tree to be in the workspace.

I can't have a tree in the game temporarily because I have another script that detects trees as soon as added and does things with them - if the tree is temporarily there (even for 0.1 second) and gets instantly deleted, my script may try to use that tree.

Yes, I know there are many easy ways to go around this, but most of them are really messy.

I have a part with all the other trees inside of it. I'm trying to make it so, before inserting the tree into the game, the script checks if it would intersect any other trees.

I was thinking maybe a for loop and some heavy math conditions could cut it, but that would also be pretty big. Are there any easier ways to detect whether an object would be intersecting another?

0
There are lots of options, depending on what you consider "intersecting". If your trees have a reasonably circular footprint, and it's acceptable for them to be no nearer to each other than you'd get from circle packing, the check comes down to just simple radius checks against the would-be neighbors. EmilyBendsSpace 1025 — 4y
1
If you want collision geometry level of precision, temporarily placing an invisible tree is by far the easiest solution. EmilyBendsSpace 1025 — 4y
1
Yeah I am not reading all that. Easy fix, use GetTouchingParts in an event. greatneil80 2647 — 4y

1 answer

Log in to vote
0
Answered by
sheepposu 561 Moderation Voter
4 years ago

I can think of an easy way using the touch event. Before adding the tree and using a touch event to detect touching, make it transparent, or you could have a bool value inside it named "Testing" and set it to true. When the other script is messing with trees, have it first check whether the tree is transparent or the value of "Testing", whichever you do, and use that value to decide whether or not it should mess with the tree. If you have any questions, just comment.

0
may I ask why use a touched event instead of magnitude? Despayr 505 — 4y
0
I thought touched events only fire when physics causes two parts to touch, not when one is teleported into another? GetTouchingParts would probably be more apt Ankur_007 290 — 4y
Ad

Answer this question