From 80ae65c08a36cca4c42060e5d90b6f58696d75f3 Mon Sep 17 00:00:00 2001 From: dusoleil Date: Fri, 16 Sep 2022 22:14:34 -0400 Subject: Fix rounding error in chunk render distance --- World.gd | 47 +++++++++++++++++++++++------------------------ World.tscn | 12 ++++++------ 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/World.gd b/World.gd index 6aba103..da811a8 100644 --- a/World.gd +++ b/World.gd @@ -5,42 +5,41 @@ onready var _traveler = get_node(traveler) export var acceleration = 10.0 onready var Chunk = preload("res://Chunk.tscn") -onready var chunk_size = $"WorldGen/Chunk/Size".shape.radius*2.0 +onready var chunk_size = $"Chunks/Chunk/Size".shape.radius*2.0 var chunks = {} -func v2_coords(xform:Transform): - var coords = xform.origin - coords = Vector2(coords.x,coords.z) - return coords +func v2_coords(coords:Vector3): + return Vector2(coords.x,coords.z) -func chunk_coords(coords:Vector2): - coords = (coords / chunk_size).floor() - return coords - -func world_coords(coords:Vector2): - coords *= chunk_size +func v3_coords(coords:Vector2): return Vector3(coords.x,0.0,coords.y) -func gen_chunk(xform:Transform): +func chunk_coords(coords:Vector2): + return coords.snapped(Vector2(chunk_size,chunk_size)) + +func gen_chunk(coords:Vector2): var chunk = Chunk.instance() - chunk.transform = xform - $"WorldGen".add_child(chunk) - chunks[chunk_coords(v2_coords(xform))] = chunk + chunk.transform.origin = v3_coords(coords) + $"Chunks".add_child(chunk) + chunks[coords] = chunk func gen_chunks(): - var gen_center = -v2_coords($"WorldGen".transform) - var gen_radius = $"WorldGenCull/Radius".shape.radius-chunk_size - for x in range(-gen_radius, gen_radius, chunk_size): - for y in range(-gen_radius, gen_radius, chunk_size): + var gen_center = -v2_coords($"Chunks".transform.origin) + var gen_radius = $"ChunkRenderDistance/Radius".shape.radius + var gen_radius_rounded = stepify(gen_radius, chunk_size) + for x in range(-gen_radius_rounded, gen_radius_rounded+chunk_size, chunk_size): + for y in range(-gen_radius_rounded, gen_radius_rounded+chunk_size, chunk_size): var gen_coords = Vector2(x,y) + if gen_coords.length() > gen_radius: + continue var coords = chunk_coords(gen_coords + gen_center) - if gen_coords.length() <= gen_radius && !chunks.has(coords): - gen_chunk(Transform(Basis(), world_coords(coords))) + if !chunks.has(coords): + gen_chunk(coords) -func _on_WorldGenCull_area_exited(area:Area): +func _on_ChunkRenderDistance_area_exited(area:Area): gen_chunks() area.queue_free() - chunks.erase(chunk_coords(v2_coords(area.transform))) + chunks.erase(v2_coords(area.transform.origin)) func _ready(): gen_chunks() @@ -51,7 +50,7 @@ func travel(direction:Vector3): direction = -direction direction = heading * direction direction = direction.normalized() - $"WorldGen".add_central_force(direction*acceleration) + $"Chunks".add_central_force(direction*acceleration) func _physics_process(_state): var stick = Input.get_axis("ship_down","ship_up") diff --git a/World.tscn b/World.tscn index 67e4848..2f4ccec 100644 --- a/World.tscn +++ b/World.tscn @@ -11,7 +11,7 @@ radius = 1000.0 [node name="World" type="Spatial"] script = ExtResource( 1 ) -[node name="WorldGen" type="RigidBody" parent="."] +[node name="Chunks" type="RigidBody" parent="."] collision_layer = 0 collision_mask = 0 can_sleep = false @@ -21,18 +21,18 @@ axis_lock_angular_y = true axis_lock_angular_z = true linear_damp = 0.5 -[node name="CollisionShape" type="CollisionShape" parent="WorldGen"] +[node name="CollisionShape" type="CollisionShape" parent="Chunks"] shape = SubResource( 23 ) -[node name="Chunk" parent="WorldGen" instance=ExtResource( 3 )] +[node name="Chunk" parent="Chunks" instance=ExtResource( 3 )] visible = false collision_layer = 0 -[node name="WorldGenCull" type="Area" parent="."] +[node name="ChunkRenderDistance" type="Area" parent="."] collision_layer = 0 collision_mask = 32 -[node name="Radius" type="CollisionShape" parent="WorldGenCull"] +[node name="Radius" type="CollisionShape" parent="ChunkRenderDistance"] shape = SubResource( 24 ) -[connection signal="area_exited" from="WorldGenCull" to="." method="_on_WorldGenCull_area_exited"] +[connection signal="area_exited" from="ChunkRenderDistance" to="." method="_on_ChunkRenderDistance_area_exited"] -- cgit v1.2.3