Up to date

This page is up to date for Godot `4.2`. If you still find outdated information, please open an issue.

# AStar2D¶

A* 的一种实现，用于查找 2D 空间中连通图上两个顶点之间的最短路径。

## 描述¶

A* 算法的一种实现，用于在 2D 空间中的连通图上找到两个顶点之间的最短路径。

## 方法¶

 float _compute_cost ( int from_id, int to_id ) virtual const float _estimate_cost ( int from_id, int to_id ) virtual const void add_point ( int id, Vector2 position, float weight_scale=1.0 ) bool are_points_connected ( int id, int to_id, bool bidirectional=true ) const void clear ( ) void connect_points ( int id, int to_id, bool bidirectional=true ) void disconnect_points ( int id, int to_id, bool bidirectional=true ) int get_available_point_id ( ) const int get_closest_point ( Vector2 to_position, bool include_disabled=false ) const Vector2 get_closest_position_in_segment ( Vector2 to_position ) const PackedInt64Array get_id_path ( int from_id, int to_id ) int get_point_capacity ( ) const PackedInt64Array int get_point_count ( ) const PackedInt64Array PackedVector2Array get_point_path ( int from_id, int to_id ) Vector2 get_point_position ( int id ) const float get_point_weight_scale ( int id ) const bool has_point ( int id ) const bool is_point_disabled ( int id ) const void remove_point ( int id ) void reserve_space ( int num_nodes ) void set_point_disabled ( int id, bool disabled=true ) void set_point_position ( int id, Vector2 position ) void set_point_weight_scale ( int id, float weight_scale )

## 方法说明¶

float _compute_cost ( int from_id, int to_id ) virtual const

float _estimate_cost ( int from_id, int to_id ) virtual const

void add_point ( int id, Vector2 position, float weight_scale=1.0 )

```var astar = AStar2D.new()
astar.add_point(1, Vector2(1, 0), 4) # 添加点 (1, 0)、权重为 4、ID 为 1
```

bool are_points_connected ( int id, int to_id, bool bidirectional=true ) const

void clear ( )

void connect_points ( int id, int to_id, bool bidirectional=true )

```var astar = AStar2D.new()
astar.connect_points(1, 2, false)
```

void disconnect_points ( int id, int to_id, bool bidirectional=true )

int get_available_point_id ( ) const

int get_closest_point ( Vector2 to_position, bool include_disabled=false ) const

Vector2 get_closest_position_in_segment ( Vector2 to_position ) const

```var astar = AStar2D.new()
astar.connect_points(1, 2)
var res = astar.get_closest_position_in_segment(Vector2(3, 3)) # 返回 (0, 3)
```

PackedInt64Array get_id_path ( int from_id, int to_id )

```var astar = AStar2D.new()
astar.add_point(2, Vector2(0, 1), 1) # 默认权重为 1

astar.connect_points(1, 2, false)
astar.connect_points(2, 3, false)
astar.connect_points(4, 3, false)
astar.connect_points(1, 4, false)

var res = astar.get_id_path(1, 3) # 返回 [1, 2, 3]
```

int get_point_capacity ( ) const

PackedInt64Array get_point_connections ( int id )

```var astar = AStar2D.new()

astar.connect_points(1, 2, true)
astar.connect_points(1, 3, true)

var neighbors = astar.get_point_connections(1) # 返回 [2, 3]
```

int get_point_count ( ) const

PackedInt64Array get_point_ids ( )

PackedVector2Array get_point_path ( int from_id, int to_id )

Vector2 get_point_position ( int id ) const

float get_point_weight_scale ( int id ) const

bool has_point ( int id ) const

bool is_point_disabled ( int id ) const

void remove_point ( int id )

void reserve_space ( int num_nodes )

void set_point_disabled ( int id, bool disabled=true )

void set_point_position ( int id, Vector2 position )

void set_point_weight_scale ( int id, float weight_scale )