- Home /
Raycast Down with Approximation Search
I've got an approximation search and would like to ad from the BezierCurve Point the Point on the Ground. => but for correct Result the Point on the Ground must be calculated every Time on the approximation search (due to correct distances)
Approximation Search Code:
float step = 0.1f;
float t = step;
float lastT = new float();
float distance = 5;
/* Bezier.GetPoint(Vector3_A,Vector3_B,Vector3_C,Vector3_D,t) is a Vector3 Equation for a Point on the Bezier Curve */
while (t >= 0 && t <= 1f)
{
while (t < 1f && Vector3.Distance(Bezier.GetPoint(Vector3_A,Vector3_B,Vector3_C,Vector3_D,t), last_spawn) < distance){
t += step;}
step /= 100;
while (t > lastT && Vector3.Distance(Bezier.GetPoint(Vector3_A,Vector3_B,Vector3_C,Vector3_D,t), last_spawn) > distance){
t -= step;}
step /= 100;
if (t > 1f || t < lastT){
break;}
if(step < 0.000001f){
myList.Add(Bezier.GetPoint(Vector3_A,Vector3_B,Vector3_C,Vector3_D,t));
lastT = t;
step = 0.1f;
}
}
(Pseudo Code)
Now because the BezierLine is never really aligned with the Surface (Terrain) I need to get the Point on the Ground. If I now Add following Vector3.RaycastDown_Position();
to the Bezier.GetPoint(a,b,c,d,t);
Method => Bezier.GetPoint(a,b,c,d,t).RaycastDown_Position();
the Editor will freeze and get's Laggy!
Raycast Down Code
public static Vector3 RaycastDown_Position(this Vector3 t){
RaycastHit hit;
if(Physics.Raycast(t + (Vector3.up * 20),Vector3.down,out hit,Mathf.Infinity)){
t.y = hit.point.y;
}
return t;
}
Question:
What would be a better Approach to avoid Laggy Editor, or even Freezes?
if I add the RaycastDown Method after the approximation search, it'l cause inaccurate Result at the distance between the Calculated Points! thanks @Bunny83 for any suggestion =)
Little hard to tell what's going on, but seeing that many while loops dependent on Vector3.Distance seems like a recipe for performance issues.
See if using magnitude squared changes anything:
float sqrDistance = distance * distance;
while (t >= 0 && t <= 1f)
{
while (t < 1f && (Bezier.GetPoint(Vector3_A, Vector3_B, Vector3_C, Vector3_D, t), last_spawn).sqr$$anonymous$$agnitude < sqrDistance)
{
t += step;
}
step /= 100;
while (t > lastT && (Bezier.GetPoint(Vector3_A, Vector3_B, Vector3_C, Vector3_D, t), last_spawn).sqr$$anonymous$$agnitude < sqrDistance)
{
t -= step;
}
step /= 100;
// ...
}
well performance wise the code works fine => this question is a result out of [my previous][1] one.
i think the heavy part lies on the getpoint equation from the bezier, but it’s the only one which is a valid solution right now.
so getpoint.y = raycast down kills the performance, as mentioned above without the raycast all is fine [1]: https://answers.unity.com/questions/1599711/subdivide-bezier-curves.html
I mean, have you done any measurements to see how many raycasts are used during that block? It's hard to tell what the endgame looks like here.
Is it possible to draw a picture of what your current situation is and what you'd like the outcome to be?
I'm currently working on a Bezier based path-mesh building system and might be able to help. $$anonymous$$y only problem at this point is that i do not understand what you're trying to get out of this exactly.
A few questions:
What points on the terrain are you looking for?
Which conditions have to be met to get the correct point on the bezier curve?
Regarding performance: how does your Bezier.GetPoint() method look like? In there lies a big difference in performance, done correctly it will safe aprox. 65% of calculation time (hint: if you're using lerps in there then you will have problems with performance).
Am I correct in my assumption that your problem is an uneven spacing between the posts of the fence?
Is dynamic mesh generation off the table?
well lets have a chat a lt discord if you’re fancy, i made some big steps forward
i’m looking for points i calculate inside a subdivision of the whole conected bezier splines.
1. the only condition is that the distance between the posts are the same as the palings, which gonna be spawned after calculation of the points.
i use de casteljau’s get point method
//GetPoint equation: public static Vector3 GetPoint (Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) { t = $$anonymous$$athf.Clamp01(t); float One$$anonymous$$inusT = 1f - t; return One$$anonymous$$inusT One$$anonymous$$inusT One$$anonymous$$inusT p0 + 3f One$$anonymous$$inusT One$$anonymous$$inusT t p1 + 3f One$$anonymous$$inusT t t p2 + t t t p3; }
yes you are right. i had problems with that
no it isnt, working right now on that, and proper pivot adjustment for premade props
Answer by dan_wipf · Mar 14, 2019 at 09:20 PM
I did now some research about getting / placing objects on Terrain, with alot of Calls for height!
Raycast Down method
This Method is good to use on single objects, and returns aswell the normal’s for Rotation, align objects on Ground. Performance wise, it’s not recomended for alot of calls like 10000 and more => raycastcommand can be fazter, but still bot recomended.
2D float[ , ] GetHeights Method
Search a 2D array with y and x worldposition for a height. Can be used with the job’s system => but generally slow => just refresh the 2Darray when Terrain has changed, or performance is beyond good and evil.
SampleHeight / Current in use, Best Solution
i use this now, it’s realy fast to get the height on a Vector3 Position, even with 50000 and more calls. just need to be sure that the calls are not every frame, only on change of the bezier or other major changes, which affect the position and the current height
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to get the distance between two objects in feet/meter? 1 Answer
Moving a 3D object with 2D swipes 2 Answers
Smooth Movement on Geometry Collission? 0 Answers
Raycast from an objects co-ordinates 1 Answer