changeset 283:9632a323e7a9

did: improve matrix transformation API, no need to manually convert Vec3 anymore
author Sam <sam@basx.dev>
date Sun, 04 Jun 2023 00:30:43 +0700
parents a4f9b97e80de
children 330e91ebd525
files src/semicongine/collision.nim src/semicongine/core/matrix.nim src/semicongine/mesh.nim
diffstat 3 files changed, 23 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/src/semicongine/collision.nim	Sat Jun 03 22:28:37 2023 +0700
+++ b/src/semicongine/collision.nim	Sun Jun 04 00:30:43 2023 +0700
@@ -16,10 +16,10 @@
   # from https://math.stackexchange.com/questions/1472049/check-if-a-point-is-inside-a-rectangular-shaped-area-3d
   let
     t = hitbox.entity.getModelTransform() * hitbox.transform
-    P1 = t * newVec4f(0, 0, 0, 1) # origin
-    P2 = t * Z.toVec4(1'f32)
-    P4 = t * X.toVec4(1'f32)
-    P5 = t * Y.toVec4(1'f32)
+    P1 = t * newVec3f(0, 0, 0) # origin
+    P2 = t * Z
+    P4 = t * X
+    P5 = t * Y
     u = (P1 - P4).cross(P1 - P5)
     v = (P1 - P2).cross(P1 - P5)
     w = (P1 - P2).cross(P1 - P4)
@@ -50,20 +50,20 @@
 
 func findFurthestPoint(hitsphere: HitSphere, direction: Vec3f): Vec3f =
   let directionNormalizedToSphere = ((direction / direction.length) * hitsphere.radius)
-  return (hitsphere.entity.getModelTransform() * directionNormalizedToSphere.toVec4(1'f32)).toVec3
+  return hitsphere.entity.getModelTransform() * directionNormalizedToSphere
 
 func findFurthestPoint(hitbox: HitBox, direction: Vec3f): Vec3f =
   let transform = hitbox.entity.getModelTransform() * hitbox.transform
   return findFurthestPoint(
     [
-      (transform * newVec4f(0, 0, 0, 1)).toVec3,
-      (transform * X.toVec4(1'f32)).toVec3,
-      (transform * Y.toVec4(1'f32)).toVec3,
-      (transform * Z.toVec4(1'f32)).toVec3,
-      (transform * (X + Y).toVec4(1'f32)).toVec3,
-      (transform * (X + Z).toVec4(1'f32)).toVec3,
-      (transform * (Y + Z).toVec4(1'f32)).toVec3,
-      (transform * (X + Y + Z).toVec4(1'f32)).toVec3,
+      transform * newVec3f(0, 0, 0),
+      transform * X,
+      transform * Y,
+      transform * Z,
+      transform * (X + Y),
+      transform * (X + Z),
+      transform * (Y + Z),
+      transform * (X + Y + Z),
     ],
     direction
   )
--- a/src/semicongine/core/matrix.nim	Sat Jun 03 22:28:37 2023 +0700
+++ b/src/semicongine/core/matrix.nim	Sun Jun 04 00:30:43 2023 +0700
@@ -282,6 +282,8 @@
 
 createAllMultiplicationOperators()
 
+func `*`*(mat: Mat4, vec: Vec3f): Vec3f =
+  (mat * vec.toVec4(1)).toVec3
 
 func transposed*[T](m: TMat2[T]): TMat2[T] = TMat2[T](data: [
   m[0, 0], m[1, 0],
--- a/src/semicongine/mesh.nim	Sat Jun 03 22:28:37 2023 +0700
+++ b/src/semicongine/mesh.nim	Sun Jun 04 00:30:43 2023 +0700
@@ -213,10 +213,7 @@
 proc transform*[T: GPUType](mesh: var Mesh, attribute: string, transform: Mat4) =
   assert attribute in mesh.data
   for v in getValues[T](mesh.data[attribute])[].mitems:
-    when T is Vec3f:
-      v = (transform * v.toVec4(1'f32)).toVec3
-    else:
-      v = transform * v
+    v = transform * v
 
 func rect*(width=1'f32, height=1'f32, color="ffffffff"): Mesh =
   result = Mesh(
@@ -281,9 +278,14 @@
   mesh.instanceTransforms = mat
   mesh.dirtyInstanceTransforms = true
 
-proc getInstanceTransform*(mesh: Mesh, i: uint32): Mat4 =
+func getInstanceTransform*(mesh: Mesh, i: uint32): Mat4 =
   assert 0 <= i and i < mesh.instanceCount
   mesh.instanceTransforms[i]
 
-proc getInstanceTransforms*(mesh: Mesh): seq[Mat4] =
+func getInstanceTransforms*(mesh: Mesh): seq[Mat4] =
   mesh.instanceTransforms
+
+func getCollisionPoints*(mesh: Mesh, positionAttribute="position"): seq[Vec3f] =
+  for p in getMeshData[Vec3f](mesh, positionAttribute)[]:
+    result.add mesh.entity.getModelTransform() * p
+