From b384856adeed758ce3fe395f6b973639f21fc4d8 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Sun, 9 Jul 2023 16:21:38 +0100 Subject: add borders --- Nodes/Level_test.tscn | 16 +++++++++++++--- Scripts/Road.gd | 40 +++++++++++++++++++++++++++++++++++++--- Scripts/drawing.gd | 4 +++- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/Nodes/Level_test.tscn b/Nodes/Level_test.tscn index 0703906..dd45a9f 100644 --- a/Nodes/Level_test.tscn +++ b/Nodes/Level_test.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=12 format=3 uid="uid://dliou1u4o6bib"] +[gd_scene load_steps=13 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"] @@ -7,6 +7,7 @@ [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" path="res://Nodes/checkpoint.tscn" id="7_52mhf"] +[ext_resource type="Texture2D" uid="uid://bcn7tgs6a4fm7" path="res://Assets/road.png" id="7_mmi61"] [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"] @@ -44,11 +45,20 @@ acceleration = 0.05 script = ExtResource("5_x7h3g") [node name="Road" type="Line2D" parent="DrawNode"] +texture_repeat = 2 width = 100.0 -default_color = Color(0.133333, 0.133333, 0.133333, 1) -joint_mode = 2 +texture = ExtResource("7_mmi61") +texture_mode = 1 +joint_mode = 1 +round_precision = 16 script = ExtResource("7_nyjk7") +[node name="LeftBorder" type="Line2D" parent="DrawNode/Road"] +width = 2.0 + +[node name="RightBorder" type="Line2D" parent="DrawNode/Road"] +width = 2.0 + [node name="TrackPath" type="Path2D" parent="DrawNode"] curve = SubResource("Curve2D_0exre") diff --git a/Scripts/Road.gd b/Scripts/Road.gd index e0b69a1..4b19c70 100644 --- a/Scripts/Road.gd +++ b/Scripts/Road.gd @@ -1,18 +1,20 @@ extends Line2D -var epsilon = 20 +var epsilon = 2 + +@onready var leftBorder = $LeftBorder +@onready var rightBorder = $RightBorder # 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) + points = simplify_rec(points, epsilon) func simplify_rec(pointArray: PackedVector2Array, epsilon: float): var dmax = 0 @@ -46,3 +48,35 @@ func find_perpendicular_distance(p: Vector2, p1: Vector2, p2: Vector2): result = abs(slope * p.x - p.y + intercept) / sqrt(pow(slope, 2) + 1) return result + +func get_bisector(left_side: bool, p2: Vector2, p1: Vector2 = p2, p3: Vector2 = p2): + var v1 = (p2 - p1).normalized() + var v2 = (p2 - p3).normalized() + var normal + if v1 == -v2: + normal = v2.rotated(-PI/2) + else: + normal = (v1 + v2).normalized() + if v2.rotated(-PI/2).dot(v1) < 0: + normal = -normal + + if left_side: + normal = -normal + return normal + + +func draw_borders(): + for pointIndex in range(0, points.size() - 1): + var normal + if pointIndex == 0: + continue + elif pointIndex == points.size() -1: + continue + #normal = Vector2.ZERO + else: + normal = get_bisector(true, points[pointIndex], points[pointIndex-1], points[pointIndex+1]) + + var point = points[pointIndex] + (normal*50) + leftBorder.add_point(point) + point = points[pointIndex] - (normal*50) + rightBorder.add_point(point) diff --git a/Scripts/drawing.gd b/Scripts/drawing.gd index c45196b..a8068a2 100644 --- a/Scripts/drawing.gd +++ b/Scripts/drawing.gd @@ -17,13 +17,15 @@ func start_drawing(): func _end_drawing(): drawing = false line.simplify() + line.draw_borders() + for point in line.points: + path.curve.add_point(point) owner.set_driving(true) func _input(event): if drawing and event is InputEventMouseMotion: line.add_point(event.position) - path.curve.add_point(event.position) func reset(): -- cgit