Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use NavigationObstacle2D to handle collision avoidance between robots #7

Merged
merged 1 commit into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions assets/Scenes/Robot.tscn
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
[gd_scene load_steps=6 format=2]
[gd_scene load_steps=5 format=2]

[ext_resource path="res://assets/cdda_sprites/mon_eyebot_season_winter.png" type="Texture" id=1]
[ext_resource path="res://src/Robot.gd" type="Script" id=2]

[sub_resource type="CircleShape2D" id=3]
radius = 6.0

[sub_resource type="CircleShape2D" id=4]
radius = 9.0

[sub_resource type="CircleShape2D" id=2]
radius = 150.0

Expand All @@ -34,14 +31,6 @@ collision_mask = 4
width = 2.0
default_color = Color( 0.894118, 0.623529, 0.909804, 1 )

[node name="Collision_avoidance" type="Area2D" parent="."]
collision_layer = 8
collision_mask = 8

[node name="Collision_avoidance_Shape2D" type="CollisionShape2D" parent="Collision_avoidance"]
self_modulate = Color( 0.862745, 0.145098, 0.945098, 1 )
shape = SubResource( 4 )

[node name="Area2D" type="Area2D" parent="." groups=["Depot"]]
modulate = Color( 0.796078, 0.913725, 0.219608, 1 )
show_behind_parent = true
Expand All @@ -51,5 +40,8 @@ monitorable = false
shape = SubResource( 2 )

[node name="NavigationAgent2D" type="NavigationAgent2D" parent="."]
avoidance_enabled = true
radius = 12.0
neighbor_dist = 32.0

[connection signal="area_entered" from="Collision_avoidance" to="." method="_on_Collision_avoidance_area_entered"]
[node name="NavigationObstacle2D" type="NavigationObstacle2D" parent="."]
32 changes: 3 additions & 29 deletions src/Robot.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,26 @@ var grab_target: Vector2
var _home_depot_pos: Vector2
var _home_depot_area: Area2D

var _last_pos: Vector2
var _stuck_counter:= 0.0
var _heading: float

export var _step_size:= 40

onready var line:= $Line2D
onready var awarness_area:= $Area2D
onready var collision_avoidance_area:= $Collision_avoidance
onready var collision_shape:= $CollisionShape2D
onready var nav_agent:= $NavigationAgent2D

onready var map := get_node("../TileMap")

var STUCK_THRESHOLD:= 0.4

func _ready() -> void:
set_process(true)

nav_agent.connect("velocity_computed", self, "move")

var AI_core = get_tree().get_nodes_in_group("Core")[0]
_home_depot_area = AI_core.get_child(2)
_home_depot_pos = _home_depot_area.global_position

_last_pos = global_position
pick_heading()
new_path()

Expand Down Expand Up @@ -67,22 +63,12 @@ func _physics_process(_delta: float) -> void:
if _current_path.empty():
new_path()

if position.distance_to(_last_pos)<= 0.2:
_stuck_counter += _delta
else:
_stuck_counter = 0.0

_last_pos = global_position


var next_location= _current_path[0]
var direction:= position.direction_to(next_location)
var target_vel:= direction * _speed
move(target_vel)

#TODO: unstuck
if _stuck_counter > STUCK_THRESHOLD:
pass
nav_agent.set_velocity(target_vel)

if close_enough(next_location):
_current_path.remove(0)
Expand Down Expand Up @@ -137,17 +123,5 @@ func get_closest_ressource_in_awarness()->Body:
closest_body= body
return closest_body

func _on_Collision_avoidance_area_entered(area: Area2D) -> void:
var into_colllider= area.global_position - global_position
var tangent_out = into_colllider.tangent().normalized() * _body_size
var new_point = area.global_position + tangent_out
_current_path.insert(0,new_point)
line.add_point(new_point,0)


func _on_Collision_avoidance_area_exited(area: Area2D) -> void:
_current_path = make_path(_curr_dest)
line.points = _current_path

func is_robot()->bool:
return true