Datasets:
File size: 6,646 Bytes
5de14c4 b2f36c3 5de14c4 685f518 5de14c4 685f518 5de14c4 685f518 5de14c4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
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 var end_position = $"../EndPosition"
@onready var raycast_sensor = $"RayCastSensor3D"
@onready var first_jump_pad = $"../Pads/FirstPad"
@onready var second_jump_pad = $"../Pads/SecondPad"
@onready var robot = $Robot
var next = 1
var done = false
var just_reached_end = false
var just_reached_next = false
var just_fell_off = false
var best_goal_distance := 10000.0
var grounded := false
var _heuristic := "player"
var move_action := 0.0
var turn_action := 0.0
var jump_action := false
var n_steps = 0
var _goal_vec = null
var reward = 0.0
func _ready():
raycast_sensor.activate()
reset()
#func _process(_delta):
# if _goal_vec != null:
# DebugDraw.draw_line_3d(position, position + (_goal_vec*10), Color(1, 1, 0))
func _physics_process(_delta):
#reward = 0.0
n_steps +=1
if n_steps >= MAX_STEPS:
done = true
needs_reset = true
if needs_reset:
needs_reset = false
reset()
return
move_vec *= 0
move_vec = get_move_vec()
#move_vec = move_vec.normalized()
move_vec = move_vec.rotated(Vector3(0, 1, 0), rotation.y)
move_vec *= MOVE_SPEED
move_vec.y = y_velo
set_velocity(move_vec)
set_up_direction(Vector3(0, 1, 0))
move_and_slide()
# turning
var turn_vec = get_turn_vec()
rotation.y += deg_to_rad(turn_vec*TURN_SENS)
grounded = is_on_floor()
y_velo -= GRAVITY
var just_jumped = false
if grounded and get_jump_action():
robot.set_animation("jump")
just_jumped = true
y_velo = JUMP_FORCE
grounded = false
if grounded and y_velo <= 0:
y_velo = -0.1
if y_velo < -MAX_FALL_SPEED:
y_velo = -MAX_FALL_SPEED
if y_velo < 0 and !grounded :
robot.set_animation("falling")
var horizontal_speed = Vector2(move_vec.x, move_vec.z)
if horizontal_speed.length() < 0.1 and grounded:
robot.set_animation("idle")
elif horizontal_speed.length() < 1.0 and grounded:
robot.set_animation("walk")
elif horizontal_speed.length() >= 1.0 and grounded:
robot.set_animation("run")
update_reward()
if Input.is_action_just_pressed("r_key"):
reset()
func get_move_vec() -> Vector3:
if done:
move_vec = Vector3.ZERO
return move_vec
if _heuristic == "model":
return Vector3(
0,
0,
clamp(move_action, -1.0, 0.5)
)
var move_vec := Vector3(
0,
0,
clamp(Input.get_action_strength("move_backwards") - Input.get_action_strength("move_forwards"),-1.0, 0.5)
)
return move_vec
func get_turn_vec() -> float:
if _heuristic == "model":
return turn_action
var rotation_amount = Input.get_action_strength("turn_left") - Input.get_action_strength("turn_right")
return rotation_amount
func get_jump_action() -> bool:
if done:
jump_action = false
return jump_action
if _heuristic == "model":
return jump_action
return Input.is_action_just_pressed("jump")
func reset():
needs_reset = false
next = 1
n_steps = 0
first_jump_pad.position = Vector3.ZERO
second_jump_pad.position = Vector3(0,0,-12)
just_reached_end = false
just_fell_off = false
jump_action = false
set_position(Vector3(0,5,0))
rotation.y = deg_to_rad(randf_range(-180,180))
y_velo = 0.1
reset_best_goal_distance()
func set_action(action):
move_action = action["move"][0]
turn_action = action["turn"][0]
jump_action = action["jump"] == 1
func reset_if_done():
if done:
reset()
func get_obs():
var goal_distance = 0.0
var goal_vector = Vector3.ZERO
if next == 0:
goal_distance = position.distance_to(first_jump_pad.position)
goal_vector = (first_jump_pad.position - position).normalized()
if next == 1:
goal_distance = position.distance_to(second_jump_pad.position)
goal_vector = (second_jump_pad.position - position).normalized()
goal_vector = goal_vector.rotated(Vector3.UP, -rotation.y)
goal_distance = clamp(goal_distance, 0.0, 20.0)
var obs = []
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 get_obs_space():
# typs of obs space: box, discrete, repeated
return {
"obs": {
"size": [len(get_obs()["obs"])],
"space": "box"
}
}
func update_reward():
reward -= 0.01 # step penalty
reward += shaping_reward()
func get_reward():
var current_reward = reward
reward = 0 # reset the reward to zero checked every decision step
return current_reward
func shaping_reward():
var s_reward = 0.0
var goal_distance = 0
if next == 0:
goal_distance = position.distance_to(first_jump_pad.position)
if next == 1:
goal_distance = position.distance_to(second_jump_pad.position)
#print(goal_distance)
if goal_distance < best_goal_distance:
s_reward += best_goal_distance - goal_distance
best_goal_distance = 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 {
"move" : {
"size": 1,
"action_type": "continuous"
},
"turn" : {
"size": 1,
"action_type": "continuous"
},
"jump": {
"size": 2,
"action_type": "discrete"
}
}
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()
|