jtatman commited on
Commit
f1061a2
1 Parent(s): f27c6a5

Upload folder using huggingface_hub

Browse files
Files changed (49) hide show
  1. .gitattributes +1 -0
  2. 3DCarParkingExample.csproj +10 -0
  3. 3DCarParkingExample.sln +19 -0
  4. README.md +25 -0
  5. addons/godot_rl_agents/controller/ai_controller_2d.gd +82 -0
  6. addons/godot_rl_agents/controller/ai_controller_3d.gd +80 -0
  7. addons/godot_rl_agents/godot_rl_agents.gd +16 -0
  8. addons/godot_rl_agents/icon.png +3 -0
  9. addons/godot_rl_agents/onnx/csharp/ONNXInference.cs +103 -0
  10. addons/godot_rl_agents/onnx/csharp/SessionConfigurator.cs +131 -0
  11. addons/godot_rl_agents/onnx/csharp/docs/ONNXInference.xml +31 -0
  12. addons/godot_rl_agents/onnx/csharp/docs/SessionConfigurator.xml +29 -0
  13. addons/godot_rl_agents/onnx/wrapper/ONNX_wrapper.gd +24 -0
  14. addons/godot_rl_agents/plugin.cfg +7 -0
  15. addons/godot_rl_agents/sensors/sensors_2d/ExampleRaycastSensor2D.tscn +48 -0
  16. addons/godot_rl_agents/sensors/sensors_2d/ISensor2D.gd +20 -0
  17. addons/godot_rl_agents/sensors/sensors_2d/RaycastSensor2D.gd +118 -0
  18. addons/godot_rl_agents/sensors/sensors_2d/RaycastSensor2D.tscn +7 -0
  19. addons/godot_rl_agents/sensors/sensors_3d/ExampleRaycastSensor3D.tscn +6 -0
  20. addons/godot_rl_agents/sensors/sensors_3d/ISensor3D.gd +20 -0
  21. addons/godot_rl_agents/sensors/sensors_3d/RGBCameraSensor3D.gd +11 -0
  22. addons/godot_rl_agents/sensors/sensors_3d/RGBCameraSensor3D.tscn +42 -0
  23. addons/godot_rl_agents/sensors/sensors_3d/RaycastSensor3D.gd +166 -0
  24. addons/godot_rl_agents/sensors/sensors_3d/RaycastSensor3D.tscn +33 -0
  25. addons/godot_rl_agents/sync.gd +338 -0
  26. icon.svg +1 -0
  27. onnx/model.onnx +3 -0
  28. project.godot +52 -0
  29. readme-license.md +5 -0
  30. scenes/TestingScene.tscn +66 -0
  31. scenes/TrainingScene.tscn +128 -0
  32. scenes/blender/car_base.blend +0 -0
  33. scenes/blender/grass.png +3 -0
  34. scenes/blender/grass2.png +3 -0
  35. scenes/blender/grass3.png +3 -0
  36. scenes/blender/parking.blend +3 -0
  37. scenes/car/Car.gd +217 -0
  38. scenes/car/Car.tscn +151 -0
  39. scenes/car/CarAIController.gd +66 -0
  40. scenes/car/StaticCar.gd +19 -0
  41. scenes/car/StaticCar.tscn +28 -0
  42. scenes/car/Wheel.tscn +21 -0
  43. scenes/car/rear_light_braking.tres +5 -0
  44. scenes/car/rear_light_reversing.tres +5 -0
  45. scenes/car/static_cart_material.tres +6 -0
  46. scenes/game_scene/GameScene.tscn +139 -0
  47. scenes/game_scene/ParkingManager.gd +37 -0
  48. scenes/game_scene/ParkingSpotManager.gd +48 -0
  49. scenes/sync_override.gd +6 -0
.gitattributes CHANGED
@@ -56,3 +56,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
56
  # Video files - compressed
57
  *.mp4 filter=lfs diff=lfs merge=lfs -text
58
  *.webm filter=lfs diff=lfs merge=lfs -text
 
 
56
  # Video files - compressed
57
  *.mp4 filter=lfs diff=lfs merge=lfs -text
58
  *.webm filter=lfs diff=lfs merge=lfs -text
