Skip to content

Commit 78c7a48

Browse files
jak-sdkaaronfranke
andcommitted
Re-write the 3d/navmesh demo to be easier to follow
The previous code had no comments and had some unintuitive steps. I've re-written and re-structured it, and added comments. Hopefully it will be easier to follow for newcomers to Godot. * Re-write main methods * Various bugfixes (variable conflicts, edge cases) * Comment improvements * Whitespace fixes Co-authored-by: Aaron Franke <[email protected]>
1 parent 31d54a3 commit 78c7a48

File tree

1 file changed

+59
-58
lines changed

1 file changed

+59
-58
lines changed

3d/navmesh/navmesh.gd

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ extends Navigation
33
const SPEED = 10.0
44

55
var camrot = 0.0
6-
7-
var begin = Vector3()
8-
var end = Vector3()
96
var m = SpatialMaterial.new()
107

118
var path = []
12-
var draw_path = true
9+
var show_path = true
10+
11+
onready var robot = get_node("RobotBase")
12+
onready var camera = get_node("CameraBase/Camera")
1313

1414
func _ready():
1515
set_process_input(true)
@@ -18,47 +18,54 @@ func _ready():
1818
m.albedo_color = Color.white
1919

2020

21-
func _process(delta):
22-
if path.size() > 1:
23-
var to_walk = delta * SPEED
24-
var to_watch = Vector3.UP
25-
while to_walk > 0 and path.size() >= 2:
26-
var pfrom = path[path.size() - 1]
27-
var pto = path[path.size() - 2]
28-
to_watch = (pto - pfrom).normalized()
29-
var d = pfrom.distance_to(pto)
30-
if d <= to_walk:
31-
path.remove(path.size() - 1)
32-
to_walk -= d
33-
else:
34-
path[path.size() - 1] = pfrom.linear_interpolate(pto, to_walk / d)
35-
to_walk = 0
36-
37-
var atpos = path[path.size() - 1]
38-
var atdir = to_watch
39-
atdir.y = 0
40-
41-
var t = Transform()
42-
t.origin = atpos
43-
t = t.looking_at(atpos + atdir, Vector3.UP)
44-
get_node("RobotBase").set_transform(t)
45-
46-
if path.size() < 2:
47-
path = []
48-
set_process(false)
49-
else:
50-
set_process(false)
21+
func _physics_process(delta):
22+
var direction = Vector3()
23+
24+
# We need to scale the movement speed by how much delta has passed,
25+
# otherwise the motion won't be smooth.
26+
var step_size = delta * SPEED
27+
28+
if path.size() > 0:
29+
# Direction is the difference between where we are now
30+
# and where we want to go.
31+
var destination = path[0]
32+
direction = destination - robot.translation
33+
34+
# If the next node is closer than we intend to 'step', then
35+
# take a smaller step. Otherwise we would go past it and
36+
# potentially go through a wall or over a cliff edge!
37+
if step_size > direction.length():
38+
step_size = direction.length()
39+
# We should also remove this node since we're about to reach it.
40+
path.remove(0)
41+
42+
# Move the robot towards the path node, by how far we want to travel.
43+
# Note: For a KinematicBody, we would instead use move_and_slide
44+
# so collisions work properly.
45+
robot.translation += direction.normalized() * step_size
46+
47+
# Lastly let's make sure we're looking in the direction we're traveling.
48+
# Clamp y to 0 so the robot only looks left and right, not up/down.
49+
direction.y = 0
50+
if direction:
51+
# Direction is relative, so apply it to the robot's location to
52+
# get a point we can actually look at.
53+
var look_at_point = robot.translation + direction.normalized()
54+
# Make the robot look at the point.
55+
robot.look_at(look_at_point, Vector3.UP)
5156

5257

5358
func _unhandled_input(event):
5459
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
55-
var from = get_node("CameraBase/Camera").project_ray_origin(event.position)
56-
var to = from + get_node("CameraBase/Camera").project_ray_normal(event.position) * 100
57-
var p = get_closest_point_to_segment(from, to)
60+
var from = camera.project_ray_origin(event.position)
61+
var to = from + camera.project_ray_normal(event.position) * 1000
62+
var target_point = get_closest_point_to_segment(from, to)
63+
64+
# Set the path between the robots current location and our target.
65+
path = get_simple_path(robot.translation, target_point, true)
5866

59-
begin = get_closest_point(get_node("RobotBase").get_translation())
60-
end = p
61-
_update_path()
67+
if show_path:
68+
draw_path(path)
6269

6370
if event is InputEventMouseMotion:
6471
if event.button_mask & (BUTTON_MASK_MIDDLE + BUTTON_MASK_RIGHT):
@@ -67,21 +74,15 @@ func _unhandled_input(event):
6774
print("Camera Rotation: ", camrot)
6875

6976

70-
func _update_path():
71-
var p = get_simple_path(begin, end, true)
72-
path = Array(p) # Vector3 array too complex to use, convert to regular array.
73-
path.invert()
74-
set_process(true)
75-
76-
if draw_path:
77-
var im = get_node("Draw")
78-
im.set_material_override(m)
79-
im.clear()
80-
im.begin(Mesh.PRIMITIVE_POINTS, null)
81-
im.add_vertex(begin)
82-
im.add_vertex(end)
83-
im.end()
84-
im.begin(Mesh.PRIMITIVE_LINE_STRIP, null)
85-
for x in p:
86-
im.add_vertex(x)
87-
im.end()
77+
func draw_path(path_array):
78+
var im = get_node("Draw")
79+
im.set_material_override(m)
80+
im.clear()
81+
im.begin(Mesh.PRIMITIVE_POINTS, null)
82+
im.add_vertex(path_array[0])
83+
im.add_vertex(path_array[path_array.size() - 1])
84+
im.end()
85+
im.begin(Mesh.PRIMITIVE_LINE_STRIP, null)
86+
for x in path:
87+
im.add_vertex(x)
88+
im.end()

0 commit comments

Comments
 (0)