-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.gd
125 lines (95 loc) · 2.22 KB
/
Player.gd
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
extends CharacterBody3D
const SPEED = 10
const JUMP_VELOCITY = 4.5
const MAXIMUM_FALL_SPEED = 10
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var move_vector = Vector3()
var done = false
var move_action = 0.0
var y_velocity = 0
var _heuristic = "model"
var number_of_steps = 0
const MAX_STEPS = 10000
var needs_reset = false
var reward = 0.0
@onready var second_platform = $"../SecondPlatform"
var best_goal_distance =5000
func shaping_reward():
var shaping_reward =0.0
var goal_distance =0
goal_distance = position.distance_to(second_platform.position)
if goal_distance < best_goal_distance:
shaping_reward += best_goal_distance - goal_distance
best_goal_distance = goal_distance
shaping_reward /= 1.0
return shaping_reward
func update_reward():
reward -= 0.01
reward += shaping_reward()
func get_reward():
var current_reward = reward
reward = 0
return current_reward
func get_move_vector() -> Vector3:
if done:
move_vector = Vector3.ZERO
return move_vector
return Vector3(
0,
0,
clamp(move_action, -1.0, 1.0)
)
func reset():
needs_reset = false
number_of_steps = 0
set_position(Vector3(0, 0.7, 0))
y_velocity = 0.1
func _physics_process(delta):
number_of_steps += 1
if number_of_steps >= MAX_STEPS:
done = true
needs_reset = true
if needs_reset:
reset()
return
move_vector *= 0
move_vector = get_move_vector()
move_vector = move_vector.rotated(Vector3(0, 1, 0), rotation.y)
move_vector *= SPEED
move_vector.y = y_velocity
set_velocity(move_vector)
set_up_direction(Vector3(0, 1, 0))
move_and_slide()
func get_action_space():
return {
"move": {
"size": 1,
"action_type": "continuous"
}
}
func set_action(action):
move_action = action["move"][0]
func get_obs():
var obs = []
obs.append_array([
move_vector.x / SPEED,
move_vector.y / MAXIMUM_FALL_SPEED,
move_vector.z / SPEED
])
return {
"obs": obs
}
func get_obs_space():
return {
"obs": {
"size": [len(get_obs()["obs"])],
"space": "box"
}
}
func set_heuristic(heuristic):
self._heuristic = heuristic
func get_done():
return done
func set_done_false():
done = false