# HG changeset patch # User Sam # Date 1685806117 -25200 # Node ID c0f8ef9594ccd883b3859a4e962bda20dfc53522 # Parent f91f54c8d86c301ae0fd6c0d3ba004b9723fc3c8 add: hitbox/sphere helpers diff -r f91f54c8d86c -r c0f8ef9594cc src/semicongine/collision.nim --- a/src/semicongine/collision.nim Sat Jun 03 19:22:18 2023 +0700 +++ b/src/semicongine/collision.nim Sat Jun 03 22:28:37 2023 +0700 @@ -170,3 +170,32 @@ if direction == newVec3f(0, 0, 0): direction[0] = 0.001 inc n + +func calculateHitbox*(points: seq[Vec3f]): HitBox = + var + minX = high(float32) + maxX = low(float32) + minY = high(float32) + maxY = low(float32) + minZ = high(float32) + maxZ = low(float32) + + for p in points: + minX = min(minX, p.x) + maxX = max(maxX, p.x) + minY = min(minY, p.y) + maxY = max(maxY, p.y) + minZ = min(minZ, p.z) + maxZ = max(maxz, p.z) + + let + scaleX = (maxX - minX) + scaleY = (maxY - minY) + scaleZ = (maxZ - minZ) + + HitBox(transform: translate3d(minX, minY, minZ) * scale3d(scaleX, scaleY, scaleZ)) + +func calculateHitsphere*(points: seq[Vec3f]): HitSphere = + result = HitSphere() + for p in points: + result.radius = max(result.radius, p.length) diff -r f91f54c8d86c -r c0f8ef9594cc src/semicongine/mesh.nim --- a/src/semicongine/mesh.nim Sat Jun 03 19:22:18 2023 +0700 +++ b/src/semicongine/mesh.nim Sat Jun 03 22:28:37 2023 +0700 @@ -7,6 +7,7 @@ import ./core import ./scene +import ./collision type MeshIndexType* = enum @@ -213,7 +214,7 @@ assert attribute in mesh.data for v in getValues[T](mesh.data[attribute])[].mitems: when T is Vec3f: - v = (transform * newVec4f(v.x, v.y, v.z, 1'f32)).xyz + v = (transform * v.toVec4(1'f32)).toVec3 else: v = transform * v