nav collision

This commit is contained in:
veclavtlica
2026-11-01 16:00:46 +03:00
parent 7b74d0606f
commit 6bd30e9ad9
21 changed files with 1915 additions and 19 deletions
+37 -4
View File
@@ -32,25 +32,29 @@ func _prepare_polygons() -> void:
var polys := Geometry2D.triangulate_delaunay(polygon)
var pickled := Array()
for i in polys.size() / 3:
pickled.push_front(PackedInt32Array([polys[i * 3 + 0], polys[i * 3 + 1], polys[i * 3 + 2]]))
pickled.push_back(PackedInt32Array([polys[i * 3 + 0], polys[i * 3 + 1], polys[i * 3 + 2]]))
polygons = pickled
_fix_acute_polygons()
_fix_polygons()
func _fix_acute_polygons() -> void:
func _fix_polygons() -> void:
# Outside triangles are in reversed winding, find and eliminate.
var new := Array()
for poly in polygons as Array[PackedInt32Array]:
var ab := polygon[poly[1]] - polygon[poly[0]]
var ac := polygon[poly[2]] - polygon[poly[0]]
if (ab.x * ac.y - ab.y * ac.x) * 0.5 <= 0:
new.push_front(poly)
# Fix winding as well.
new.push_back(PackedInt32Array([poly[2],poly[1],poly[0]]))
polygons = new
func _prepare_mesh() -> void:
_prepare_floor_mesh()
_prepare_wall_mesh()
_prepare_floor_collision()
_prepare_wall_collision()
func _prepare_floor_mesh() -> void:
@@ -76,6 +80,7 @@ func _prepare_floor_mesh() -> void:
_floor_mesh.mesh = m
# TODO: Make last segment UV stretch to edge, tiling it.
func _prepare_wall_mesh() -> void:
var m := ImmediateMesh.new()
m.surface_begin(Mesh.PRIMITIVE_TRIANGLES)
@@ -121,3 +126,31 @@ func _prepare_wall_mesh() -> void:
ndy = rng.randf_range(-1.0, 1.0)
m.surface_end()
_wall_mesh.mesh = m
func _prepare_floor_collision() -> void:
if _floor_mesh.get_node_or_null("FloorMesh_col"):
_floor_mesh.remove_child(_floor_mesh.get_node("FloorMesh_col"))
_floor_mesh.create_convex_collision()
(_floor_mesh.get_node("FloorMesh_col") as StaticBody3D).set_collision_layer_value(2, 1)
func _prepare_wall_collision() -> void:
if _wall_mesh.get_node_or_null("WallMesh_col"):
_wall_mesh.remove_child(_wall_mesh.get_node("WallMesh_col"))
_wall_mesh.create_trimesh_collision()
_wall_mesh.get_node("WallMesh_col").get_node("CollisionShape3D").shape.backface_collision = true
(_wall_mesh.get_node("WallMesh_col") as StaticBody3D).set_collision_layer_value(3, 1)
#var shape := ConcavePolygonShape3D.new()
#shape.backface_collision = true
#var faces := PackedVector3Array()
#for ld in polygon.size():
#var nd := (ld + 1) % polygon.size()
#faces.push_back(Vector3(polygon[ld].x * _scale, 0, polygon[ld].y * _scale))
#faces.push_back(Vector3(polygon[nd].x * _scale, 0, polygon[nd].y * _scale))
#faces.push_back(Vector3(polygon[ld].x * _scale, 5, polygon[ld].y * _scale))
#faces.push_back(Vector3(polygon[nd].x * _scale, 0, polygon[nd].y * _scale))
#faces.push_back(Vector3(polygon[nd].x * _scale, 5, polygon[nd].y * _scale))
#faces.push_back(Vector3(polygon[ld].x * _scale, 5, polygon[ld].y * _scale))
#shape.set_faces(faces)
#(_wall_mesh.get_node("Area3D").get_node("CollisionShape3D") as CollisionShape3D).shape = shape