- Home /
Difficult path logic
I am attempting to create a procedural path from point A to point B. In the end I want it to be a spline, but right now I'm just trying to generate the points at least (I know splines are a big project, beyond the scope of a this thread).
It will be a Harry Potter vs. Voldemort wand dueling type of effect. Random lightning is one thing, but this has some very specific requirements.
Requirements
- The path will be a minimum length from point A to point B; even if A and B are closer than minimum distance.
The path will not pass through obstacles.
The path will not follow the terrain (random height is okay) and will not pass into terrain.
Here is an diagram a created to illustrate:
I have been trying to figure this one out for weeks, please let me know if you have any advice. Thanks!
Answer by vazor · Mar 12 at 08:46 PM
One way to approach something like this is to start with the two data points, add in a customizable parameter for step_length, and then labels or layers to tell when you are colliding with an obstacle.
Then, you have a function that plots the points. If your wiggle design in the second example is meant to be an oscillating wave of some kind, you can do a trig transform to determine the direction. So for the first step through your code loop, you would subtract points B-A to get a vector from a to b. Then scale it down to the step_length. Then rotate it up or down based on where you are in the total length (step_length times iterations count divided by distance between A and B). That gives you one small step along your ideal path.
Next, you check your new point to see if it collides with the ground or an obstacle. You can usually use a collision test API for this, along with layers/labels to only collide with the right things. If you don't collide with anything, great, save that point in your path data and move to the next iteration. If you DO collide with something you need to avoid, then you need to modify your rotation up or down until it is no longer colliding. You can do a ray test or just adjust it by some amount and keep rechecking until it is clear. This is the very basic approach and is prone to all sorts of edge cases and bugs. A good collision avoidance algorithm or a navigation mesh can help.
But in general that is the approach I would take. I'm sure there are other options though. Good luck!
Thanks for the answer!
That sounds like a great approach. How would you use it to maintain a consistent path length regardless of distance to target? So if the target is 5 meters away, the path will still be 10 meters long.
That depends on what you settle on as your wiggle design. For a trig function, you can simply scale up the amplitude e.g. to make a bigger sine wave. You could also try using a per-step weight that adjusts your up/down rotations to be larger based on the ratio of target length vs A-B distance length. A final thought might be to use physics systems- I imagine a library that handles spring joints might work, and then you can apply some forces up and down to the 1/4 and 3/4 joints along the chain to get a wiggle pattern. This physics approach might be more appropriate if the fixed length is more important than a smooth curved shape.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Hint joint how to configure 0 Answers
Hint Joint like a rope have weird behavior 1 Answer
Procedural Terrain - Diamond Square Algorithm 1 Answer
Generate FTL style star map 0 Answers