# HG changeset patch # User Sam # Date 1685806117 -25200 # Node ID a4f9b97e80de986e54619e512b7bcc830d2636d1 # Parent ebe4ac41e1cf6435c70f6ca68ae3ce2f68cffb53 add: hitbox/sphere helpers diff -r ebe4ac41e1cf -r a4f9b97e80de 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 ebe4ac41e1cf -r a4f9b97e80de 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