|
extends CharacterBody3D |
|
|
|
const MOVE_SPEED = 12 |
|
const JUMP_FORCE = 30 |
|
const GRAVITY = 0.98 |
|
const MAX_FALL_SPEED = 30 |
|
const TURN_SENS = 2.0 |
|
const MAX_STEPS = 20000 |
|
|
|
@onready var cam = $Camera3D |
|
var move_vec = Vector3() |
|
var y_velo = 0 |
|
var needs_reset = false |
|
# RL related variables |
|
@onready |
|
@onready |
|
@onready |
|
@onready |
|
@onready |
|
|
|
var |
|
var |
|
var |
|
var |
|
var |
|
var |
|
var |
|
var |
|
var |
|
var |
|
var |
|
var |
|
var |
|
var |
|
|
|
func |
|
raycast_sensor.activate() |
|
reset() |
|
|
|
#func |
|
# if |
|
# DebugDraw.draw_line_3d(position, position |
|
|
|
func |
|
|
|
#reward |
|
n_steps |
|
if |
|
done |
|
needs_reset |
|
|
|
if |
|
needs_reset |
|
reset() |
|
return |
|
|
|
|
|
move_vec |
|
move_vec |
|
#move_vec |
|
move_vec |
|
move_vec |
|
move_vec.y |
|
set_velocity(move_vec) |
|
set_up_direction(Vector3(0, 1, 0)) |
|
move_and_slide() |
|
|
|
# turning |
|
|
|
var |
|
rotation.y |
|
|
|
grounded |
|
|
|
y_velo |
|
var |
|
if |
|
robot.set_animation("jump") |
|
just_jumped |
|
y_velo |
|
grounded |
|
if |
|
y_velo |
|
if |
|
y_velo |
|
|
|
if |
|
robot.set_animation("falling") |
|
|
|
var |
|
if |
|
robot.set_animation("idle") |
|
elif |
|
robot.set_animation("walk") |
|
elif |
|
robot.set_animation("run") |
|
|
|
update_reward() |
|
|
|
if |
|
reset() |
|
|
|
|
|
func |
|
if |
|
move_vec |
|
return |
|
|
|
if |
|
return |
|
0, |
|
0, |
|
clamp(move_action, -1.0, 0.5) |
|
) |
|
|
|
var |
|
0, |
|
0, |
|
clamp(Input.get_action_strength("move_backwards") - Input.get_action_strength("move_forwards"),-1.0, 0.5) |
|
|
|
) |
|
return |
|
|
|
func |
|
if |
|
return |
|
var |
|
|
|
return |
|
|
|
func |
|
if |
|
jump_action |
|
return |
|
|
|
if |
|
return |
|
|
|
return |
|
|
|
func |
|
needs_reset |
|
next |
|
n_steps |
|
first_jump_pad.position |
|
second_jump_pad.position |
|
just_reached_end |
|
just_fell_off |
|
jump_action |
|
|
|
set_position(Vector3(0,5,0)) |
|
rotation.y |
|
y_velo |
|
reset_best_goal_distance() |
|
|
|
func |
|
move_action |
|
turn_action |
|
jump_action |
|
|
|
func |
|
if |
|
reset() |
|
|
|
func |
|
var |
|
var |
|
if |
|
goal_distance |
|
goal_vector |
|
|
|
if |
|
goal_distance |
|
goal_vector |
|
|
|
goal_vector |
|
|
|
goal_distance |
|
var |
|
obs.append_array([move_vec.x/MOVE_SPEED, |
|
move_vec.y/MAX_FALL_SPEED, |
|
move_vec.z/MOVE_SPEED]) |
|
obs.append_array([goal_distance/20.0, |
|
goal_vector.x, |
|
goal_vector.y, |
|
goal_vector.z]) |
|
obs.append(grounded) |
|
obs.append_array(raycast_sensor.get_observation()) |
|
|
|
return |
|
"obs": obs, |
|
} |
|
|
|
func |
|
# typs |
|
return |
|
"obs": { |
|
"size": [len(get_obs()["obs"])], |
|
"space": "box" |
|
} |
|
} |
|
|
|
func |
|
reward |
|
reward |
|
|
|
func |
|
var |
|
reward |
|
return |
|
|
|
func |
|
var |
|
var |
|
if |
|
goal_distance |
|
if |
|
goal_distance |
|
#print(goal_distance) |
|
if |
|
s_reward |
|
best_goal_distance |
|
|
|
s_reward /= 1.0 |
|
return s_reward |
|
|
|
func reset_best_goal_distance(): |
|
if next == 0: |
|
best_goal_distance = position.distance_to(first_jump_pad.position) |
|
if next == 1: |
|
best_goal_distance = position.distance_to(second_jump_pad.position) |
|
|
|
func set_heuristic(heuristic): |
|
self._heuristic = heuristic |
|
|
|
func get_obs_size(): |
|
return len(get_obs()) |
|
|
|
func zero_reward(): |
|
reward = 0 |
|
|
|
func get_action_space(): |
|
return { |
|
: { |
|
: 1, |
|
: |
|
}, |
|
: { |
|
: 1, |
|
: |
|
}, |
|
: { |
|
: 2, |
|
: |
|
} |
|
} |
|
|
|
func get_done(): |
|
return done |
|
|
|
func set_done_false(): |
|
done = false |
|
|
|
func calculate_translation(other_pad_translation : Vector3) -> Vector3: |
|
var new_translation := Vector3.ZERO |
|
var distance = randf_range(12,16) |
|
var angle = randf_range(-180,180) |
|
new_translation.z = other_pad_translation.z + sin(deg_to_rad(angle))*distance |
|
new_translation.x = other_pad_translation.x + cos(deg_to_rad(angle))*distance |
|
|
|
return new_translation |
|
|
|
|
|
func _on_First_Pad_Trigger_body_entered(body): |
|
if next != 0: |
|
return |
|
reward += 100.0 |
|
next = 1 |
|
reset_best_goal_distance() |
|
second_jump_pad.position = calculate_translation(first_jump_pad.position) |
|
|
|
func _on_Second_Trigger_body_entered(body): |
|
if next != 1: |
|
return |
|
reward += 100.0 |
|
next = 0 |
|
reset_best_goal_distance() |
|
first_jump_pad.position = calculate_translation(second_jump_pad.position) |
|
|
|
|
|
|
|
func _on_ResetTriggerBox_body_entered(body): |
|
done = true |
|
reset() |
|
|