using EvilEngine.Formats.Collision; using EvilEngine.Formats.Maps; using EvilEngine.Formats.Records; using EvilEngine.Host.Collision; using Godot; using Godot.Collections; namespace EvilEngine.Shared.World; public sealed partial class WorldCollision : Node3D { public const uint WorldLayer = 1; public const uint PlayerClipLayer = 2; public const uint NpcClipLayer = 4; public const uint VehicleClipLayer = 8; public const uint PhysicsClipLayer = 16; public const uint PropLayer = 32; public const uint CharacterLayer = 64; public const PhysicsServer3D.BodyParameter ShapeSurfacesParameter = (PhysicsServer3D.BodyParameter)100; private const int BatchSize = 256; private readonly List _bodies = []; private readonly MapColliderSet _colliders; private readonly List _shapes = []; public WorldCollision(MapColliderSet colliders) { _colliders = colliders; Name = "Collision"; } public int Shapes => _shapes.Count; public static uint Layer(IReadOnlyList contents) { if (SolidContents.Blocks(contents)) return WorldLayer; if (contents.Contains(SolidContents.PlayerClip)) return PlayerClipLayer; if (contents.Contains(SolidContents.NpcClip)) return NpcClipLayer; if (contents.Contains(SolidContents.VehicleClip)) return VehicleClipLayer; return contents.Contains(SolidContents.PhysicsClip) ? PhysicsClipLayer : 0; } public override void _Ready() { Build(_colliders, GetWorld3D().Space, _bodies, _shapes); } public static void Build(MapColliderSet colliders, Rid space, List bodies, List shapes, WorldSurfaces? surfaces = null, Func? records = null) { var meshes = new System.Collections.Generic.Dictionary(ReferenceEqualityComparer.Instance); var open = new System.Collections.Generic.Dictionary(); var made = new List<(Rid Body, List Shapes)>(); var placedOn = new System.Collections.Generic.Dictionary>(); var known = new System.Collections.Generic.Dictionary(); (float Friction, float Bounce) Values(CollisionSurface surface) { if (surface == CollisionSurface.None || records is null) return (float.NaN, float.NaN); if (known.TryGetValue(surface, out var values)) return values; var record = records(surface); return known[surface] = ((float?)record?.Friction ?? float.NaN, (float?)record?.Bounce ?? float.NaN); } Rid Body(uint layer) { var body = PhysicsServer3D.BodyCreate(); PhysicsServer3D.BodySetMode(body, PhysicsServer3D.BodyMode.Static); PhysicsServer3D.BodySetCollisionLayer(body, layer); PhysicsServer3D.BodySetCollisionMask(body, 0); bodies.Add(body); List placed = []; made.Add((body, placed)); placedOn[body] = placed; return body; } foreach (var collider in colliders.Colliders) { var layer = Layer(collider.Contents); if (layer == 0) continue; if (collider.Shape is MapColliderShape.Triangles triangles) { if (!meshes.TryGetValue(triangles.Mesh, out var mesh)) meshes[triangles.Mesh] = mesh = Mesh(triangles.Mesh, shapes, Values); var body = Body(layer); PhysicsServer3D.BodyAddShape(body, mesh, Affine(triangles.Transform)); made[^1].Shapes.Add(collider); continue; } if (Shape(collider.Shape, shapes) is not var (shape, transform)) continue; if (!open.TryGetValue(layer, out var batch) || batch.Shapes == BatchSize) batch = (Body(layer), 0); PhysicsServer3D.BodyAddShape(batch.Body, shape, transform); placedOn[batch.Body].Add(collider); open[layer] = (batch.Body, batch.Shapes + 1); } foreach (var (body, placed) in made) { if (records is not null) { var pairs = new float[placed.Count * 2]; var any = false; for (var index = 0; index < placed.Count; index++) { var (friction, bounce) = placed[index].Shape is MapColliderShape.Triangles ? (float.NaN, float.NaN) : Values(WorldSurfaces.Of(placed[index].Shape)); pairs[index * 2] = friction; pairs[index * 2 + 1] = bounce; any |= !float.IsNaN(friction) || !float.IsNaN(bounce); } if (any) PhysicsServer3D.BodySetParam(body, ShapeSurfacesParameter, pairs); } foreach (var collider in placed) surfaces?.Add(body, collider); PhysicsServer3D.BodySetSpace(body, space); } } private static Rid Mesh(ColliderMesh mesh, List shapes, Func values) { var rid = PhysicsServer3D.ConcavePolygonShapeCreate(); var data = new Dictionary { ["faces"] = mesh.Indices.Select(index => Vector(mesh.Positions[index])).ToArray(), ["backface_collision"] = true }; if (mesh.Surfaces.Count > 0) { var parts = mesh.Surfaces.Select(values).ToArray(); data["friction"] = Enumerable.Range(0, mesh.Count).Select(face => parts[mesh.Parts[face]].Friction).ToArray(); data["bounce"] = Enumerable.Range(0, mesh.Count).Select(face => parts[mesh.Parts[face]].Bounce).ToArray(); } PhysicsServer3D.ShapeSetData(rid, data); PhysicsServer3D.ShapeSetMargin(rid, 0); shapes.Add(rid); return rid; } private static Transform3D Affine(MapAffine affine) { var m = affine.Basis; return new Transform3D(new Basis(new Vector3(m[0], m[3], m[6]), new Vector3(m[1], m[4], m[7]), new Vector3(m[2], m[5], m[8])), Vector(affine.Origin)); } public override void _ExitTree() { foreach (var body in _bodies) PhysicsServer3D.FreeRid(body); foreach (var shape in _shapes) PhysicsServer3D.FreeRid(shape); _bodies.Clear(); _shapes.Clear(); } private static (Rid, Transform3D)? Shape(MapColliderShape shape, List shapes) { Rid rid; var transform = Transform3D.Identity; switch (shape) { case MapColliderShape.Hull hull: rid = PhysicsServer3D.ConvexPolygonShapeCreate(); PhysicsServer3D.ShapeSetData(rid, hull.Points.Select(Vector).ToArray()); break; case MapColliderShape.Primitive primitive: var rotation = primitive.Rotation; transform = new Transform3D(new Basis(new Quaternion(rotation.X, rotation.Y, rotation.Z, rotation.W)), Vector(primitive.Position)); if (Primitive(primitive.Shape) is not { } made) return null; rid = made; break; default: return null; } PhysicsServer3D.ShapeSetMargin(rid, 0); shapes.Add(rid); return (rid, transform); } public static Rid? Primitive(ColliderShape shape) { Rid rid; switch (shape) { case ColliderShape.Box box: rid = PhysicsServer3D.BoxShapeCreate(); PhysicsServer3D.ShapeSetData(rid, new Vector3(box.Size.X, box.Size.Y, box.Size.Z) / 2); break; case ColliderShape.Sphere sphere: rid = PhysicsServer3D.SphereShapeCreate(); PhysicsServer3D.ShapeSetData(rid, sphere.Radius); break; case ColliderShape.Capsule capsule: rid = PhysicsServer3D.CapsuleShapeCreate(); PhysicsServer3D.ShapeSetData(rid, new Dictionary { ["radius"] = capsule.Radius, ["height"] = capsule.Height + 2 * capsule.Radius }); break; case ColliderShape.Cylinder cylinder: rid = PhysicsServer3D.CylinderShapeCreate(); PhysicsServer3D.ShapeSetData(rid, new Dictionary { ["radius"] = cylinder.Radius, ["height"] = cylinder.Height }); break; default: return null; } return rid; } private static Vector3 Vector(MapVector point) { return new Vector3(point.X, point.Y, point.Z); } }