add a point X pixel away on a get_simple_path and get it position ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By quizzcode

Hey there !

I’m working on a tower defense game. One of my tower’s power is the ability to potentially “teleport” back on it path a mob.

Here is my process :
When the ability happen, i do a get_simple_path from self.gloal_position to Start_spawn.
– Great, on hit, the mob walk all the way back —

now i want to add a point X pixel away on that path.
Then i want to get the global_position of that point. (Those 2 steps are the one I can’t figure out)
and then, i will reposition my enemy, and re-path his way from self.global_position to end_path.

I posted before, got some infos regarding offset and add_point but I can’t seem to figure out how to make it work.
I’m looking for some help to formulate the process has i has been 2 days and I feel completely stuck on it.
Not able to find any example or even a hint on how to build it.

every answer / input is appreciated :slight_smile:

ps : here is a pic recap :

:bust_in_silhouette: Reply From: Andrea

I suppose you are using a navigation2D node to get the simple path.
If i recall correctly, get_simply_path returns an array of points that are the edges of the path.
You could go through those points and calculate a distance equal to X=300.

eg:

var final_point=Vector2()  
var path=navigation2d.get_simple_path() 
var i=path.size()-1    
var X2=0.0        #distance to the point i     
var X1=0.0   #distance to the point i-1
while i>0:
  X2+=(path[i]-path[i-1]).length()  
  if X2>=300 and X1<300:                                        
      final_point=path[i]*(300-X1)/(X2-X1)+path[i-1]*(X2-300)/(X2-X1)
      break
  X1=X2
  i-=1

Hey Andres, thanks for your message. Yes i use a navigation2D node.
From what I understand (i will try your code in the morning, its 3am here) this “look” for a point within 300 pixel.

Sometime the path is only 3 points. So the value of X might not match.
Have you heard of the offset value of get simple path ? it works with pixel. but don’t know how to use it…

quizzcode | 2020-05-26 10:30

Exactly, the code check on the path and “look” for a point at 300 pixel distance from the end.
I think it works for any array with >2 points (which i assume is the minimum for a get_simple_path). If the values of X2 never exceed 300, it means the the lenght of the path from start to end is <300, in that case is obviously impossible to get a point at 300 distance (and the final_point returns an empty vector)

Never heard of the offseet value sorry

Andrea | 2020-05-26 10:53

Yes ! This actually make sense !
I added a verification to check that if vecto2.x and .y == 0 i simply reposition from the start.
Thank you, this is super helpful i did not think of using length()

quizzcode | 2020-05-26 17:06