Godot Version
4.2
Question
Hi everyone, i followed this great Tutorial by Cashew Oldew:
Simplified 2D Grid PATHFINDING in Godot 4.2 (youtube.com)
Trying to make the movement more complex changed the speed at the line:
Current:
global_position = global_position.move_toward(targetPosition, speed)
Original (video):
global_position = global_position.move_toward(targetPosition, 5)
The ‘var speed’ changes when the current position is not equal as the starting position, but the diagonal movement speed is 1.5 times faster than the vertical and horizontal speed, so, tried to normalize the Vector2 move_toward function but it does not appear to work.
Tried: direction_to func but it didnt work neither.
Is it possible to normalize the Vector2 move_toward func?
Have a great day!
Did you try:
global_position = global_position.move_toward(targetPosition, speed).normalized()
?
I’m not sure if we share the same definition of “normalized”. Because the output of move_toward
function already is normalized!
position = Vector2.ZERO
position = position.move_toward(position + Vector2(1, 1), 1)
print(Vector2(1, 1).normalized()) # (0.707107, 0.707107)
print(position) # (0.707107, 0.707107)
Maybe give an example of what happens and what you expect to happen instead?
1 Like
Oh okay I think I got your point - see vector clamp function in documentation - should help to limit vector length.
Oooh, thx for your comments, I found out the true problem, you are right, it is normalized, but the true problem is the distance the object is moving (horizontal = 1.0, diagonal movement = 1.5 ), in the way it is coded the object just “slide” on screen from tile to tile, and i want to be more like “step by step” movement reducing it’s speed on every tile.
So, the not so fancy solution i found (hope you can help me to find a better approuch) was coding the speed like this:
###Code
func updateSpeed():
currentPosition = global_position
if speed >= maxSpeed:
speed = maxSpeed
if speed <= minSpeed:
speed = minSpeed
if currentPosition == startingPosition:
speed = maxSpeed
elif currentPosition.x != startingPosition.x and currentPosition.y == startingPosition.y:
speed -= 0.0655
elif currentPosition.x == startingPosition.x and currentPosition.y != startingPosition.y:
speed -= 0.0655
elif currentPosition.x != startingPosition.x and currentPosition.y != startingPosition.y:
speed -= 0.045
So the horizontal and vertical movement are decelerated by 0.0655 (less distance) and the diangonal movement was decelerated by 0.045 (greater distance). It looks fine but obviously is not the best solution.
1 Like
It’s not clear from your example where and how often you call updateSpeed()
, or where the startingPosition
is updated, but I assume you call it in _process
and startingPosition
is set to the newly reached point every time you reach a point in the path?
A simpler solution would be to just set the speed based on the distance to the goal:
speed = clamp(global_position.distance_to(target_position), min_speed, max_speed)