changeset 282:a4f9b97e80de

add: hitbox/sphere helpers
author Sam <sam@basx.dev>
date Sat, 03 Jun 2023 22:28:37 +0700
parents ebe4ac41e1cf
children 9632a323e7a9
files src/semicongine/collision.nim src/semicongine/mesh.nim
diffstat 2 files changed, 31 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- 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