Skip to content

Commit 22ae6f5

Browse files
authored
Merge pull request #1 from OpenSkyCity/jay-fixes
Some fixes
2 parents 5a58101 + 16ad1d6 commit 22ae6f5

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

TerrainChunked.gd

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ func _ready():
5454
if get_node_or_null(terrain_tool_node):
5555
terrain_tool = get_node(terrain_tool_node)
5656
# print(range(-_size/2-8, 64-/2+8, 16))
57-
for i in range((-_size+chunk_size)/2, (_size+chunk_size)/2, chunk_size):
58-
for j in range((-_size+chunk_size)/2, (_size+chunk_size)/2, chunk_size):
57+
var r = range((-_size+chunk_size)/2, (_size+chunk_size)/2, chunk_size)
58+
# var r = range((-2*chunk_size+chunk_size)/2, (2*chunk_size+chunk_size)/2, chunk_size)
59+
for i in r:
60+
for j in r:
5961
var image_offset: Vector2 = Vector2(range_lerp(i, -_size/2, _size/2, 0, _size), range_lerp(j, -_size/2, _size/2, 0, _size))
6062
var chunk = Chunk.new(Vector3(i, 0, j), chunk_size, 0, height_map, image_offset)
6163
add_child(chunk)
@@ -96,10 +98,16 @@ func _process(delta):
9698
var aabb = terrain_tool.get_transformed_aabb()
9799
aabb.position.y -= 50
98100
aabb.size.y += 100
99-
var queried_chunks = quad_tree.query(aabb)
101+
102+
# print("--------: ", aabb)
103+
104+
var vertex_size = 0.5
105+
106+
var queried_chunks = quad_tree.query(aabb.grow(vertex_size))
100107
for chunk in queried_chunks:
101-
var new_aabb = aabb.intersection(chunk.get_transformed_aabb())
108+
var new_aabb = aabb
102109
var old_aabb = (chunk.global_transform as Transform).xform_inv(new_aabb)
110+
# print("chunk: ", chunk, " (", chunk.get_transformed_aabb(), "), aabb: ", aabb, ", new: ", new_aabb, ", old: ", old_aabb)
103111
old_aabb.position.y -= 50
104112
old_aabb.size.y += 100
105113
(chunk as Chunk)._process_chunk(delta, terrain_tool, old_aabb)

TerrainTool.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func _unhandled_input(event):
3333
radius = mesh.bottom_radius
3434
attenuated_circle.radius = radius
3535
if Input.is_action_just_pressed("left_button"):
36+
position = translation
3637
for audio in sound_dict.values():
3738
audio.stop()
3839
sound_dict[current_state].play()

chunking/Chunk.gd

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func generate_mesh():
3636
mesh.size = Vector2(size, size)
3737

3838
if lod_level == 0:
39-
mesh.subdivide_width = size * 2
40-
mesh.subdivide_depth = size * 2
39+
mesh.subdivide_width = (size * 2)-1
40+
mesh.subdivide_depth = (size * 2)-1
4141
if lod_level == 1:
4242
mesh.subdivide_width = size
4343
mesh.subdivide_depth = size
@@ -47,7 +47,7 @@ func generate_mesh():
4747

4848
mesh_data_tool = create_datatool_from_mesh()
4949
generate_collision_mesh()
50-
50+
5151
setup_quadtree()
5252

5353
func setup_quadtree():
@@ -56,6 +56,7 @@ func setup_quadtree():
5656
var spatial_vertex = Spatial.new()
5757
spatial_vertex.translation = vertex
5858
mesh_data_tool.set_vertex(i, Vector3(vertex.x, 0, vertex.z))
59+
# print("vertex ", i, ": ", vertex)
5960
spatial_vertex.set_meta("i", i)
6061
quad_tree.add_body(spatial_vertex, _get_common_bounds(vertex))
6162
quad_tree.draw(1, true, true, true, global_transform)
@@ -65,11 +66,20 @@ func _get_common_bounds(vertex):
6566
return AABB(Vector3(vertex.x-0.25, vertex.y, vertex.z-0.25), Vector3(0.5, 0, 0.5))
6667

6768
func _process_chunk(delta, terrain_tool, aabb: AABB):
68-
var bodies = quad_tree.query(aabb)
69+
var vertex_size = 0.5
70+
aabb.position.x = floor(aabb.position.x / vertex_size)*vertex_size
71+
aabb.position.z = floor(aabb.position.z / vertex_size)*vertex_size
72+
aabb.end.x = ceil(aabb.end.x / vertex_size)*vertex_size
73+
aabb.end.x = ceil(aabb.end.x / vertex_size)*vertex_size
74+
75+
var bodies = quad_tree.query(aabb.grow(0.05))
76+
# print("chunk: ", self, "aabb:", aabb, " -> ", bodies.size())
77+
6978
for body in bodies:
7079
var vertex = body.get_translation()
7180
var old_vertex = body.get_translation()
7281
var i = body.get_meta("i")
82+
7383
if !terrain_tool.current_state == terrain_tool.TerrainToolStates.SLOPE_FLATTEN:
7484
vertex.y += terrain_tool.get_strength_at_position(old_vertex, false) * ((2.0 * int(!terrain_tool.current_state == terrain_tool.TerrainToolStates.SLOPE_DOWN)) - 1) * delta
7585
else:
@@ -79,6 +89,7 @@ func _process_chunk(delta, terrain_tool, aabb: AABB):
7989
vertex.y += terrain_tool.get_strength_at_position(old_vertex, false) * ((2.0 * int(vertex.y < 0)) - 1) * delta
8090
vertex.y = clamp(vertex.y, -50, 100)
8191
if vertex != old_vertex:
92+
# print("vertex ", i, ": ", vertex, ": ", old_vertex.y, " -> ", vertex.y)
8293
mesh_data_tool.set_vertex(i, vertex)
8394
body.set_translation(vertex)
8495
quad_tree.update_body(body, _get_common_bounds(vertex))

0 commit comments

Comments
 (0)