0 votes

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 getsimplepath from self.gloalposition to Startspawn.
-- 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 globalposition 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 :)

ps : here is a pic recap :
enter image description here

asked May 26 in Engine by quizzcode (152 points)

1 Answer

+1 vote
Best answer

I suppose you are using a navigation2D node to get the simple path.
If i recall correctly, getsimplypath 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
answered May 26 by Andrea (520 points)
selected May 26 by quizzcode

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....

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

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()

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.