#Given a path (Array of arrays), a Path2D - recover the last and next node in the path from Path2D

43 messages · Page 1 of 1 (latest)

austere galleon
#

In my game, we have

        var path_follow = path_2d.get_child(0)
        var curve = path_2d.curve
        
        # Check if Path2D has more than one node
        if curve.get_point_count() > 1:
            # Move the PathFollow2D along the path based on the creature's velocity
            path_follow.offset += enemy['velocity'] * delta
            
            # Update the creature's position to match the PathFollow2D position
            enemy['pos'] = path_follow.global_position

To move a creature and:

func update_path2D(path_2d, new_path, enemy):
    # Clear the existing points in the Curve2D
    path_2d.curve.clear_points()
    
    # Add the new path points to the Curve2D
    for pos in new_path:
        path_2d.curve.add_point(Vector2(pos[0], pos[1]))
    
    # Reset the PathFollow2D offset
    path_2d.get_child(0).offset = 0
    
    # Store the path's baked length as a variable
    enemy['path_follow_baked_length'] = path_2d.curve.get_baked_length()

To update the creature's Path2D according to a grid-based array of positions new_path like

new_path = [ [0,0], [0,1], [0,2], [1,3] ]

Note: we get new_path from running A*

I need a cheap way to check the previous/last node in new_path from the creature's path_2d variable.

The goal is to basically change the creature's direction (enemy['dir']) every time a new node is reached, without having to constantly check _process at every update (which is very expensive and lags the game).

#

path_2d - the path in light blue
new_path - green and red grid nodes
grid - the blue nodes

Want: the red nodes in new_path (these are the previous and next nodes)

#

Need an easy way to keep track of that, so that every time a new node is reached we can just change the creature's direction (in this case it would be "E") without having to compute it in _process at every update

#

Note: the creature must follow the path_2d to move (can't just move_and_slide the creature from one node to the next)

inner pagoda
#

PathFollow2d has a rotate property does this do what you want?

#

Oh I think I see. You have a separate variable you want to track the rotation in as well as the rotation of the pathfollow2d.
I don't think the nodes really exist for the pathfollow2d it bakes its own points as described in the docs. It might not even be exactly on an angle corner node depending on where its points are.

austere galleon
#

The Path2D methods that I found like get_closest_point, get_closest_offset, interpolate etc are really CPU intensive

mental beacon
#

Are you able to simply state exactly what you want in a sentence or two ? I'm struggling to understand.

austere galleon
#

@mental beacon Creature needs to walk along a grid-based A* path (new_path) smoothly. I'm using a Path2D to make that happen. Now I also need the cardinal direction the creature is moving along

#

Need the red arrows as well

#

And most importantly need to be able to detect when a change in direction occurs

mental beacon
#

If you've got rotation set to true, you could check the nodes rotation

austere galleon
#

Wdym rotation set to true ?

mental beacon
austere galleon
#

The best I can think of is , say, for a path that is 3 nodes long (not including the initial node)

#

Sampling get_unit_offset() in _process

#

So say, when
get_unit_offset() ~ 0.33
get_unit_offset() ~ 0.66
I'll change the direction

mental beacon
#

You could. Why not set rotation to true and store the nodes rotation at the end of the process method and check it against its current rotation at the start of the process method and you can detect rotation changes and also get the rotation value

austere galleon
#

Oh I think I get it

#

So if rotate is set to true in pathfollow2d

#

This somehow stores the entity's rotation ?

mental beacon
#

It changes the entity's rotation based on the points in the path it's following AFAIK

#

So it's all calculates for you

austere galleon
#

I see

#

Well it's hard to parse what they mean in the docs haha

mental beacon
#

Otherwise, you could do the method you explained, but it would require more code

austere galleon
#

So something like pathfollow2d.is_rotating() should return the actual rotation ?

mental beacon
#

is_rotating() returns a bool. You would just check the nodes rotation property

inner pagoda
#

Nope. The pathfollow2d node should be rotated.

mental beacon
#

That statement in the doc is saying it rotates the node (and all of its children) for you

austere galleon
#

I see

mental beacon
#

Based on the point in the path it's moving towards

inner pagoda
#

It would not rotate the children directly. Children inherit the rotation of their parent. You can check the global rotation of the child but the local rotation would not change.

mental beacon
#

Yes, sorry, that is what I meant

austere galleon
#

Yes if the nodes store local rotation with respect to the parent

#

You would need to keep track of the previous rotation to get global rotation

#

Or like, just add the node's rotation to the direction vector most likely

#

I think I understand what this data structure does

#

There is this lookahead float property which allows you to control how fast you want to calculate the tangents