aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@blos.sm>2023-07-09 14:02:37 +0100
committerLibravatar cel 🌸 <cel@blos.sm>2023-07-09 14:02:37 +0100
commitae7b7226c64e7b9ce62e045991c315040563fe5a (patch)
treeabca4450641dee1a63150d45eeaf4fb3b6fb7676
parentf1677fac1f7c7989a28d078498357c88f9f631f3 (diff)
download2023-ae7b7226c64e7b9ce62e045991c315040563fe5a.tar.gz
2023-ae7b7226c64e7b9ce62e045991c315040563fe5a.tar.bz2
2023-ae7b7226c64e7b9ce62e045991c315040563fe5a.zip
road simplification via Ramer–Douglas–Peucker
-rw-r--r--Assets/road.pngbin0 -> 252 bytes
-rw-r--r--Assets/road.png.import34
-rw-r--r--Nodes/Level_test.tscn11
-rw-r--r--Scripts/Road.gd48
-rw-r--r--Scripts/drawing.gd3
5 files changed, 92 insertions, 4 deletions
diff --git a/Assets/road.png b/Assets/road.png
new file mode 100644
index 0000000..5acc2e9
--- /dev/null
+++ b/Assets/road.png
Binary files differ
diff --git a/Assets/road.png.import b/Assets/road.png.import
new file mode 100644
index 0000000..59a81a8
--- /dev/null
+++ b/Assets/road.png.import
@@ -0,0 +1,34 @@
+[remap]
+
+importer="texture"
+type="CompressedTexture2D"
+uid="uid://bcn7tgs6a4fm7"
+path="res://.godot/imported/road.png-c3c47e2cc8c90beae2ad5c8a3bb2c3ca.ctex"
+metadata={
+"vram_texture": false
+}
+
+[deps]
+
+source_file="res://Assets/road.png"
+dest_files=["res://.godot/imported/road.png-c3c47e2cc8c90beae2ad5c8a3bb2c3ca.ctex"]
+
+[params]
+
+compress/mode=0
+compress/high_quality=false
+compress/lossy_quality=0.7
+compress/hdr_compression=1
+compress/normal_map=0
+compress/channel_pack=0
+mipmaps/generate=false
+mipmaps/limit=-1
+roughness/mode=0
+roughness/src_normal=""
+process/fix_alpha_border=true
+process/premult_alpha=false
+process/normal_map_invert_y=false
+process/hdr_as_srgb=false
+process/hdr_clamp_exposure=false
+process/size_limit=0
+detect_3d/compress_to=1
diff --git a/Nodes/Level_test.tscn b/Nodes/Level_test.tscn
index f082198..0703906 100644
--- a/Nodes/Level_test.tscn
+++ b/Nodes/Level_test.tscn
@@ -1,4 +1,4 @@
-[gd_scene load_steps=11 format=3 uid="uid://dliou1u4o6bib"]
+[gd_scene load_steps=12 format=3 uid="uid://dliou1u4o6bib"]
[ext_resource type="Script" path="res://Scripts/scene_control.gd" id="1_0esj3"]
[ext_resource type="Texture2D" uid="uid://5xqahgawme4m" path="res://Assets/start.png" id="1_wenkl"]
@@ -6,7 +6,8 @@
[ext_resource type="Texture2D" uid="uid://dg3v488vx0oj6" path="res://Assets/checkpoint.png" id="3_cgigr"]
[ext_resource type="PackedScene" uid="uid://c1mvbh2fwqtkb" path="res://Nodes/car.tscn" id="4_xvobs"]
[ext_resource type="Script" path="res://Scripts/drawing.gd" id="5_x7h3g"]
-[ext_resource type="PackedScene" uid="uid://wkfai2lvnp7n" path="res://Nodes/checkpoint.tscn" id="7_52mhf"]
+[ext_resource type="PackedScene" path="res://Nodes/checkpoint.tscn" id="7_52mhf"]
+[ext_resource type="Script" path="res://Scripts/Road.gd" id="7_nyjk7"]
[ext_resource type="PackedScene" uid="uid://c8r6i3bu37ogu" path="res://Nodes/ui.tscn" id="8_8u1ba"]
[sub_resource type="CircleShape2D" id="CircleShape2D_6g88q"]
@@ -42,7 +43,11 @@ acceleration = 0.05
[node name="DrawNode" type="Node2D" parent="."]
script = ExtResource("5_x7h3g")
-[node name="TrackLine" type="Line2D" parent="DrawNode"]
+[node name="Road" type="Line2D" parent="DrawNode"]
+width = 100.0
+default_color = Color(0.133333, 0.133333, 0.133333, 1)
+joint_mode = 2
+script = ExtResource("7_nyjk7")
[node name="TrackPath" type="Path2D" parent="DrawNode"]
curve = SubResource("Curve2D_0exre")
diff --git a/Scripts/Road.gd b/Scripts/Road.gd
new file mode 100644
index 0000000..e0b69a1
--- /dev/null
+++ b/Scripts/Road.gd
@@ -0,0 +1,48 @@
+extends Line2D
+
+var epsilon = 20
+
+# Called when the node enters the scene tree for the first time.
+func _ready():
+ pass # Replace with function body.
+
+
+# Called every frame. 'delta' is the elapsed time since the previous frame.
+func _process(delta):
+ pass
+
+func simplify():
+ points = simplify_rec(points, 5)
+
+func simplify_rec(pointArray: PackedVector2Array, epsilon: float):
+ var dmax = 0
+ var index = -1
+ if pointArray.size() < 3:
+ return pointArray
+ var end = pointArray.size() - 1
+ for i in range(1, end-1):
+ var d = find_perpendicular_distance(pointArray[i], pointArray[0], pointArray[end])
+ if d > dmax:
+ index = i
+ dmax = d
+
+ if dmax > epsilon:
+ var recResults1 = simplify_rec(pointArray.slice(0, index), epsilon)
+ var recResults2 = simplify_rec(pointArray.slice(index, end), epsilon)
+ recResults1.append_array(recResults2)
+ return recResults1
+ else:
+ return [pointArray[0], pointArray[end]]
+
+func find_perpendicular_distance(p: Vector2, p1: Vector2, p2: Vector2):
+ var result
+ var slope
+ var intercept
+ if p1.x == p2.x:
+ result = p.x - p1.x
+ else:
+ slope = (p2.y - p1.y) / (p2.x - p1.x)
+ intercept = p1.y - (slope * p1.x)
+ result = abs(slope * p.x - p.y + intercept) / sqrt(pow(slope, 2) + 1)
+
+ return result
diff --git a/Scripts/drawing.gd b/Scripts/drawing.gd
index 61896f9..c45196b 100644
--- a/Scripts/drawing.gd
+++ b/Scripts/drawing.gd
@@ -2,7 +2,7 @@ extends Node
var drawing = false
-@onready var line: Line2D = $TrackLine
+@onready var line = $Road
@onready var path: Path2D = $TrackPath
func _ready():
@@ -16,6 +16,7 @@ func start_drawing():
func _end_drawing():
drawing = false
+ line.simplify()
owner.set_driving(true)