59
+ scenes/blender/parking.blend filter=lfs diff=lfs merge=lfs -text
3DCarParkingExample.csproj ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ <Project Sdk="Godot.NET.Sdk/4.2.1">
2
+ <PropertyGroup>
3
+ <TargetFramework>net6.0</TargetFramework>
4
+ <EnableDynamicLoading>true</EnableDynamicLoading>
5
+ <RootNamespace>GodotRLAgents</RootNamespace>
6
+ </PropertyGroup>
7
+ <ItemGroup>
8
+ <PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.15.1" />
9
+ </ItemGroup>
10
+ </Project>
3DCarParkingExample.sln ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Microsoft Visual Studio Solution File, Format Version 12.00
2
+ # Visual Studio 2012
3
+ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "3DCarParkingExample", "3DCarParkingExample.csproj", "{D30E1C4E-9E5D-4A7B-BE94-BC24DD6418C6}"
4
+ EndProject
5
+ Global
6
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
7
+ Debug|Any CPU = Debug|Any CPU
8
+ ExportDebug|Any CPU = ExportDebug|Any CPU
9
+ ExportRelease|Any CPU = ExportRelease|Any CPU
10
+ EndGlobalSection
11
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
12
+ {D30E1C4E-9E5D-4A7B-BE94-BC24DD6418C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13
+ {D30E1C4E-9E5D-4A7B-BE94-BC24DD6418C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
14
+ {D30E1C4E-9E5D-4A7B-BE94-BC24DD6418C6}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU
15
+ {D30E1C4E-9E5D-4A7B-BE94-BC24DD6418C6}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU
16
+ {D30E1C4E-9E5D-4A7B-BE94-BC24DD6418C6}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU
17
+ {D30E1C4E-9E5D-4A7B-BE94-BC24DD6418C6}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU
18
+ EndGlobalSection
19
+ EndGlobal
README.md ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: godot-rl
3
+ tags:
4
+ - deep-reinforcement-learning
5
+ - reinforcement-learning
6
+ - godot-rl
7
+ - environments
8
+ - video-games
9
+ ---
10
+
11
+ A RL environment called 3DCarParking for the Godot Game Engine.
12
+
13
+ This environment was created with: https://github.com/edbeeching/godot_rl_agents
14
+
15
+
16
+ ## Downloading the environment
17
+
18
+ After installing Godot RL Agents, download the environment with:
19
+
20
+ ```
21
+ gdrl.env_from_hub -r jtatman/godot_rl_3DCarParking
22
+ ```
23
+
24
+
25
+
addons/godot_rl_agents/controller/ai_controller_2d.gd ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ extends Node2D
2
+ class_name AIController2D
3
+
4
+ @export var reset_after := 1000
5
+
6
+ var heuristic := "human"
7
+ var done := false
8
+ var reward := 0.0
9
+ var n_steps := 0
10
+ var needs_reset := false
11
+
12
+ var _player: Node2D
13
+
14
+ func _ready():
15
+ add_to_group("AGENT")
16
+
17
+ func init(player: Node2D):
18
+ _player = player
19
+
20
+ #-- Methods that need implementing using the "extend script" option in Godot --#
21
+ func get_obs() -> Dictionary:
22
+ assert(false, "the get_obs method is not implemented when extending from ai_controller")
23
+ return {"obs":[]}
24
+
25
+ func get_reward() -> float:
26
+ assert(false, "the get_reward method is not implemented when extending from ai_controller")
27
+ return 0.0
28
+
29
+ func get_action_space() -> Dictionary:
30
+ assert(false, "the get get_action_space method is not implemented when extending from ai_controller")
31
+ return {
32
+ "example_actions_continous" : {
33
+ "size": 2,
34
+ "action_type": "continuous"
35
+ },
36
+ "example_actions_discrete" : {
37
+ "size": 2,
38
+ "action_type": "discrete"
39
+ },
40
+ }
41
+
42
+ func set_action(action) -> void:
43
+ assert(false, "the get set_action method is not implemented when extending from ai_controller")
44
+ # -----------------------------------------------------------------------------#
45
+
46
+ func _physics_process(delta):
47
+ n_steps += 1
48
+ if n_steps > reset_after:
49
+ needs_reset = true
50
+
51
+ func get_obs_space():
52
+ # may need overriding if the obs space is complex
53
+ var obs = get_obs()
54
+ return {
55
+ "obs": {
56
+ "size": [len(obs["obs"])],
57
+ "space": "box"
58
+ },
59
+ }
60
+
61
+ func reset():
62
+ n_steps = 0
63
+ needs_reset = false
64
+
65
+ func reset_if_done():
66
+ if done:
67
+ reset()
68
+
69
+ func set_heuristic(h):
70
+ # sets the heuristic from "human" or "model" nothing to change here
71
+ heuristic = h
72
+
73
+ func get_done():
74
+ return done
75
+
76
+ func set_done_false():
77
+ done = false
78
+
79
+ func zero_reward():
80
+ reward = 0.0
81
+
82
+
addons/godot_rl_agents/controller/ai_controller_3d.gd ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ extends Node3D
2
+ class_name AIController3D
3
+
4
+ @export var reset_after := 1000
5
+
6
+ var heuristic := "human"
7
+ var done := false
8
+ var reward := 0.0
9
+ var n_steps := 0
10
+ var needs_reset := false
11
+
12
+ var _player: Node3D
13
+
14
+ func _ready():
15
+ add_to_group("AGENT")
16
+
17
+ func init(player: Node3D):
18
+ _player = player
19
+
20
+ #-- Methods that need implementing using the "extend script" option in Godot --#
21
+ func get_obs() -> Dictionary:
22
+ assert(false, "the get_obs method is not implemented when extending from ai_controller")
23
+ return {"obs":[]}
24
+
25
+ func get_reward() -> float:
26
+ assert(false, "the get_reward method is not implemented when extending from ai_controller")
27
+ return 0.0
28
+
29
+ func get_action_space() -> Dictionary:
30
+ assert(false, "the get get_action_space method is not implemented when extending from ai_controller")
31
+ return {
32
+ "example_actions_continous" : {
33
+ "size": 2,
34
+ "action_type": "continuous"
35
+ },
36
+ "example_actions_discrete" : {
37
+ "size": 2,
38
+ "action_type": "discrete"
39
+ },
40
+ }
41
+
42
+ func set_action(action) -> void:
43
+ assert(false, "the get set_action method is not implemented when extending from ai_controller")
44
+ # -----------------------------------------------------------------------------#
45
+
46
+ func _physics_process(delta):
47
+ n_steps += 1
48
+ if n_steps > reset_after:
49
+ needs_reset = true
50
+
51
+ func get_obs_space():
52
+ # may need overriding if the obs space is complex
53
+ var obs = get_obs()
54
+ return {
55
+ "obs": {
56
+ "size": [len(obs["obs"])],
57
+ "space": "box"
58
+ },
59
+ }
60
+
61
+ func reset():
62
+ n_steps = 0
63
+ needs_reset = false
64
+
65
+ func reset_if_done():
66
+ if done:
67
+ reset()
68
+
69
+ func set_heuristic(h):
70
+ # sets the heuristic from "human" or "model" nothing to change here
71
+ heuristic = h
72
+
73
+ func get_done():
74
+ return done
75
+
76
+ func set_done_false():
77
+ done = false
78
+
79
+ func zero_reward():
80
+ reward = 0.0
addons/godot_rl_agents/godot_rl_agents.gd ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @tool
2
+ extends EditorPlugin
3
+
4
+
5
+ func _enter_tree():
6
+ # Initialization of the plugin goes here.
7
+ # Add the new type with a name, a parent type, a script and an icon.
8
+ add_custom_type("Sync", "Node", preload("sync.gd"), preload("icon.png"))
9
+ #add_custom_type("RaycastSensor2D2", "Node", preload("raycast_sensor_2d.gd"), preload("icon.png"))
10
+
11
+
12
+ func _exit_tree():
13
+ # Clean-up of the plugin goes here.
14
+ # Always remember to remove it from the engine when deactivated.
15
+ remove_custom_type("Sync")
16
+ #remove_custom_type("RaycastSensor2D2")
addons/godot_rl_agents/icon.png ADDED

Git LFS Details

  • SHA256: e3a8bc372d3313ce1ede4e7554472e37b322178b9488bfb709e296585abd3c44
  • Pointer size: 128 Bytes
  • Size of remote file: 198 Bytes
addons/godot_rl_agents/onnx/csharp/ONNXInference.cs ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using Godot;
2
+ using Microsoft.ML.OnnxRuntime;
3
+ using Microsoft.ML.OnnxRuntime.Tensors;
4
+ using System.Collections.Generic;
5
+ using System.Linq;
6
+
7
+ namespace GodotONNX
8
+ {
9
+ /// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/ONNXInference/*'/>
10
+ public partial class ONNXInference : GodotObject
11
+ {
12
+
13
+ private InferenceSession session;
14
+ /// <summary>
15
+ /// Path to the ONNX model. Use Initialize to change it.
16
+ /// </summary>
17
+ private string modelPath;
18
+ private int batchSize;
19
+
20
+ private SessionOptions SessionOpt;
21
+
22
+ //init function
23
+ /// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/Initialize/*'/>
24
+ public void Initialize(string Path, int BatchSize)
25
+ {
26
+ modelPath = Path;
27
+ batchSize = BatchSize;
28
+ SessionOpt = SessionConfigurator.MakeConfiguredSessionOptions();
29
+ session = LoadModel(modelPath);
30
+
31
+ }
32
+ /// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/Run/*'/>
33
+ public Godot.Collections.Dictionary<string, Godot.Collections.Array<float>> RunInference(Godot.Collections.Array<float> obs, int state_ins)
34
+ {
35
+ //Current model: Any (Godot Rl Agents)
36
+ //Expects a tensor of shape [batch_size, input_size] type float named obs and a tensor of shape [batch_size] type float named state_ins
37
+
38
+ //Fill the input tensors
39
+ // create span from inputSize
40
+ var span = new float[obs.Count]; //There's probably a better way to do this
41
+ for (int i = 0; i < obs.Count; i++)
42
+ {
43
+ span[i] = obs[i];
44
+ }
45
+
46
+ IReadOnlyCollection<NamedOnnxValue> inputs = new List<NamedOnnxValue>
47
+ {
48
+ NamedOnnxValue.CreateFromTensor("obs", new DenseTensor<float>(span, new int[] { batchSize, obs.Count })),
49
+ NamedOnnxValue.CreateFromTensor("state_ins", new DenseTensor<float>(new float[] { state_ins }, new int[] { batchSize }))
50
+ };
51
+ IReadOnlyCollection<string> outputNames = new List<string> { "output", "state_outs" }; //ONNX is sensible to these names, as well as the input names
52
+
53
+ IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results;
54
+ //We do not use "using" here so we get a better exception explaination later
55
+ try
56
+ {
57
+ results = session.Run(inputs, outputNames);
58
+ }
59
+ catch (OnnxRuntimeException e)
60
+ {
61
+ //This error usually means that the model is not compatible with the input, beacause of the input shape (size)
62
+ GD.Print("Error at inference: ", e);
63
+ return null;
64
+ }
65
+ //Can't convert IEnumerable<float> to Variant, so we have to convert it to an array or something
66
+ Godot.Collections.Dictionary<string, Godot.Collections.Array<float>> output = new Godot.Collections.Dictionary<string, Godot.Collections.Array<float>>();
67
+ DisposableNamedOnnxValue output1 = results.First();
68
+ DisposableNamedOnnxValue output2 = results.Last();
69
+ Godot.Collections.Array<float> output1Array = new Godot.Collections.Array<float>();
70
+ Godot.Collections.Array<float> output2Array = new Godot.Collections.Array<float>();
71
+
72
+ foreach (float f in output1.AsEnumerable<float>())
73
+ {
74
+ output1Array.Add(f);
75
+ }
76
+
77
+ foreach (float f in output2.AsEnumerable<float>())
78
+ {
79
+ output2Array.Add(f);
80
+ }
81
+
82
+ output.Add(output1.Name, output1Array);
83
+ output.Add(output2.Name, output2Array);
84
+
85
+ //Output is a dictionary of arrays, ex: { "output" : [0.1, 0.2, 0.3, 0.4, ...], "state_outs" : [0.5, ...]}
86
+ results.Dispose();
87
+ return output;
88
+ }
89
+ /// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/Load/*'/>
90
+ public InferenceSession LoadModel(string Path)
91
+ {
92
+ using Godot.FileAccess file = FileAccess.Open(Path, Godot.FileAccess.ModeFlags.Read);
93
+ byte[] model = file.GetBuffer((int)file.GetLength());
94
+ //file.Close(); file.Dispose(); //Close the file, then dispose the reference.
95
+ return new InferenceSession(model, SessionOpt); //Load the model
96
+ }
97
+ public void FreeDisposables()
98
+ {
99
+ session.Dispose();
100
+ SessionOpt.Dispose();
101
+ }
102
+ }
103
+ }
addons/godot_rl_agents/onnx/csharp/SessionConfigurator.cs ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using Godot;
2
+ using Microsoft.ML.OnnxRuntime;
3
+
4
+ namespace GodotONNX
5
+ {
6
+ /// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/SessionConfigurator/*'/>
7
+
8
+ public static class SessionConfigurator
9
+ {
10
+ public enum ComputeName
11
+ {
12
+ CUDA,
13
+ ROCm,
14
+ DirectML,
15
+ CoreML,
16
+ CPU
17
+ }
18
+
19
+ /// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/GetSessionOptions/*'/>
20
+ public static SessionOptions MakeConfiguredSessionOptions()
21
+ {
22
+ SessionOptions sessionOptions = new();
23
+ SetOptions(sessionOptions);
24
+ return sessionOptions;
25
+ }
26
+
27
+ private static void SetOptions(SessionOptions sessionOptions)
28
+ {
29
+ sessionOptions.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_WARNING;
30
+ ApplySystemSpecificOptions(sessionOptions);
31
+ }
32
+
33
+ /// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/SystemCheck/*'/>
34
+ static public void ApplySystemSpecificOptions(SessionOptions sessionOptions)
35
+ {
36
+ //Most code for this function is verbose only, the only reason it exists is to track
37
+ //implementation progress of the different compute APIs.
38
+
39
+ //December 2022: CUDA is not working.
40
+
41
+ string OSName = OS.GetName(); //Get OS Name
42
+
43
+ //ComputeName ComputeAPI = ComputeCheck(); //Get Compute API
44
+ // //TODO: Get CPU architecture
45
+
46
+ //Linux can use OpenVINO (C#) on x64 and ROCm on x86 (GDNative/C++)
47
+ //Windows can use OpenVINO (C#) on x64
48
+ //TODO: try TensorRT instead of CUDA
49
+ //TODO: Use OpenVINO for Intel Graphics
50
+
51
+ // Temporarily using CPU on all platforms to avoid errors detected with DML
52
+ ComputeName ComputeAPI = ComputeName.CPU;
53
+
54
+ //match OS and Compute API
55
+ GD.Print($"OS: {OSName} Compute API: {ComputeAPI}");
56
+
57
+ // CPU is set by default without appending necessary
58
+ // sessionOptions.AppendExecutionProvider_CPU(0);
59
+
60
+ /*
61
+ switch (OSName)
62
+ {
63
+ case "Windows": //Can use CUDA, DirectML
64
+ if (ComputeAPI is ComputeName.CUDA)
65
+ {
66
+ //CUDA
67
+ //sessionOptions.AppendExecutionProvider_CUDA(0);
68
+ //sessionOptions.AppendExecutionProvider_DML(0);
69
+ }
70
+ else if (ComputeAPI is ComputeName.DirectML)
71
+ {
72
+ //DirectML
73
+ //sessionOptions.AppendExecutionProvider_DML(0);
74
+ }
75
+ break;
76
+ case "X11": //Can use CUDA, ROCm
77
+ if (ComputeAPI is ComputeName.CUDA)
78
+ {
79
+ //CUDA
80
+ //sessionOptions.AppendExecutionProvider_CUDA(0);
81
+ }
82
+ if (ComputeAPI is ComputeName.ROCm)
83
+ {
84
+ //ROCm, only works on x86
85
+ //Research indicates that this has to be compiled as a GDNative plugin
86
+ //GD.Print("ROCm not supported yet, using CPU.");
87
+ //sessionOptions.AppendExecutionProvider_CPU(0);
88
+ }
89
+ break;
90
+ case "macOS": //Can use CoreML
91
+ if (ComputeAPI is ComputeName.CoreML)
92
+ { //CoreML
93
+ //TODO: Needs testing
94
+ //sessionOptions.AppendExecutionProvider_CoreML(0);
95
+ //CoreML on ARM64, out of the box, on x64 needs .tar file from GitHub
96
+ }
97
+ break;
98
+ default:
99
+ GD.Print("OS not Supported.");
100
+ break;
101
+ }
102
+ */
103
+ }
104
+
105
+
106
+ /// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/ComputeCheck/*'/>
107
+ public static ComputeName ComputeCheck()
108
+ {
109
+ string adapterName = Godot.RenderingServer.GetVideoAdapterName();
110
+ //string adapterVendor = Godot.RenderingServer.GetVideoAdapterVendor();
111
+ adapterName = adapterName.ToUpper(new System.Globalization.CultureInfo(""));
112
+ //TODO: GPU vendors for MacOS, what do they even use these days?
113
+
114
+ if (adapterName.Contains("INTEL"))
115
+ {
116
+ return ComputeName.DirectML;
117
+ }
118
+ if (adapterName.Contains("AMD") || adapterName.Contains("RADEON"))
119
+ {
120
+ return ComputeName.DirectML;
121
+ }
122
+ if (adapterName.Contains("NVIDIA"))
123
+ {
124
+ return ComputeName.CUDA;
125
+ }
126
+
127
+ GD.Print("Graphics Card not recognized."); //Should use CPU
128
+ return ComputeName.CPU;
129
+ }
130
+ }
131
+ }
addons/godot_rl_agents/onnx/csharp/docs/ONNXInference.xml ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <docs>
2
+ <members name="ONNXInference">
3
+ <ONNXInference>
4
+ <summary>
5
+ The main <c>ONNXInference</c> Class that handles the inference process.
6
+ </summary>
7
+ </ONNXInference>
8
+ <Initialize>
9
+ <summary>
10
+ Starts the inference process.
11
+ </summary>
12
+ <param name="Path">Path to the ONNX model, expects a path inside resources.</param>
13
+ <param name="BatchSize">How many observations will the model recieve.</param>
14
+ </Initialize>
15
+ <Run>
16
+ <summary>
17
+ Runs the given input through the model and returns the output.
18
+ </summary>
19
+ <param name="obs">Dictionary containing all observations.</param>
20
+ <param name="state_ins">How many different agents are creating these observations.</param>
21
+ <returns>A Dictionary of arrays, containing instructions based on the observations.</returns>
22
+ </Run>
23
+ <Load>
24
+ <summary>
25
+ Loads the given model into the inference process, using the best Execution provider available.
26
+ </summary>
27
+ <param name="Path">Path to the ONNX model, expects a path inside resources.</param>
28
+ <returns>InferenceSession ready to run.</returns>
29
+ </Load>
30
+ </members>
31
+ </docs>
addons/godot_rl_agents/onnx/csharp/docs/SessionConfigurator.xml ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <docs>
2
+ <members name="SessionConfigurator">
3
+ <SessionConfigurator>
4
+ <summary>
5
+ The main <c>SessionConfigurator</c> Class that handles the execution options and providers for the inference process.
6
+ </summary>
7
+ </SessionConfigurator>
8
+ <GetSessionOptions>
9
+ <summary>
10
+ Creates a SessionOptions with all available execution providers.
11
+ </summary>
12
+ <returns>SessionOptions with all available execution providers.</returns>
13
+ </GetSessionOptions>
14
+ <SystemCheck>
15
+ <summary>
16
+ Appends any execution provider available in the current system.
17
+ </summary>
18
+ <remarks>
19
+ This function is mainly verbose for tracking implementation progress of different compute APIs.
20
+ </remarks>
21
+ </SystemCheck>
22
+ <ComputeCheck>
23
+ <summary>
24
+ Checks for available GPUs.
25
+ </summary>
26
+ <returns>An integer identifier for each compute platform.</returns>
27
+ </ComputeCheck>
28
+ </members>
29
+ </docs>
addons/godot_rl_agents/onnx/wrapper/ONNX_wrapper.gd ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ extends Resource
2
+ class_name ONNXModel
3
+ var inferencer_script = load("res://addons/godot_rl_agents/onnx/csharp/ONNXInference.cs")
4
+
5
+ var inferencer = null
6
+
7
+ # Must provide the path to the model and the batch size
8
+ func _init(model_path, batch_size):
9
+ inferencer = inferencer_script.new()
10
+ inferencer.Initialize(model_path, batch_size)
11
+
12
+ # This function is the one that will be called from the game,
13
+ # requires the observation as an array and the state_ins as an int
14
+ # returns an Array containing the action the model takes.
15
+ func run_inference(obs : Array, state_ins : int) -> Dictionary:
16
+ if inferencer == null:
17
+ printerr("Inferencer not initialized")
18
+ return {}
19
+ return inferencer.RunInference(obs, state_ins)
20
+
21
+ func _notification(what):
22
+ if what == NOTIFICATION_PREDELETE:
23
+ inferencer.FreeDisposables()
24
+ inferencer.free()
addons/godot_rl_agents/plugin.cfg ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ [plugin]
2
+
3
+ name="GodotRLAgents"
4
+ description="Custom nodes for the godot rl agents toolkit "
5
+ author="Edward Beeching"
6
+ version="0.1"
7
+ script="godot_rl_agents.gd"
addons/godot_rl_agents/sensors/sensors_2d/ExampleRaycastSensor2D.tscn ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [gd_scene load_steps=5 format=3 uid="uid://ddeq7mn1ealyc"]
2
+
3
+ [ext_resource type="Script" path="res://addons/godot_rl_agents/sensors/sensors_2d/RaycastSensor2D.gd" id="1"]
4
+
5
+ [sub_resource type="GDScript" id="2"]
6
+ script/source = "extends Node2D
7
+
8
+
9
+
10
+ func _physics_process(delta: float) -> void:
11
+ print(\"step start\")
12
+
13
+ "
14
+
15
+ [sub_resource type="GDScript" id="1"]
16
+ script/source = "extends RayCast2D
17
+
18
+ var steps = 1
19
+
20
+ func _physics_process(delta: float) -> void:
21
+ print(\"processing raycast\")
22
+ steps += 1
23
+ if steps % 2:
24
+ force_raycast_update()
25
+
26
+ print(is_colliding())
27
+ "
28
+
29
+ [sub_resource type="CircleShape2D" id="3"]
30
+
31
+ [node name="ExampleRaycastSensor2D" type="Node2D"]
32
+ script = SubResource("2")
33
+
34
+ [node name="ExampleAgent" type="Node2D" parent="."]
35
+ position = Vector2(573, 314)
36
+ rotation = 0.286234
37
+
38
+ [node name="RaycastSensor2D" type="Node2D" parent="ExampleAgent"]
39
+ script = ExtResource("1")
40
+
41
+ [node name="TestRayCast2D" type="RayCast2D" parent="."]
42
+ script = SubResource("1")
43
+
44
+ [node name="StaticBody2D" type="StaticBody2D" parent="."]
45
+ position = Vector2(1, 52)
46
+
47
+ [node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"]
48
+ shape = SubResource("3")
addons/godot_rl_agents/sensors/sensors_2d/ISensor2D.gd ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ extends Node2D
2
+ class_name ISensor2D
3
+
4
+ var _obs : Array = []
5
+ var _active := false
6
+
7
+ func get_observation():
8
+ pass
9
+
10
+ func activate():
11
+ _active = true
12
+
13
+ func deactivate():
14
+ _active = false
15
+
16
+ func _update_observation():
17
+ pass
18
+
19
+ func reset():
20
+ pass
addons/godot_rl_agents/sensors/sensors_2d/RaycastSensor2D.gd ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @tool
2
+ extends ISensor2D
3
+ class_name RaycastSensor2D
4
+
5
+ @export_flags_2d_physics var collision_mask := 1:
6
+ get: return collision_mask
7
+ set(value):
8
+ collision_mask = value
9
+ _update()
10
+
11
+ @export var collide_with_areas := false:
12
+ get: return collide_with_areas
13
+ set(value):
14
+ collide_with_areas = value
15
+ _update()
16
+
17
+ @export var collide_with_bodies := true:
18
+ get: return collide_with_bodies
19
+ set(value):
20
+ collide_with_bodies = value
21
+ _update()
22
+
23
+ @export var n_rays := 16.0:
24
+ get: return n_rays
25
+ set(value):
26
+ n_rays = value
27
+ _update()
28
+
29
+ @export_range(5,200,5.0) var ray_length := 200:
30
+ get: return ray_length
31
+ set(value):
32
+ ray_length = value
33
+ _update()
34
+ @export_range(5,360,5.0) var cone_width := 360.0:
35
+ get: return cone_width
36
+ set(value):
37
+ cone_width = value
38
+ _update()
39
+
40
+ @export var debug_draw := true :
41
+ get: return debug_draw
42
+ set(value):
43
+ debug_draw = value
44
+ _update()
45
+
46
+
47
+ var _angles = []
48
+ var rays := []
49
+
50
+ func _update():
51
+ if Engine.is_editor_hint():
52
+ if debug_draw:
53
+ _spawn_nodes()
54
+ else:
55
+ for ray in get_children():
56
+ if ray is RayCast2D:
57
+ remove_child(ray)
58
+
59
+ func _ready() -> void:
60
+ _spawn_nodes()
61
+
62
+ func _spawn_nodes():
63
+ for ray in rays:
64
+ ray.queue_free()
65
+ rays = []
66
+
67
+ _angles = []
68
+ var step = cone_width / (n_rays)
69
+ var start = step/2 - cone_width/2
70
+
71
+ for i in n_rays:
72
+ var angle = start + i * step
73
+ var ray = RayCast2D.new()
74
+ ray.set_target_position(Vector2(
75
+ ray_length*cos(deg_to_rad(angle)),
76
+ ray_length*sin(deg_to_rad(angle))
77
+ ))
78
+ ray.set_name("node_"+str(i))
79
+ ray.enabled = true
80
+ ray.collide_with_areas = collide_with_areas
81
+ ray.collide_with_bodies = collide_with_bodies
82
+ ray.collision_mask = collision_mask
83
+ add_child(ray)
84
+ rays.append(ray)
85
+
86
+
87
+ _angles.append(start + i * step)
88
+
89
+
90
+ func _physics_process(delta: float) -> void:
91
+ if self._active:
92
+ self._obs = calculate_raycasts()
93
+
94
+ func get_observation() -> Array:
95
+ if len(self._obs) == 0:
96
+ print("obs was null, forcing raycast update")
97
+ return self.calculate_raycasts()
98
+ return self._obs
99
+
100
+
101
+ func calculate_raycasts() -> Array:
102
+ var result = []
103
+ for ray in rays:
104
+ ray.force_raycast_update()
105
+ var distance = _get_raycast_distance(ray)
106
+ result.append(distance)
107
+ return result
108
+
109
+ func _get_raycast_distance(ray : RayCast2D) -> float :
110
+ if !ray.is_colliding():
111
+ return 0.0
112
+
113
+ var distance = (global_position - ray.get_collision_point()).length()
114
+ distance = clamp(distance, 0.0, ray_length)
115
+ return (ray_length - distance) / ray_length
116
+
117
+
118
+
addons/godot_rl_agents/sensors/sensors_2d/RaycastSensor2D.tscn ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ [gd_scene load_steps=2 format=3 uid="uid://drvfihk5esgmv"]
2
+
3
+ [ext_resource type="Script" path="res://addons/godot_rl_agents/sensors/sensors_2d/RaycastSensor2D.gd" id="1"]
4
+
5
+ [node name="RaycastSensor2D" type="Node2D"]
6
+ script = ExtResource("1")
7
+ n_rays = 17.0
addons/godot_rl_agents/sensors/sensors_3d/ExampleRaycastSensor3D.tscn ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ [gd_scene format=3 uid="uid://biu787qh4woik"]
2
+
3
+ [node name="ExampleRaycastSensor3D" type="Node3D"]
4
+
5
+ [node name="Camera3D" type="Camera3D" parent="."]
6
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.804183, 0, 2.70146)
addons/godot_rl_agents/sensors/sensors_3d/ISensor3D.gd ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ extends Node3D
2
+ class_name ISensor3D
3
+
4
+ var _obs : Array = []
5
+ var _active := false
6
+
7
+ func get_observation():
8
+ pass
9
+
10
+ func activate():
11
+ _active = true
12
+
13
+ func deactivate():
14
+ _active = false
15
+
16
+ func _update_observation():
17
+ pass
18
+
19
+ func reset():
20
+ pass
addons/godot_rl_agents/sensors/sensors_3d/RGBCameraSensor3D.gd ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ extends Node3D
2
+ class_name RGBCameraSensor3D
3
+ var camera_pixels = null
4
+
5
+ @onready var camera_texture := $Control/TextureRect/CameraTexture as Sprite2D
6
+
7
+ func get_camera_pixel_encoding():
8
+ return camera_texture.get_texture().get_data().data["data"].hex_encode()
9
+
10
+ func get_camera_shape()-> Array:
11
+ return [$SubViewport.size[0], $SubViewport.size[1], 4]
addons/godot_rl_agents/sensors/sensors_3d/RGBCameraSensor3D.tscn ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [gd_scene load_steps=3 format=2]
2
+
3
+ [ext_resource path="res://addons/godot_rl_agents/sensors/sensors_3d/RGBCameraSensor3D.gd" type="Script" id=1]
4
+
5
+ [sub_resource type="ViewportTexture" id=1]
6
+ viewport_path = NodePath("SubViewport")
7
+
8
+ [node name="RGBCameraSensor3D" type="Node3D"]
9
+ script = ExtResource( 1 )
10
+
11
+ [node name="RemoteTransform3D" type="RemoteTransform3D" parent="."]
12
+ remote_path = NodePath("../SubViewport/Camera3D")
13
+
14
+ [node name="SubViewport" type="SubViewport" parent="."]
15
+ size = Vector2( 32, 32 )
16
+ render_target_update_mode = 3
17
+
18
+ [node name="Camera3D" type="Camera3D" parent="SubViewport"]
19
+ near = 0.5
20
+
21
+ [node name="Control" type="Control" parent="."]
22
+ anchor_right = 1.0
23
+ anchor_bottom = 1.0
24
+ __meta__ = {
25
+ "_edit_use_anchors_": false
26
+ }
27
+
28
+ [node name="TextureRect" type="ColorRect" parent="Control"]
29
+ offset_left = 1096.0
30
+ offset_top = 534.0
31
+ offset_right = 1114.0
32
+ offset_bottom = 552.0
33
+ scale = Vector2( 10, 10 )
34
+ color = Color( 0.00784314, 0.00784314, 0.00784314, 1 )
35
+ __meta__ = {
36
+ "_edit_use_anchors_": false
37
+ }
38
+
39
+ [node name="CameraTexture" type="Sprite2D" parent="Control/TextureRect"]
40
+ texture = SubResource( 1 )
41
+ offset = Vector2( 9, 9 )
42
+ flip_v = true
addons/godot_rl_agents/sensors/sensors_3d/RaycastSensor3D.gd ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @tool
2
+ extends ISensor3D
3
+ class_name RayCastSensor3D
4
+ @export_flags_3d_physics var collision_mask = 1:
5
+ get: return collision_mask
6
+ set(value):
7
+ collision_mask = value
8
+ _update()
9
+ @export_flags_3d_physics var boolean_class_mask = 1:
10
+ get: return boolean_class_mask
11
+ set(value):
12
+ boolean_class_mask = value
13
+ _update()
14
+
15
+ @export var n_rays_width := 6.0:
16
+ get: return n_rays_width
17
+ set(value):
18
+ n_rays_width = value
19
+ _update()
20
+
21
+ @export var n_rays_height := 6.0:
22
+ get: return n_rays_height
23
+ set(value):
24
+ n_rays_height = value
25
+ _update()
26
+
27
+ @export var ray_length := 10.0:
28
+ get: return ray_length
29
+ set(value):
30
+ ray_length = value
31
+ _update()
32
+
33
+ @export var cone_width := 60.0:
34
+ get: return cone_width
35
+ set(value):
36
+ cone_width = value
37
+ _update()
38
+
39
+ @export var cone_height := 60.0:
40
+ get: return cone_height
41
+ set(value):
42
+ cone_height = value
43
+ _update()
44
+
45
+ @export var collide_with_areas := false:
46
+ get: return collide_with_areas
47
+ set(value):
48
+ collide_with_areas = value
49
+ _update()
50
+
51
+ @export var collide_with_bodies := true:
52
+ get: return collide_with_bodies
53
+ set(value):
54
+ collide_with_bodies = value
55
+ _update()
56
+
57
+ @export var class_sensor := false
58
+
59
+ var rays := []
60
+ var geo = null
61
+
62
+ func _update():
63
+ if Engine.is_editor_hint():
64
+ if is_node_ready():
65
+ _spawn_nodes()
66
+
67
+ func _ready() -> void:
68
+ if Engine.is_editor_hint():
69
+ if get_child_count() == 0:
70
+ _spawn_nodes()
71
+ else:
72
+ _spawn_nodes()
73
+
74
+ func _spawn_nodes():
75
+ print("spawning nodes")
76
+ for ray in get_children():
77
+ ray.queue_free()
78
+ if geo:
79
+ geo.clear()
80
+ #$Lines.remove_points()
81
+ rays = []
82
+
83
+ var horizontal_step = cone_width / (n_rays_width)
84
+ var vertical_step = cone_height / (n_rays_height)
85
+
86
+ var horizontal_start = horizontal_step/2 - cone_width/2
87
+ var vertical_start = vertical_step/2 - cone_height/2
88
+
89
+ var points = []
90
+
91
+ for i in n_rays_width:
92
+ for j in n_rays_height:
93
+ var angle_w = horizontal_start + i * horizontal_step
94
+ var angle_h = vertical_start + j * vertical_step
95
+ #angle_h = 0.0
96
+ var ray = RayCast3D.new()
97
+ var cast_to = to_spherical_coords(ray_length, angle_w, angle_h)
98
+ ray.set_target_position(cast_to)
99
+
100
+ points.append(cast_to)
101
+
102
+ ray.set_name("node_"+str(i)+" "+str(j))
103
+ ray.enabled = true
104
+ ray.collide_with_bodies = collide_with_bodies
105
+ ray.collide_with_areas = collide_with_areas
106
+ ray.collision_mask = collision_mask
107
+ add_child(ray)
108
+ ray.set_owner(get_tree().edited_scene_root)
109
+ rays.append(ray)
110
+ ray.force_raycast_update()
111
+
112
+ # if Engine.editor_hint:
113
+ # _create_debug_lines(points)
114
+
115
+ func _create_debug_lines(points):
116
+ if not geo:
117
+ geo = ImmediateMesh.new()
118
+ add_child(geo)
119
+
120
+ geo.clear()
121
+ geo.begin(Mesh.PRIMITIVE_LINES)
122
+ for point in points:
123
+ geo.set_color(Color.AQUA)
124
+ geo.add_vertex(Vector3.ZERO)
125
+ geo.add_vertex(point)
126
+ geo.end()
127
+
128
+ func display():
129
+ if geo:
130
+ geo.display()
131
+
132
+ func to_spherical_coords(r, inc, azimuth) -> Vector3:
133
+ return Vector3(
134
+ r*sin(deg_to_rad(inc))*cos(deg_to_rad(azimuth)),
135
+ r*sin(deg_to_rad(azimuth)),
136
+ r*cos(deg_to_rad(inc))*cos(deg_to_rad(azimuth))
137
+ )
138
+
139
+ func get_observation() -> Array:
140
+ return self.calculate_raycasts()
141
+
142
+ func calculate_raycasts() -> Array:
143
+ var result = []
144
+ for ray in rays:
145
+ ray.set_enabled(true)
146
+ ray.force_raycast_update()
147
+ var distance = _get_raycast_distance(ray)
148
+
149
+ result.append(distance)
150
+ if class_sensor:
151
+ var hit_class = 0
152
+ if ray.get_collider():
153
+ var hit_collision_layer = ray.get_collider().collision_layer
154
+ hit_collision_layer = hit_collision_layer & collision_mask
155
+ hit_class = (hit_collision_layer & boolean_class_mask) > 0
156
+ result.append(hit_class)
157
+ ray.set_enabled(false)
158
+ return result
159
+
160
+ func _get_raycast_distance(ray : RayCast3D) -> float :
161
+ if !ray.is_colliding():
162
+ return 0.0
163
+
164
+ var distance = (global_transform.origin - ray.get_collision_point()).length()
165
+ distance = clamp(distance, 0.0, ray_length)
166
+ return (ray_length - distance) / ray_length
addons/godot_rl_agents/sensors/sensors_3d/RaycastSensor3D.tscn ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [gd_scene load_steps=2 format=3 uid="uid://b803cbh1fmy66"]
2
+
3
+ [ext_resource type="Script" path="res://addons/godot_rl_agents/sensors/sensors_3d/RaycastSensor3D.gd" id="1"]
4
+
5
+ [node name="RaycastSensor3D" type="Node3D"]
6
+ script = ExtResource("1")
7
+ n_rays_width = 4.0
8
+ n_rays_height = 2.0
9
+ ray_length = 11.0
10
+
11
+ [node name="@node_0 0@18991" type="RayCast3D" parent="."]
12
+ target_position = Vector3(-4.06608, -2.84701, 9.81639)
13
+
14
+ [node name="node_0 1" type="RayCast3D" parent="."]
15
+ target_position = Vector3(-4.06608, 2.84701, 9.81639)
16
+
17
+ [node name="@node_1 0@18992" type="RayCast3D" parent="."]
18
+ target_position = Vector3(-1.38686, -2.84701, 10.5343)
19
+
20
+ [node name="@node_1 1@18993" type="RayCast3D" parent="."]
21
+ target_position = Vector3(-1.38686, 2.84701, 10.5343)
22
+
23
+ [node name="@node_2 0@18994" type="RayCast3D" parent="."]
24
+ target_position = Vector3(1.38686, -2.84701, 10.5343)
25
+
26
+ [node name="@node_2 1@18995" type="RayCast3D" parent="."]
27
+ target_position = Vector3(1.38686, 2.84701, 10.5343)
28
+
29
+ [node name="@node_3 0@18996" type="RayCast3D" parent="."]
30
+ target_position = Vector3(4.06608, -2.84701, 9.81639)
31
+
32
+ [node name="@node_3 1@18997" type="RayCast3D" parent="."]
33
+ target_position = Vector3(4.06608, 2.84701, 9.81639)
addons/godot_rl_agents/sync.gd ADDED
@@ -0,0 +1,338 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ extends Node
2
+ # --fixed-fps 2000 --disable-render-loop
3
+ @export_range(1, 10, 1, "or_greater") var action_repeat := 8
4
+ @export_range(1, 10, 1, "or_greater") var speed_up = 1
5
+ @export var onnx_model_path := ""
6
+
7
+ @onready var start_time = Time.get_ticks_msec()
8
+
9
+ const MAJOR_VERSION := "0"
10
+ const MINOR_VERSION := "3"
11
+ const DEFAULT_PORT := "11008"
12
+ const DEFAULT_SEED := "1"
13
+ var stream : StreamPeerTCP = null
14
+ var connected = false
15
+ var message_center
16
+ var should_connect = true
17
+ var agents
18
+ var need_to_send_obs = false
19
+ var args = null
20
+ var initialized = false
21
+ var just_reset = false
22
+ var onnx_model = null
23
+ var n_action_steps = 0
24
+
25
+ var _action_space : Dictionary
26
+ var _obs_space : Dictionary
27
+
28
+ # Called when the node enters the scene tree for the first time.
29
+
30
+ func _ready():
31
+ await get_tree().root.ready
32
+ get_tree().set_pause(true)
33
+ _initialize()
34
+ await get_tree().create_timer(1.0).timeout
35
+ get_tree().set_pause(false)
36
+
37
+ func _initialize():
38
+ _get_agents()
39
+ _obs_space = agents[0].get_obs_space()
40
+ _action_space = agents[0].get_action_space()
41
+ args = _get_args()
42
+ Engine.physics_ticks_per_second = _get_speedup() * 60 # Replace with function body.
43
+ Engine.time_scale = _get_speedup() * 1.0
44
+ prints("physics ticks", Engine.physics_ticks_per_second, Engine.time_scale, _get_speedup(), speed_up)
45
+
46
+ # Run inference if onnx model path is set, otherwise wait for server connection
47
+ var run_onnx_model_inference : bool = onnx_model_path != ""
48
+ if run_onnx_model_inference:
49
+ assert(FileAccess.file_exists(onnx_model_path), "Onnx Model Path set on Sync node does not exist: " + onnx_model_path)
50
+ onnx_model = ONNXModel.new(onnx_model_path, 1)
51
+ _set_heuristic("model")
52
+ else:
53
+ connected = connect_to_server()
54
+ if connected:
55
+ _set_heuristic("model")
56
+ _handshake()
57
+ _send_env_info()
58
+ else:
59
+ _set_heuristic("human")
60
+
61
+ _set_seed()
62
+ _set_action_repeat()
63
+ initialized = true
64
+
65
+ func _physics_process(delta):
66
+ # two modes, human control, agent control
67
+ # pause tree, send obs, get actions, set actions, unpause tree
68
+ if n_action_steps % action_repeat != 0:
69
+ n_action_steps += 1
70
+ return
71
+
72
+ n_action_steps += 1
73
+
74
+ if connected:
75
+ get_tree().set_pause(true)
76
+
77
+ if just_reset:
78
+ just_reset = false
79
+ var obs = _get_obs_from_agents()
80
+
81
+ var reply = {
82
+ "type": "reset",
83
+ "obs": obs
84
+ }
85
+ _send_dict_as_json_message(reply)
86
+ # this should go straight to getting the action and setting it checked the agent, no need to perform one phyics tick
87
+ get_tree().set_pause(false)
88
+ return
89
+
90
+ if need_to_send_obs:
91
+ need_to_send_obs = false
92
+ var reward = _get_reward_from_agents()
93
+ var done = _get_done_from_agents()
94
+ #_reset_agents_if_done() # this ensures the new observation is from the next env instance : NEEDS REFACTOR
95
+
96
+ var obs = _get_obs_from_agents()
97
+
98
+ var reply = {
99
+ "type": "step",
100
+ "obs": obs,
101
+ "reward": reward,
102
+ "done": done
103
+ }
104
+ _send_dict_as_json_message(reply)
105
+
106
+ var handled = handle_message()
107
+
108
+ elif onnx_model != null:
109
+ var obs : Array = _get_obs_from_agents()
110
+
111
+ var actions = []
112
+ for o in obs:
113
+ var action = onnx_model.run_inference(o["obs"], 1.0)
114
+ action["output"] = clamp_array(action["output"], -1.0, 1.0)
115
+ var action_dict = _extract_action_dict(action["output"])
116
+ actions.append(action_dict)
117
+
118
+ _set_agent_actions(actions)
119
+ need_to_send_obs = true
120
+ get_tree().set_pause(false)
121
+ _reset_agents_if_done()
122
+
123
+ else:
124
+ _reset_agents_if_done()
125
+
126
+ func _extract_action_dict(action_array: Array):
127
+ var index = 0
128
+ var result = {}
129
+ for key in _action_space.keys():
130
+ var size = _action_space[key]["size"]
131
+ if _action_space[key]["action_type"] == "discrete":
132
+ result[key] = round(action_array[index])
133
+ else:
134
+ result[key] = action_array.slice(index,index+size)
135
+ index += size
136
+
137
+ return result
138
+
139
+ func _get_agents():
140
+ agents = get_tree().get_nodes_in_group("AGENT")
141
+
142
+ func _set_heuristic(heuristic):
143
+ for agent in agents:
144
+ agent.set_heuristic(heuristic)
145
+
146
+ func _handshake():
147
+ print("performing handshake")
148
+
149
+ var json_dict = _get_dict_json_message()
150
+ assert(json_dict["type"] == "handshake")
151
+ var major_version = json_dict["major_version"]
152
+ var minor_version = json_dict["minor_version"]
153
+ if major_version != MAJOR_VERSION:
154
+ print("WARNING: major verison mismatch ", major_version, " ", MAJOR_VERSION)
155
+ if minor_version != MINOR_VERSION:
156
+ print("WARNING: minor verison mismatch ", minor_version, " ", MINOR_VERSION)
157
+
158
+ print("handshake complete")
159
+
160
+ func _get_dict_json_message():
161
+ # returns a dictionary from of the most recent message
162
+ # this is not waiting
163
+ while stream.get_available_bytes() == 0:
164
+ stream.poll()
165
+ if stream.get_status() != 2:
166
+ print("server disconnected status, closing")
167
+ get_tree().quit()
168
+ return null
169
+
170
+ OS.delay_usec(10)
171
+
172
+ var message = stream.get_string()
173
+ var json_data = JSON.parse_string(message)
174
+
175
+ return json_data
176
+
177
+ func _send_dict_as_json_message(dict):
178
+ stream.put_string(JSON.stringify(dict))
179
+
180
+ func _send_env_info():
181
+ var json_dict = _get_dict_json_message()
182
+ assert(json_dict["type"] == "env_info")
183
+
184
+
185
+ var message = {
186
+ "type" : "env_info",
187
+ "observation_space": _obs_space,
188
+ "action_space":_action_space,
189
+ "n_agents": len(agents)
190
+ }
191
+ _send_dict_as_json_message(message)
192
+
193
+ func connect_to_server():
194
+ print("Waiting for one second to allow server to start")
195
+ OS.delay_msec(1000)
196
+ print("trying to connect to server")
197
+ stream = StreamPeerTCP.new()
198
+
199
+ # "localhost" was not working on windows VM, had to use the IP
200
+ var ip = "127.0.0.1"
201
+ var port = _get_port()
202
+ var connect = stream.connect_to_host(ip, port)
203
+ stream.set_no_delay(true) # TODO check if this improves performance or not
204
+ stream.poll()
205
+ # Fetch the status until it is either connected (2) or failed to connect (3)
206
+ while stream.get_status() < 2:
207
+ stream.poll()
208
+ return stream.get_status() == 2
209
+
210
+ func _get_args():
211
+ print("getting command line arguments")
212
+ var arguments = {}
213
+ for argument in OS.get_cmdline_args():
214
+ print(argument)
215
+ if argument.find("=") > -1:
216
+ var key_value = argument.split("=")
217
+ arguments[key_value[0].lstrip("--")] = key_value[1]
218
+ else:
219
+ # Options without an argument will be present in the dictionary,
220
+ # with the value set to an empty string.
221
+ arguments[argument.lstrip("--")] = ""
222
+
223
+ return arguments
224
+
225
+ func _get_speedup():
226
+ print(args)
227
+ return args.get("speedup", str(speed_up)).to_int()
228
+
229
+ func _get_port():
230
+ return args.get("port", DEFAULT_PORT).to_int()
231
+
232
+ func _set_seed():
233
+ var _seed = args.get("env_seed", DEFAULT_SEED).to_int()
234
+ seed(_seed)
235
+
236
+ func _set_action_repeat():
237
+ action_repeat = args.get("action_repeat", str(action_repeat)).to_int()
238
+
239
+ func disconnect_from_server():
240
+ stream.disconnect_from_host()
241
+
242
+
243
+
244
+ func handle_message() -> bool:
245
+ # get json message: reset, step, close
246
+ var message = _get_dict_json_message()
247
+ if message["type"] == "close":
248
+ print("received close message, closing game")
249
+ get_tree().quit()
250
+ get_tree().set_pause(false)
251
+ return true
252
+
253
+ if message["type"] == "reset":
254
+ print("resetting all agents")
255
+ _reset_all_agents()
256
+ just_reset = true
257
+ get_tree().set_pause(false)
258
+ #print("resetting forcing draw")
259
+ # RenderingServer.force_draw()
260
+ # var obs = _get_obs_from_agents()
261
+ # print("obs ", obs)
262
+ # var reply = {
263
+ # "type": "reset",
264
+ # "obs": obs
265
+ # }
266
+ # _send_dict_as_json_message(reply)
267
+ return true
268
+
269
+ if message["type"] == "call":
270
+ var method = message["method"]
271
+ var returns = _call_method_on_agents(method)
272
+ var reply = {
273
+ "type": "call",
274
+ "returns": returns
275
+ }
276
+ print("calling method from Python")
277
+ _send_dict_as_json_message(reply)
278
+ return handle_message()
279
+
280
+ if message["type"] == "action":
281
+ var action = message["action"]
282
+ _set_agent_actions(action)
283
+ need_to_send_obs = true
284
+ get_tree().set_pause(false)
285
+ return true
286
+
287
+ print("message was not handled")
288
+ return false
289
+
290
+ func _call_method_on_agents(method):
291
+ var returns = []
292
+ for agent in agents:
293
+ returns.append(agent.call(method))
294
+
295
+ return returns
296
+
297
+
298
+ func _reset_agents_if_done():
299
+ for agent in agents:
300
+ if agent.get_done():
301
+ agent.set_done_false()
302
+
303
+ func _reset_all_agents():
304
+ for agent in agents:
305
+ agent.needs_reset = true
306
+ #agent.reset()
307
+
308
+ func _get_obs_from_agents():
309
+ var obs = []
310
+ for agent in agents:
311
+ obs.append(agent.get_obs())
312
+
313
+ return obs
314
+
315
+ func _get_reward_from_agents():
316
+ var rewards = []
317
+ for agent in agents:
318
+ rewards.append(agent.get_reward())
319
+ agent.zero_reward()
320
+ return rewards
321
+
322
+ func _get_done_from_agents():
323
+ var dones = []
324
+ for agent in agents:
325
+ var done = agent.get_done()
326
+ if done: agent.set_done_false()
327
+ dones.append(done)
328
+ return dones
329
+
330
+ func _set_agent_actions(actions):
331
+ for i in range(len(actions)):
332
+ agents[i].set_action(actions[i])
333
+
334
+ func clamp_array(arr : Array, min:float, max:float):
335
+ var output : Array = []
336
+ for a in arr:
337
+ output.append(clamp(a, min, max))
338
+ return output
icon.svg ADDED
onnx/model.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:902d0d267b3404a52b82cc90acefd0b5b1afea89965e3887a4a3b578bf84a4db
3
+ size 25805
project.godot ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ; Engine configuration file.
2
+ ; It's best edited using the editor UI and not directly,
3
+ ; since the parameters that go here are not all obvious.
4
+ ;
5
+ ; Format:
6
+ ; [section] ; section goes between []
7
+ ; param=value ; assign values to parameters
8
+
9
+ config_version=5
10
+
11
+ [application]
12
+
13
+ config/name="3DCarParkingExample"
14
+ config/description="An example of a simple 3d car parking RL environment. "
15
+ run/main_scene="res://scenes/TrainingScene.tscn"
16
+ config/features=PackedStringArray("4.2", "C#", "Forward Plus")
17
+ config/icon="res://icon.svg"
18
+
19
+ [dotnet]
20
+
21
+ project/assembly_name="3DCarParkingExample"
22
+
23
+ [editor_plugins]
24
+
25
+ enabled=PackedStringArray("res://addons/godot_rl_agents/plugin.cfg")
26
+
27
+ [input]
28
+
29
+ move_forward={
30
+ "deadzone": 0.5,
31
+ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"echo":false,"script":null)
32
+ , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"echo":false,"script":null)
33
+ ]
34
+ }
35
+ move_backward={
36
+ "deadzone": 0.5,
37
+ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"echo":false,"script":null)
38
+ , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"echo":false,"script":null)
39
+ ]
40
+ }
41
+ steer_left={
42
+ "deadzone": 0.5,
43
+ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"echo":false,"script":null)
44
+ , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"echo":false,"script":null)
45
+ ]
46
+ }
47
+ steer_right={
48
+ "deadzone": 0.5,
49
+ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"echo":false,"script":null)
50
+ , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"echo":false,"script":null)
51
+ ]
52
+ }
readme-license.md ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ Simple 3D car parking environment made by Ivan-267 using Godot, Godot RL Agents, and Blender.
2
+
3
+ The following license is only for the assets in the folder "scenes\blender":
4
+ Author: https://github.com/Ivan-267
5
+ License: https://creativecommons.org/licenses/by/4.0/
scenes/TestingScene.tscn ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [gd_scene load_steps=8 format=3 uid="uid://00lmrvmkwpim"]
2
+
3
+ [ext_resource type="PackedScene" uid="uid://ca6csk8y5nvuu" path="res://scenes/game_scene/GameScene.tscn" id="1_cnrg2"]
4
+ [ext_resource type="Script" path="res://scenes/sync_override.gd" id="2_xg13p"]
5
+
6
+ [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_vwcdc"]
7
+ resource_local_to_scene = true
8
+ shading_mode = 0
9
+ albedo_color = Color(1, 0, 0, 1)
10
+
11
+ [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_k2b80"]
12
+ resource_local_to_scene = true
13
+ shading_mode = 0
14
+
15
+ [sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_g5l5s"]
16
+
17
+ [sub_resource type="Sky" id="Sky_a413c"]
18
+ sky_material = SubResource("ProceduralSkyMaterial_g5l5s")
19
+
20
+ [sub_resource type="Environment" id="Environment_x2xl3"]
21
+ background_mode = 2
22
+ background_energy_multiplier = 1.2
23
+ sky = SubResource("Sky_a413c")
24
+ sdfgi_enabled = true
25
+ glow_enabled = true
26
+ glow_bloom = 0.02
27
+ volumetric_fog_density = 0.01
28
+
29
+ [node name="TrainingScene" type="Node3D"]
30
+
31
+ [node name="GameScene" parent="." instance=ExtResource("1_cnrg2")]
32
+
33
+ [node name="Car" parent="GameScene" index="0"]
34
+ braking_material = SubResource("StandardMaterial3D_vwcdc")
35
+ reversing_material = SubResource("StandardMaterial3D_k2b80")
36
+
37
+ [node name="Camera3D" type="Camera3D" parent="GameScene/Car" index="8"]
38
+ transform = Transform3D(-1, 2.99003e-08, -8.21505e-08, 0, 0.939693, 0.34202, 8.74228e-08, 0.34202, -0.939693, 0, 1.63, -2.105)
39
+ current = true
40
+ fov = 100.0
41
+
42
+ [node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
43
+ transform = Transform3D(0.857167, 0.364187, -0.364187, 0, 0.707107, 0.707107, 0.515038, -0.606109, 0.606109, 0, 0, 0)
44
+ shadow_enabled = true
45
+
46
+ [node name="WorldEnvironment" type="WorldEnvironment" parent="."]
47
+ environment = SubResource("Environment_x2xl3")
48
+
49
+ [node name="Sync" type="Node" parent="."]
50
+ script = ExtResource("2_xg13p")
51
+ speed_up = 3
52
+ onnx_model_path = "onnx/model.onnx"
53
+
54
+ [node name="Camera3D" type="Camera3D" parent="."]
55
+ transform = Transform3D(0.999998, 0.0014048, -0.00103571, 0, 0.593419, 0.804894, 0.00174533, -0.804893, 0.593418, 0, 11.05, 9.648)
56
+ current = true
57
+ fov = 80.0
58
+ size = 20.0
59
+
60
+ [node name="Camera3D2" type="Camera3D" parent="."]
61
+ transform = Transform3D(0.707106, 0.294355, -0.642927, 0, 0.909236, 0.416281, 0.707106, -0.294355, 0.642927, -7, 2.28, 7)
62
+ fov = 91.6
63
+ size = 20.0
64
+
65
+ [editable path="GameScene"]
66
+ [editable path="GameScene/Parking"]
scenes/TrainingScene.tscn ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [gd_scene load_steps=6 format=3 uid="uid://b4f2hcn0g5vjg"]
2
+
3
+ [ext_resource type="PackedScene" uid="uid://ca6csk8y5nvuu" path="res://scenes/game_scene/GameScene.tscn" id="1_vdknj"]
4
+ [ext_resource type="Script" path="res://scenes/sync_override.gd" id="2_p6jfa"]
5
+
6
+ [sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_g5l5s"]
7
+
8
+ [sub_resource type="Sky" id="Sky_a413c"]
9
+ sky_material = SubResource("ProceduralSkyMaterial_g5l5s")
10
+
11
+ [sub_resource type="Environment" id="Environment_xy0xn"]
12
+ background_mode = 2
13
+ background_energy_multiplier = 1.2
14
+ sky = SubResource("Sky_a413c")
15
+ volumetric_fog_density = 0.01
16
+
17
+ [node name="TrainingScene" type="Node3D"]
18
+
19
+ [node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
20
+ transform = Transform3D(0.857167, 0.364187, -0.364187, 0, 0.707107, 0.707107, 0.515038, -0.606109, 0.606109, 0, 0, 0)
21
+ shadow_enabled = true
22
+
23
+ [node name="GameScene" parent="." instance=ExtResource("1_vdknj")]
24
+
25
+ [node name="GameScene2" parent="." instance=ExtResource("1_vdknj")]
26
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 48, 0, 0)
27
+
28
+ [node name="GameScene3" parent="." instance=ExtResource("1_vdknj")]
29
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -44, 0, 0)
30
+
31
+ [node name="GameScene4" parent="." instance=ExtResource("1_vdknj")]
32
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 37)
33
+
34
+ [node name="GameScene5" parent="." instance=ExtResource("1_vdknj")]
35
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 48, 0, 37)
36
+
37
+ [node name="GameScene6" parent="." instance=ExtResource("1_vdknj")]
38
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -44, 0, 37)
39
+
40
+ [node name="GameScene7" parent="." instance=ExtResource("1_vdknj")]
41
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 71)
42
+
43
+ [node name="GameScene8" parent="." instance=ExtResource("1_vdknj")]
44
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 48, 0, 71)
45
+
46
+ [node name="GameScene9" parent="." instance=ExtResource("1_vdknj")]
47
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -44, 0, 71)
48
+
49
+ [node name="GameScene10" parent="." instance=ExtResource("1_vdknj")]
50
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 101)
51
+
52
+ [node name="GameScene11" parent="." instance=ExtResource("1_vdknj")]
53
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 48, 0, 101)
54
+
55
+ [node name="GameScene12" parent="." instance=ExtResource("1_vdknj")]
56
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -44, 0, 101)
57
+
58
+ [node name="GameScene13" parent="." instance=ExtResource("1_vdknj")]
59
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 142.849, 0, 0)
60
+
61
+ [node name="GameScene14" parent="." instance=ExtResource("1_vdknj")]
62
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 190.849, 0, 0)
63
+
64
+ [node name="GameScene15" parent="." instance=ExtResource("1_vdknj")]
65
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 98.8491, 0, 0)
66
+
67
+ [node name="GameScene16" parent="." instance=ExtResource("1_vdknj")]
68
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 142.849, 0, 37)
69
+
70
+ [node name="GameScene17" parent="." instance=ExtResource("1_vdknj")]
71
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 190.849, 0, 37)
72
+
73
+ [node name="GameScene18" parent="." instance=ExtResource("1_vdknj")]
74
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 98.8491, 0, 37)
75
+
76
+ [node name="GameScene19" parent="." instance=ExtResource("1_vdknj")]
77
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 142.849, 0, 71)
78
+
79
+ [node name="GameScene20" parent="." instance=ExtResource("1_vdknj")]
80
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 190.849, 0, 71)
81
+
82
+ [node name="GameScene21" parent="." instance=ExtResource("1_vdknj")]
83
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 98.8491, 0, 71)
84
+
85
+ [node name="GameScene22" parent="." instance=ExtResource("1_vdknj")]
86
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 142.849, 0, 101)
87
+
88
+ [node name="GameScene23" parent="." instance=ExtResource("1_vdknj")]
89
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 190.849, 0, 101)
90
+
91
+ [node name="GameScene24" parent="." instance=ExtResource("1_vdknj")]
92
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 98.8491, 0, 101)
93
+
94
+ [node name="GameScene25" parent="." instance=ExtResource("1_vdknj")]
95
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -134.151, 0, 0)
96
+
97
+ [node name="GameScene26" parent="." instance=ExtResource("1_vdknj")]
98
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -86.1509, 0, 0)
99
+
100
+ [node name="GameScene27" parent="." instance=ExtResource("1_vdknj")]
101
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -178.151, 0, 0)
102
+
103
+ [node name="GameScene28" parent="." instance=ExtResource("1_vdknj")]
104
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -134.151, 0, 37)
105
+
106
+ [node name="GameScene29" parent="." instance=ExtResource("1_vdknj")]
107
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -86.1509, 0, 37)
108
+
109
+ [node name="GameScene30" parent="." instance=ExtResource("1_vdknj")]
110
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -178.151, 0, 37)
111
+
112
+ [node name="GameScene31" parent="." instance=ExtResource("1_vdknj")]
113
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -178.151, 0, 99)
114
+
115
+ [node name="GameScene32" parent="." instance=ExtResource("1_vdknj")]
116
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -178.151, 0, 72)
117
+
118
+ [node name="WorldEnvironment" type="WorldEnvironment" parent="."]
119
+ environment = SubResource("Environment_xy0xn")
120
+
121
+ [node name="Sync" type="Node" parent="."]
122
+ script = ExtResource("2_p6jfa")
123
+ speed_up = 10
124
+
125
+ [node name="Camera3D" type="Camera3D" parent="."]
126
+ transform = Transform3D(0.999998, 0.0014048, -0.00103571, 0, 0.593419, 0.804894, 0.00174533, -0.804893, 0.593418, 0.695, 11.05, 9.648)
127
+ fov = 80.0
128
+ size = 20.0
scenes/blender/car_base.blend ADDED
Binary file (992 kB). View file
 
scenes/blender/grass.png ADDED

Git LFS Details

  • SHA256: 814d1330279a614636d5ed10bf0c906d1054d4c0f64cda93a35fda78bf3e6844
  • Pointer size: 130 Bytes
  • Size of remote file: 57.9 kB
scenes/blender/grass2.png ADDED

Git LFS Details

  • SHA256: 85a3e266438c8ebf37c370d74219331ac3c68deaa1144d00f6d582c293c7ada3
  • Pointer size: 130 Bytes
  • Size of remote file: 55.2 kB
scenes/blender/grass3.png ADDED

Git LFS Details

  • SHA256: 28a187690b9dabe4fee455d53887946da8f0698c853c443b139c2140911cc2df
  • Pointer size: 130 Bytes
  • Size of remote file: 51.6 kB
scenes/blender/parking.blend ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:068f73437707265496e1a909678fe955cd537515d13ac85c8e39d58accc229e2
3
+ size 1285376
scenes/car/Car.gd ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ extends VehicleBody3D
2
+ class_name Car
3
+
4
+ @export var playing_area_x_size: float = 20
5
+ @export var playing_area_z_size: float = 20
6
+
7
+ @export var acceleration: float = 200
8
+ @export var max_steer_angle: float = 20
9
+
10
+ @export var parking_manager: ParkingManager
11
+ @export var goal_marker: Node3D
12
+
13
+ @onready var max_velocity = acceleration / mass * 40
14
+ @onready var ai_controller: AIController3D = $AIController3D
15
+ @onready var raycast_sensor: RayCastSensor3D = $RayCastSensor3D
16
+
17
+ var goal_parking_spot: Transform3D
18
+
19
+ var requested_acceleration: float
20
+ var requested_steering: float
21
+ var _initial_transform: Transform3D
22
+ var times_restarted: int
23
+
24
+ var _smallest_distance_to_goal: float = 0
25
+ var _max_goal_dist: float = 1
26
+
27
+ var episode_ended_unsuccessfully_reward: float = -6
28
+
29
+ var _rear_lights: Array[MeshInstance3D]
30
+ @export var braking_material: StandardMaterial3D
31
+ @export var reversing_material: StandardMaterial3D
32
+
33
+ func get_normalized_velocity():
34
+ return linear_velocity.normalized() * (linear_velocity.length() / max_velocity)
35
+
36
+ func _ready():
37
+ ai_controller.init(self)
38
+ _initial_transform = transform
39
+
40
+ _rear_lights.resize(2)
41
+ _rear_lights[0] = $"car_base/Rear-light" as MeshInstance3D
42
+ _rear_lights[1] = $"car_base/Rear-light_001" as MeshInstance3D
43
+
44
+ _max_goal_dist = (
45
+ Vector2(
46
+ playing_area_x_size,
47
+ playing_area_z_size
48
+ ).length()
49
+ )
50
+
51
+ func reset():
52
+ times_restarted += 1
53
+
54
+ transform = _initial_transform
55
+
56
+ if randi_range(0, 1) == 0:
57
+ transform.origin = -transform.origin
58
+ transform.basis = transform.basis.rotated(Vector3.UP, PI)
59
+
60
+ linear_velocity = Vector3.ZERO
61
+ angular_velocity = Vector3.ZERO
62
+
63
+ transform.basis = transform.basis.rotated(Vector3.UP, randf_range(-0.3, 0.3))
64
+
65
+ parking_manager.disable_random_cars()
66
+
67
+ goal_parking_spot = parking_manager.get_random_free_parking_spot_transform()
68
+ goal_marker.global_position = goal_parking_spot.origin + Vector3.UP * 3
69
+
70
+ _smallest_distance_to_goal = _get_current_distance_to_goal()
71
+
72
+ func _physics_process(delta):
73
+ _reset_if_needed()
74
+ _update_reward()
75
+
76
+ if (ai_controller.heuristic != "human"):
77
+ engine_force = (requested_acceleration) * acceleration
78
+ steering = move_toward(steering, deg_to_rad(requested_steering * max_steer_angle), delta)
79
+ else:
80
+ engine_force = (
81
+ int(Input.is_action_pressed("move_forward")) -
82
+ int(Input.is_action_pressed("move_backward"))
83
+ ) * acceleration
84
+ steering = move_toward(
85
+ steering,
86
+ deg_to_rad((
87
+ int(Input.is_action_pressed("steer_left")) -
88
+ int(Input.is_action_pressed("steer_right"))
89
+ ) * max_steer_angle),
90
+ delta
91
+ )
92
+
93
+ _update_rear_lights()
94
+ _reset_on_out_of_bounds()
95
+ _reset_on_turned_over()
96
+ _reset_on_went_away_from_goal()
97
+ _end_episode_on_goal_reached()
98
+
99
+ func _update_rear_lights():
100
+ var velocity := get_normalized_velocity_in_player_reference().z
101
+
102
+ set_rear_light_material(null)
103
+
104
+ var brake_or_reverse_requested: bool
105
+ if (ai_controller.heuristic != "human"):
106
+ brake_or_reverse_requested = requested_acceleration < 0
107
+ else:
108
+ brake_or_reverse_requested = Input.is_action_pressed("move_backward")
109
+
110
+ if velocity >= 0:
111
+ if brake_or_reverse_requested:
112
+ set_rear_light_material(braking_material)
113
+ elif velocity <= -0.015:
114
+ set_rear_light_material(reversing_material)
115
+
116
+ func set_rear_light_material(material: StandardMaterial3D):
117
+ _rear_lights[0].set_surface_override_material(0, material)
118
+ _rear_lights[1].set_surface_override_material(0, material)
119
+
120
+ func _reset_on_out_of_bounds():
121
+ if (position.y < -2 or abs(position.x) > 10 or abs(position.z) > 10):
122
+ _end_episode(episode_ended_unsuccessfully_reward)
123
+
124
+ # If the agent was near the goal but has since moved away,
125
+ # end the episode with a negative reward
126
+ func _reset_on_went_away_from_goal():
127
+ var goal_dist = _get_current_distance_to_goal()
128
+ if _smallest_distance_to_goal < 1.5 and goal_dist > _smallest_distance_to_goal + 3.5:
129
+ _end_episode(episode_ended_unsuccessfully_reward)
130
+
131
+ # If the goal condition is reached, provide a reward based on:
132
+ # how quickly the goal was reached,
133
+ # the current velocity,
134
+ # direction alignment,
135
+ # and distance from the goal position
136
+ func _end_episode_on_goal_reached():
137
+ var goal_dist = _get_current_distance_to_goal()
138
+ if _is_goal_reached(goal_dist):
139
+ var parked_succesfully_reward: float = (
140
+ 10
141
+ - _get_direction_difference() * 4
142
+ - (float(ai_controller.n_steps) / ai_controller.reset_after) * 2
143
+ - get_normalized_velocity().length()
144
+ - (goal_dist / _max_goal_dist)
145
+ )
146
+ _end_episode(parked_succesfully_reward)
147
+
148
+ func _reset_on_turned_over():
149
+ if global_transform.basis.y.dot(Vector3.UP) < 0.6:
150
+ _end_episode(episode_ended_unsuccessfully_reward)
151
+
152
+ func _end_episode(final_reward: float = 0):
153
+ ai_controller.reward += final_reward
154
+ ai_controller.needs_reset = true
155
+ ai_controller.done = true
156
+
157
+ func _reset_if_needed():
158
+ if ai_controller.needs_reset:
159
+ reset()
160
+ ai_controller.reset()
161
+
162
+ func _update_reward():
163
+ if times_restarted == 0:
164
+ return
165
+
166
+ var goal_dist = _get_current_distance_to_goal()
167
+
168
+ # # If the car is close to the goal parking spot,
169
+ # we modify the reward based on the angle
170
+ var direction_multiplier = 1
171
+ if goal_dist < 1:
172
+ direction_multiplier = (1 - _get_direction_difference()) * 1.5
173
+
174
+ if goal_dist < _smallest_distance_to_goal:
175
+ ai_controller.reward += (
176
+ (_smallest_distance_to_goal - goal_dist) *
177
+ direction_multiplier
178
+ )
179
+ _smallest_distance_to_goal = goal_dist
180
+
181
+ # Encourage shorter paths
182
+ if goal_dist > 2:
183
+ ai_controller.reward -= 10 * (get_normalized_velocity().length() / ai_controller.reset_after)
184
+
185
+ # Returns the difference between current direction and goal direction in range 0,1
186
+ # If 1, the angle is 180 degrees, if 0, the direction is perfectly aligned.
187
+ func _get_direction_difference() -> float:
188
+ return (global_transform.basis.z.dot(-goal_parking_spot.basis.z) + 1) / 2
189
+
190
+ func _is_goal_reached(current_goal_dist: float) -> bool:
191
+ return (
192
+ current_goal_dist < 0.5 and
193
+ linear_velocity.length() < 0.05 and
194
+ _get_direction_difference() < 0.07
195
+ )
196
+
197
+ func _get_current_distance_to_goal() -> float:
198
+ # Exclude Y difference from the calculated distance
199
+ var goal_transform: Transform3D = goal_parking_spot
200
+ goal_transform.origin.y = global_position.y
201
+ return goal_transform.origin.distance_to(global_position)
202
+
203
+ func get_normalized_velocity_in_player_reference() -> Vector3:
204
+ return (
205
+ global_transform.basis.inverse() *
206
+ get_normalized_velocity()
207
+ )
208
+
209
+ func _on_green_space_body_entered(body):
210
+ _end_episode(episode_ended_unsuccessfully_reward)
211
+
212
+ func _on_walls_body_entered(body):
213
+ _end_episode(episode_ended_unsuccessfully_reward)
214
+
215
+ func _on_body_entered(body: PhysicsBody3D):
216
+ if body is StaticCar:
217
+ _end_episode(episode_ended_unsuccessfully_reward)
scenes/car/Car.tscn ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [gd_scene load_steps=9 format=3 uid="uid://cn2van3bgkmch"]
2
+
3
+ [ext_resource type="Script" path="res://scenes/car/Car.gd" id="1_22tkk"]
4
+ [ext_resource type="Material" uid="uid://chim7hdhvacuf" path="res://scenes/car/rear_light_braking.tres" id="2_on5f3"]
5
+ [ext_resource type="Material" uid="uid://d0fd3s7j6iaxc" path="res://scenes/car/rear_light_reversing.tres" id="3_ic80y"]
6
+ [ext_resource type="PackedScene" uid="uid://rfo6asayjvm8" path="res://scenes/blender/car_base.blend" id="4_5vyrv"]
7
+ [ext_resource type="PackedScene" uid="uid://d3saly1xwj5s" path="res://scenes/car/Wheel.tscn" id="5_htwgt"]
8
+ [ext_resource type="Script" path="res://scenes/car/CarAIController.gd" id="6_h3eqj"]
9
+ [ext_resource type="Script" path="res://addons/godot_rl_agents/sensors/sensors_3d/RaycastSensor3D.gd" id="7_r8420"]
10
+
11
+ [sub_resource type="ConvexPolygonShape3D" id="ConvexPolygonShape3D_ey2uy"]
12
+ points = PackedVector3Array(-1.07131, 0.109052, -1.54357, 1.08983, 0.83316, 1.90767, -1.10341, 0.822321, 1.8853, 0.989547, -0.97941, 1.03135, 1.08983, 0.83316, -1.90767, -1.01606, -0.97941, -1.03135, -1.01606, -0.97941, 1.03135, 0.989547, -0.97941, -1.03135, -1.10341, 0.822321, -1.8853, 1.04556, -0.463054, -1.25862, 1.05067, -0.349671, 1.32232, -1.07441, 0.683148, 1.83514, -1.07441, 0.683148, -1.83514, -1.07131, 0.109052, 1.54357, 1.04556, -0.463054, 1.25862, 1.05067, -0.349671, -1.32232)
13
+
14
+ [node name="Car" type="VehicleBody3D"]
15
+ collision_layer = 2
16
+ mass = 1000.0
17
+ continuous_cd = true
18
+ max_contacts_reported = 1
19
+ script = ExtResource("1_22tkk")
20
+ braking_material = ExtResource("2_on5f3")
21
+ reversing_material = ExtResource("3_ic80y")
22
+
23
+ [node name="CollisionShape3D" type="CollisionShape3D" parent="."]
24
+ transform = Transform3D(0.5, 0, 0, 0, -0.5, 7.54979e-08, 0, -7.54979e-08, -0.5, 0, 0, 0)
25
+ shape = SubResource("ConvexPolygonShape3D_ey2uy")
26
+
27
+ [node name="car_base" parent="." instance=ExtResource("4_5vyrv")]
28
+
29
+ [node name="BackWheel" type="VehicleWheel3D" parent="."]
30
+ transform = Transform3D(1, 0, 1.74846e-07, 0, 1, 0, -1.74846e-07, 0, 1, -0.7, -0.426319, -0.45)
31
+ use_as_traction = true
32
+ wheel_radius = 0.349
33
+ suspension_travel = 0.3
34
+ suspension_stiffness = 40.0
35
+
36
+ [node name="Wheel" parent="BackWheel" instance=ExtResource("5_htwgt")]
37
+ transform = Transform3D(-4.37114e-08, 1, -4.37114e-08, 0, -4.37114e-08, -1, -1, -4.37114e-08, 1.91069e-15, 0, 0, 0)
38
+
39
+ [node name="BackWheel2" type="VehicleWheel3D" parent="."]
40
+ transform = Transform3D(1, 0, 1.74846e-07, 0, 1, 0, -1.74846e-07, 0, 1, 0.7, -0.426473, -0.45)
41
+ use_as_traction = true
42
+ wheel_radius = 0.349
43
+ suspension_travel = 0.3
44
+ suspension_stiffness = 40.0
45
+
46
+ [node name="Wheel3" parent="BackWheel2" instance=ExtResource("5_htwgt")]
47
+ transform = Transform3D(-4.37114e-08, -1, 4.37114e-08, 0, -4.37114e-08, -1, 1, -4.37114e-08, 1.91069e-15, 0, 0, 0)
48
+
49
+ [node name="FrontWheel" type="VehicleWheel3D" parent="."]
50
+ transform = Transform3D(1, 0, 1.74846e-07, 0, 1, 0, -1.74846e-07, 0, 1, -0.7, -0.426473, 0.45)
51
+ use_as_traction = true
52
+ use_as_steering = true
53
+ wheel_radius = 0.349
54
+ suspension_travel = 0.3
55
+ suspension_stiffness = 40.0
56
+
57
+ [node name="Wheel2" parent="FrontWheel" instance=ExtResource("5_htwgt")]
58
+ transform = Transform3D(-4.37114e-08, -1, 4.37114e-08, 0, -4.37114e-08, -1, 1, -4.37114e-08, 1.91069e-15, 0, 0, 0)
59
+
60
+ [node name="FrontWheel2" type="VehicleWheel3D" parent="."]
61
+ transform = Transform3D(1, 0, 1.74846e-07, 0, 1, 0, -1.74846e-07, 0, 1, 0.7, -0.426473, 0.45)
62
+ use_as_traction = true
63
+ use_as_steering = true
64
+ wheel_radius = 0.349
65
+ suspension_travel = 0.3
66
+ suspension_stiffness = 40.0
67
+
68
+ [node name="Wheel4" parent="FrontWheel2" instance=ExtResource("5_htwgt")]
69
+ transform = Transform3D(-4.37114e-08, -1, 4.37114e-08, 0, -4.37114e-08, -1, 1, -4.37114e-08, 1.91069e-15, 0, 0, 0)
70
+
71
+ [node name="AIController3D" type="Node3D" parent="."]
72
+ script = ExtResource("6_h3eqj")
73
+ reset_after = 1750
74
+
75
+ [node name="RayCastSensor3D" type="Node3D" parent="."]
76
+ script = ExtResource("7_r8420")
77
+ n_rays_width = 17.0
78
+ n_rays_height = 1.0
79
+ cone_width = 360.0
80
+ cone_height = 0.0
81
+ collide_with_areas = true
82
+
83
+ [node name="@RayCast3D@23670" type="RayCast3D" parent="RayCastSensor3D"]
84
+ target_position = Vector3(-1.8375, 0, -9.82973)
85
+ collide_with_areas = true
86
+
87
+ [node name="node_1 0" type="RayCast3D" parent="RayCastSensor3D"]
88
+ target_position = Vector3(-5.26432, 0, -8.50217)
89
+ collide_with_areas = true
90
+
91
+ [node name="node_2 0" type="RayCast3D" parent="RayCastSensor3D"]
92
+ target_position = Vector3(-7.98017, 0, -6.02635)
93
+ collide_with_areas = true
94
+
95
+ [node name="node_3 0" type="RayCast3D" parent="RayCastSensor3D"]
96
+ target_position = Vector3(-9.61826, 0, -2.73663)
97
+ collide_with_areas = true
98
+
99
+ [node name="node_4 0" type="RayCast3D" parent="RayCastSensor3D"]
100
+ target_position = Vector3(-9.95734, 0, 0.922684)
101
+ collide_with_areas = true
102
+
103
+ [node name="node_5 0" type="RayCast3D" parent="RayCastSensor3D"]
104
+ target_position = Vector3(-8.95163, 0, 4.45738)
105
+ collide_with_areas = true
106
+
107
+ [node name="node_6 0" type="RayCast3D" parent="RayCastSensor3D"]
108
+ target_position = Vector3(-6.73696, 0, 7.39009)
109
+ collide_with_areas = true
110
+
111
+ [node name="node_7 0" type="RayCast3D" parent="RayCastSensor3D"]
112
+ target_position = Vector3(-3.61242, 0, 9.32472)
113
+ collide_with_areas = true
114
+
115
+ [node name="node_8 0" type="RayCast3D" parent="RayCastSensor3D"]
116
+ target_position = Vector3(0, 0, 10)
117
+ collide_with_areas = true
118
+
119
+ [node name="node_9 0" type="RayCast3D" parent="RayCastSensor3D"]
120
+ target_position = Vector3(3.61242, 0, 9.32472)
121
+ collide_with_areas = true
122
+
123
+ [node name="node_10 0" type="RayCast3D" parent="RayCastSensor3D"]
124
+ target_position = Vector3(6.73696, 0, 7.39009)
125
+ collide_with_areas = true
126
+
127
+ [node name="node_11 0" type="RayCast3D" parent="RayCastSensor3D"]
128
+ target_position = Vector3(8.95163, 0, 4.45738)
129
+ collide_with_areas = true
130
+
131
+ [node name="node_12 0" type="RayCast3D" parent="RayCastSensor3D"]
132
+ target_position = Vector3(9.95734, 0, 0.922684)
133
+ collide_with_areas = true
134
+
135
+ [node name="node_13 0" type="RayCast3D" parent="RayCastSensor3D"]
136
+ target_position = Vector3(9.61826, 0, -2.73663)
137
+ collide_with_areas = true
138
+
139
+ [node name="node_14 0" type="RayCast3D" parent="RayCastSensor3D"]
140
+ target_position = Vector3(7.98017, 0, -6.02635)
141
+ collide_with_areas = true
142
+
143
+ [node name="node_15 0" type="RayCast3D" parent="RayCastSensor3D"]
144
+ target_position = Vector3(5.26432, 0, -8.50217)
145
+ collide_with_areas = true
146
+
147
+ [node name="node_16 0" type="RayCast3D" parent="RayCastSensor3D"]
148
+ target_position = Vector3(1.8375, 0, -9.82973)
149
+ collide_with_areas = true
150
+
151
+ [connection signal="body_entered" from="." to="." method="_on_body_entered"]
scenes/car/CarAIController.gd ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ extends AIController3D
2
+ class_name CarAIController
3
+
4
+ func get_obs() -> Dictionary:
5
+
6
+ # Positions and velocities are converted to the player's frame of reference
7
+ var player_velocity = _player.get_normalized_velocity_in_player_reference()
8
+
9
+ var observations : Array = [
10
+ n_steps / float(reset_after),
11
+ player_velocity.x,
12
+ player_velocity.z,
13
+ _player.angular_velocity.y * 1.5,
14
+ _player.steering / deg_to_rad(_player.max_steer_angle)
15
+ ]
16
+
17
+ # After the first reset, the goal parking position will be assigned,
18
+ # so we provide zero values for those positions before then
19
+ # (used for detecting the size of the observation space)
20
+ if _player.times_restarted == 0:
21
+ observations.append_array([0.0, 0.0, 0.0, 0.0])
22
+ else:
23
+ var goal_transform: Transform3D = _player.goal_parking_spot
24
+ var goal_position = (
25
+ _player.to_local(goal_transform.origin) /
26
+ Vector2(
27
+ _player.playing_area_x_size,
28
+ _player.playing_area_z_size
29
+ ).length()
30
+ )
31
+ observations.append_array(
32
+ [
33
+ goal_position.x,
34
+ goal_position.z,
35
+ (goal_transform.basis.z - _player.global_transform.basis.z).x,
36
+ (goal_transform.basis.z - _player.global_transform.basis.z).z,
37
+ ]
38
+ )
39
+
40
+ observations.append_array(_player.raycast_sensor.get_observation())
41
+ return {"obs": observations}
42
+
43
+ func get_reward() -> float:
44
+ return reward
45
+
46
+ func get_action_space() -> Dictionary:
47
+ return {
48
+ "acceleration" : {
49
+ "size": 1,
50
+ "action_type": "continuous"
51
+ },
52
+ "steering" : {
53
+ "size": 1,
54
+ "action_type": "continuous"
55
+ },
56
+ }
57
+
58
+ func _physics_process(delta):
59
+ n_steps += 1
60
+ if n_steps > reset_after:
61
+ needs_reset = true
62
+ done = true
63
+
64
+ func set_action(action) -> void:
65
+ _player.requested_acceleration = clampf(action.acceleration[0], -1.0, 1.0)
66
+ _player.requested_steering = clampf(action.steering[0], -1.0, 1.0)
scenes/car/StaticCar.gd ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ extends StaticBody3D
2
+ class_name StaticCar
3
+
4
+ var _material: StandardMaterial3D
5
+ var _car_body: MeshInstance3D
6
+
7
+ func _ready():
8
+ _car_body = $"car_base/Body" as MeshInstance3D
9
+ _material = _car_body.get_active_material(0)
10
+
11
+ func randomize_color():
12
+ var instance_material = _material.duplicate()
13
+ instance_material.albedo_color = Color(
14
+ randf_range(0.2, 0.8),
15
+ randf_range(0.2, 0.8),
16
+ randf_range(0.2, 0.8),
17
+ 1
18
+ )
19
+ _car_body.set_surface_override_material(0, instance_material)
scenes/car/StaticCar.tscn ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [gd_scene load_steps=5 format=3 uid="uid://cvaj7gdsqflcd"]
2
+
3
+ [ext_resource type="Script" path="res://scenes/car/StaticCar.gd" id="1_pf02e"]
4
+ [ext_resource type="PackedScene" uid="uid://d3saly1xwj5s" path="res://scenes/car/Wheel.tscn" id="2_tfmok"]
5
+ [ext_resource type="PackedScene" uid="uid://rfo6asayjvm8" path="res://scenes/blender/car_base.blend" id="3_wccuk"]
6
+
7
+ [sub_resource type="BoxShape3D" id="BoxShape3D_7x1u3"]
8
+ size = Vector3(1.5, 1.5, 1.85183)
9
+
10
+ [node name="StaticCart" type="StaticBody3D"]
11
+ script = ExtResource("1_pf02e")
12
+
13
+ [node name="Wheel" parent="." instance=ExtResource("2_tfmok")]
14
+ transform = Transform3D(-2.18557e-07, 1, -4.37114e-08, 0, -4.37114e-08, -1, -1, -2.18557e-07, 9.55345e-15, -0.7, -0.426319, -0.45)
15
+
16
+ [node name="Wheel3" parent="." instance=ExtResource("2_tfmok")]
17
+ transform = Transform3D(1.31135e-07, -1, 4.37114e-08, 0, -4.37114e-08, -1, 1, 1.31135e-07, -5.73207e-15, 0.7, -0.426473, -0.45)
18
+
19
+ [node name="Wheel2" parent="." instance=ExtResource("2_tfmok")]
20
+ transform = Transform3D(1.31135e-07, -1, 4.37114e-08, 0, -4.37114e-08, -1, 1, 1.31135e-07, -5.73207e-15, -0.7, -0.426473, 0.45)
21
+
22
+ [node name="Wheel4" parent="." instance=ExtResource("2_tfmok")]
23
+ transform = Transform3D(1.31135e-07, -1, 4.37114e-08, 0, -4.37114e-08, -1, 1, 1.31135e-07, -5.73207e-15, 0.7, -0.426473, 0.45)
24
+
25
+ [node name="CollisionShape3D" type="CollisionShape3D" parent="."]
26
+ shape = SubResource("BoxShape3D_7x1u3")
27
+
28
+ [node name="car_base" parent="." instance=ExtResource("3_wccuk")]
scenes/car/Wheel.tscn ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [gd_scene load_steps=3 format=3 uid="uid://d3saly1xwj5s"]
2
+
3
+ [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_emc8j"]
4
+ albedo_color = Color(0.0235294, 0.0235294, 0.0235294, 1)
5
+ roughness = 0.46
6
+
7
+ [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_5qqye"]
8
+ albedo_color = Color(0.619608, 0.619608, 0.619608, 1)
9
+
10
+ [node name="Wheel" type="CSGTorus3D"]
11
+ transform = Transform3D(1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0)
12
+ inner_radius = 0.191
13
+ outer_radius = 0.349
14
+ sides = 16
15
+ ring_sides = 5
16
+ material = SubResource("StandardMaterial3D_emc8j")
17
+
18
+ [node name="CSGCylinder3D" type="CSGCylinder3D" parent="."]
19
+ radius = 0.296
20
+ height = 0.112
21
+ material = SubResource("StandardMaterial3D_5qqye")
scenes/car/rear_light_braking.tres ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ [gd_resource type="StandardMaterial3D" format=3 uid="uid://chim7hdhvacuf"]
2
+
3
+ [resource]
4
+ resource_local_to_scene = true
5
+ albedo_color = Color(1, 0, 0, 1)
scenes/car/rear_light_reversing.tres ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ [gd_resource type="StandardMaterial3D" format=3 uid="uid://d0fd3s7j6iaxc"]
2
+
3
+ [resource]
4
+ resource_local_to_scene = true
5
+ emission = Color(1, 1, 1, 1)
scenes/car/static_cart_material.tres ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ [gd_resource type="StandardMaterial3D" format=3 uid="uid://bn1o1m8nhe72c"]
2
+
3
+ [resource]
4
+ resource_local_to_scene = true
5
+ metallic = 0.16
6
+ roughness = 0.29
scenes/game_scene/GameScene.tscn ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [gd_scene load_steps=15 format=3 uid="uid://ca6csk8y5nvuu"]
2
+
3
+ [ext_resource type="PackedScene" uid="uid://cn2van3bgkmch" path="res://scenes/car/Car.tscn" id="1_5vpyy"]
4
+ [ext_resource type="Script" path="res://scenes/game_scene/ParkingSpotManager.gd" id="3_0vbrg"]
5
+ [ext_resource type="Script" path="res://scenes/game_scene/ParkingManager.gd" id="3_x8b77"]
6
+ [ext_resource type="PackedScene" uid="uid://cvaj7gdsqflcd" path="res://scenes/car/StaticCar.tscn" id="4_tyjtq"]
7
+ [ext_resource type="PackedScene" uid="uid://cc7x3j2c0bo3i" path="res://scenes/blender/parking.blend" id="5_x8abj"]
8
+
9
+ [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_u0d82"]
10
+ resource_local_to_scene = true
11
+ shading_mode = 0
12
+ albedo_color = Color(1, 0, 0, 1)
13
+
14
+ [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_nr3lu"]
15
+ resource_local_to_scene = true
16
+ shading_mode = 0
17
+
18
+ [sub_resource type="BoxShape3D" id="BoxShape3D_nxaf6"]
19
+ size = Vector3(20, 5, 20)
20
+
21
+ [sub_resource type="BoxShape3D" id="BoxShape3D_evajx"]
22
+ size = Vector3(12.2, 2.5, 2)
23
+
24
+ [sub_resource type="BoxShape3D" id="BoxShape3D_75vn3"]
25
+ size = Vector3(2, 10, 23.5)
26
+
27
+ [sub_resource type="ConcavePolygonShape3D" id="ConcavePolygonShape3D_65cdh"]
28
+ data = PackedVector3Array(5.9011, -0.0092, -0.8469, -5.9011, -0.0092, 0.8469, -5.9011, -0.0092, -0.8469, 5.9011, -0.0092, -0.8469, 5.9011, -0.0092, 0.8469, -5.9011, -0.0092, 0.8469, 5.9011, -0.0092, 0.8469, -6.0066, 0.1706, 0.9483, -5.9011, -0.0092, 0.8469, 5.9011, -0.0092, 0.8469, 6.0066, 0.1706, 0.9483, -6.0066, 0.1706, 0.9483, -5.9011, -0.0092, -0.8469, 6.0066, 0.1706, -0.9483, 5.9011, -0.0092, -0.8469, -5.9011, -0.0092, -0.8469, -6.0066, 0.1706, -0.9483, 6.0066, 0.1706, -0.9483, 5.9011, -0.0092, -0.8469, 6.0066, 0.1706, 0.9483, 5.9011, -0.0092, 0.8469, 5.9011, -0.0092, -0.8469, 6.0066, 0.1706, -0.9483, 6.0066, 0.1706, 0.9483, -6.0737, -0.0534, 1.0128, -6.0737, 0.1348, -1.0128, -6.0737, 0.1348, 1.0128, -6.0737, -0.0534, 1.0128, -6.0737, -0.0534, -1.0128, -6.0737, 0.1348, -1.0128, -6.0737, -0.0534, -1.0128, 6.0737, -0.0534, 1.0128, 6.0737, -0.0534, -1.0128, -6.0737, -0.0534, -1.0128, -6.0737, -0.0534, 1.0128, 6.0737, -0.0534, 1.0128, -6.0737, -0.0534, -1.0128, 6.0737, 0.1348, -1.0128, -6.0737, 0.1348, -1.0128, -6.0737, -0.0534, -1.0128, 6.0737, -0.0534, -1.0128, 6.0737, 0.1348, -1.0128, 6.0737, -0.0534, -1.0128, 6.0737, 0.1348, 1.0128, 6.0737, 0.1348, -1.0128, 6.0737, -0.0534, -1.0128, 6.0737, -0.0534, 1.0128, 6.0737, 0.1348, 1.0128, -5.9011, -0.0092, 0.8469, -6.0066, 0.1706, -0.9483, -5.9011, -0.0092, -0.8469, -5.9011, -0.0092, 0.8469, -6.0066, 0.1706, 0.9483, -6.0066, 0.1706, -0.9483, -6.0066, 0.1706, 0.9483, -6.0737, 0.1348, -1.0128, -6.0066, 0.1706, -0.9483, -6.0066, 0.1706, 0.9483, -6.0737, 0.1348, 1.0128, -6.0737, 0.1348, -1.0128, -6.0066, 0.1706, -0.9483, 6.0737, 0.1348, -1.0128, 6.0066, 0.1706, -0.9483, -6.0066, 0.1706, -0.9483, -6.0737, 0.1348, -1.0128, 6.0737, 0.1348, -1.0128, 6.0066, 0.1706, -0.9483, 6.0737, 0.1348, 1.0128, 6.0066, 0.1706, 0.9483, 6.0066, 0.1706, -0.9483, 6.0737, 0.1348, -1.0128, 6.0737, 0.1348, 1.0128, 6.0066, 0.1706, 0.9483, -6.0737, 0.1348, 1.0128, -6.0066, 0.1706, 0.9483, 6.0066, 0.1706, 0.9483, 6.0737, 0.1348, 1.0128, -6.0737, 0.1348, 1.0128, 6.0737, -0.0534, 1.0128, -6.0737, 0.1348, 1.0128, 6.0737, 0.1348, 1.0128, 6.0737, -0.0534, 1.0128, -6.0737, -0.0534, 1.0128, -6.0737, 0.1348, 1.0128)
29
+
30
+ [sub_resource type="PrismMesh" id="PrismMesh_f41ct"]
31
+
32
+ [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_cbhkg"]
33
+ albedo_color = Color(0, 1, 0.0627451, 1)
34
+
35
+ [sub_resource type="BoxMesh" id="BoxMesh_0kec2"]
36
+ size = Vector3(0.5, 1, 0.5)
37
+
38
+ [node name="GameScene" type="Node3D"]
39
+
40
+ [node name="Car" parent="." node_paths=PackedStringArray("parking_manager", "goal_marker") instance=ExtResource("1_5vpyy")]
41
+ transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 7.22273, 0.919515, 7.72317)
42
+ collision_mask = 513
43
+ contact_monitor = true
44
+ parking_manager = NodePath("../Parking")
45
+ goal_marker = NodePath("../GoalMarker")
46
+ braking_material = SubResource("StandardMaterial3D_u0d82")
47
+ reversing_material = SubResource("StandardMaterial3D_nr3lu")
48
+
49
+ [node name="Static" type="Node3D" parent="."]
50
+
51
+ [node name="Ground" type="StaticBody3D" parent="Static"]
52
+ collision_layer = 512
53
+
54
+ [node name="CollisionShape3D" type="CollisionShape3D" parent="Static/Ground"]
55
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -2.5, 0)
56
+ shape = SubResource("BoxShape3D_nxaf6")
57
+
58
+ [node name="GreenSpace" type="Area3D" parent="Static"]
59
+ collision_mask = 3
60
+
61
+ [node name="CollisionShape3D" type="CollisionShape3D" parent="Static/GreenSpace"]
62
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.32474, 0)
63
+ shape = SubResource("BoxShape3D_evajx")
64
+
65
+ [node name="Walls" type="Area3D" parent="Static"]
66
+ collision_mask = 3
67
+
68
+ [node name="CollisionShape3D" type="CollisionShape3D" parent="Static/Walls"]
69
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 10.75, 0, 0)
70
+ shape = SubResource("BoxShape3D_75vn3")
71
+
72
+ [node name="CollisionShape3D4" type="CollisionShape3D" parent="Static/Walls"]
73
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -10.75, 0, 0)
74
+ shape = SubResource("BoxShape3D_75vn3")
75
+
76
+ [node name="CollisionShape3D2" type="CollisionShape3D" parent="Static/Walls"]
77
+ transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 0, 0, 10.75)
78
+ shape = SubResource("BoxShape3D_75vn3")
79
+
80
+ [node name="CollisionShape3D3" type="CollisionShape3D" parent="Static/Walls"]
81
+ transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 0, 0, -10.75)
82
+ shape = SubResource("BoxShape3D_75vn3")
83
+
84
+ [node name="Parking" parent="." instance=ExtResource("5_x8abj")]
85
+ script = ExtResource("3_x8b77")
86
+ parked_car_transform_randomness = 0.35
87
+
88
+ [node name="StaticBody3D" type="StaticBody3D" parent="Parking/GreenSpace" index="2"]
89
+
90
+ [node name="CollisionShape3D" type="CollisionShape3D" parent="Parking/GreenSpace/StaticBody3D"]
91
+ shape = SubResource("ConcavePolygonShape3D_65cdh")
92
+
93
+ [node name="ParkingSpots" parent="Parking" index="4"]
94
+ script = ExtResource("3_0vbrg")
95
+
96
+ [node name="Cart" parent="Parking/ParkingSpots/ParkingSpot" index="0" instance=ExtResource("4_tyjtq")]
97
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.704576, -0.203074)
98
+
99
+ [node name="Cart2" parent="Parking/ParkingSpots/ParkingSpot_001" index="0" instance=ExtResource("4_tyjtq")]
100
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.705, -0.203)
101
+
102
+ [node name="Cart3" parent="Parking/ParkingSpots/ParkingSpot_002" index="0" instance=ExtResource("4_tyjtq")]
103
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.705, -0.203)
104
+
105
+ [node name="Cart4" parent="Parking/ParkingSpots/ParkingSpot_003" index="0" instance=ExtResource("4_tyjtq")]
106
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.705, -0.203)
107
+
108
+ [node name="Cart5" parent="Parking/ParkingSpots/ParkingSpot_004" index="0" instance=ExtResource("4_tyjtq")]
109
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.705, -0.203)
110
+
111
+ [node name="Cart6" parent="Parking/ParkingSpots/ParkingSpot_005" index="0" instance=ExtResource("4_tyjtq")]
112
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.705, 0.203)
113
+
114
+ [node name="Cart7" parent="Parking/ParkingSpots/ParkingSpot_006" index="0" instance=ExtResource("4_tyjtq")]
115
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.705, 0.203)
116
+
117
+ [node name="Cart8" parent="Parking/ParkingSpots/ParkingSpot_007" index="0" instance=ExtResource("4_tyjtq")]
118
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.705, 0.203)
119
+
120
+ [node name="Cart9" parent="Parking/ParkingSpots/ParkingSpot_008" index="0" instance=ExtResource("4_tyjtq")]
121
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.705, 0.203)
122
+
123
+ [node name="Cart10" parent="Parking/ParkingSpots/ParkingSpot_009" index="0" instance=ExtResource("4_tyjtq")]
124
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.705, 0.203)
125
+
126
+ [node name="GoalMarker" type="MeshInstance3D" parent="."]
127
+ transform = Transform3D(1, 0, 0, 0, -1, 8.74228e-08, 0, -8.74228e-08, -1, 0, 2.17536, 0)
128
+ mesh = SubResource("PrismMesh_f41ct")
129
+ surface_material_override/0 = SubResource("StandardMaterial3D_cbhkg")
130
+
131
+ [node name="MeshInstance3D" type="MeshInstance3D" parent="GoalMarker"]
132
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0)
133
+ mesh = SubResource("BoxMesh_0kec2")
134
+ surface_material_override/0 = SubResource("StandardMaterial3D_cbhkg")
135
+
136
+ [connection signal="body_entered" from="Static/GreenSpace" to="Car" method="_on_green_space_body_entered"]
137
+ [connection signal="body_entered" from="Static/Walls" to="Car" method="_on_walls_body_entered"]
138
+
139
+ [editable path="Parking"]
scenes/game_scene/ParkingManager.gd ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ extends Node3D
2
+ class_name ParkingManager
3
+
4
+ @export var disable_random_amount_of_cars_each_episode := true
5
+ @export var disable_parked_cars_count := 1
6
+ @export var parked_car_transform_randomness := 0.2
7
+
8
+ @onready var _parking_spot_manager: ParkingSpotManager = $ParkingSpots
9
+
10
+ func _ready():
11
+ _parking_spot_manager.parked_car_transform_randomness = parked_car_transform_randomness
12
+
13
+ func disable_random_cars():
14
+ _parking_spot_manager.disable_random_cars(
15
+ disable_parked_cars_count if not disable_random_amount_of_cars_each_episode
16
+ else randi_range(1, _parking_spot_manager.total_cars)
17
+ )
18
+
19
+ func get_parking_spots() -> Array[Node3D]:
20
+ return _parking_spot_manager.parking_spots
21
+
22
+ func get_closest_free_parking_spot_transform(from_global_position: Vector3):
23
+ var closest_distance: float = INF
24
+ var closest_transform: Transform3D
25
+
26
+ for parking_spot in _parking_spot_manager.parking_spots:
27
+ if not parking_spot.visible:
28
+ var distance: float = from_global_position.distance_to(parking_spot.global_position)
29
+ if distance < closest_distance:
30
+ closest_distance = distance
31
+ closest_transform = parking_spot.global_transform
32
+
33
+ assert(closest_distance != INF, "Closest distance is INF")
34
+ return closest_transform
35
+
36
+ func get_random_free_parking_spot_transform():
37
+ return _parking_spot_manager.get_random_free_parking_spot_transform()
scenes/game_scene/ParkingSpotManager.gd ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ extends Node3D
2
+ class_name ParkingSpotManager
3
+
4
+ var total_cars: int
5
+ var parking_spots: Array[Node3D]
6
+ var free_parking_spots: Array[Node3D]
7
+ var parked_car_transform_randomness
8
+
9
+ func _ready():
10
+ total_cars = get_child_count()
11
+ parking_spots.append_array(get_children())
12
+
13
+ func disable_random_cars(number_of_cars_to_disable: int):
14
+ assert(number_of_cars_to_disable <= total_cars,
15
+ "Can't disable more than the total number of parking_spots.")
16
+ var disabled_cars: int = 0
17
+ free_parking_spots.clear()
18
+
19
+ parking_spots.shuffle()
20
+
21
+ for parking_spot in parking_spots:
22
+ if disabled_cars < number_of_cars_to_disable:
23
+ parking_spot.process_mode = Node.PROCESS_MODE_DISABLED
24
+ parking_spot.hide()
25
+ free_parking_spots.append(parking_spot)
26
+ else:
27
+ parking_spot.process_mode = Node.PROCESS_MODE_INHERIT
28
+ parking_spot.show()
29
+
30
+ var car: Node3D = parking_spot.get_child(0)
31
+ randomize_car_transform(car, parking_spot)
32
+ randomize_car_color(car)
33
+ disabled_cars += 1
34
+
35
+ func randomize_car_color(car: Node3D):
36
+ car.randomize_color()
37
+
38
+ func randomize_car_transform(car: Node3D, parking_space: Node3D):
39
+ var randomness = parked_car_transform_randomness
40
+ var rotation_randomness = randomness * 0.35
41
+ var parking_space_position = parking_space.global_position
42
+
43
+ car.global_position.x = parking_space_position.x + randf_range(-randomness, randomness)
44
+ car.global_position.z = parking_space_position.z + randf_range(-randomness, randomness)
45
+ car.global_rotation.y = parking_space.global_rotation.y + randf_range(-rotation_randomness, rotation_randomness)
46
+
47
+ func get_random_free_parking_spot_transform():
48
+ return free_parking_spots.pick_random().global_transform
scenes/sync_override.gd ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ extends "res://addons/godot_rl_agents/sync.gd"
2
+
3
+ # Lowers the physics ticks per second
4
+ func _initialize():
5
+ super._initialize()
6
+ Engine.physics_ticks_per_second = _get_speedup() * 30