# HG changeset patch # User Sam # Date 1685813443 -25200 # Node ID d1c6e3132f6ad6e4a497c1e36965ebd0caf54556 # Parent c0f8ef9594ccd883b3859a4e962bda20dfc53522 did: improve matrix transformation API, no need to manually convert Vec3 anymore diff -r c0f8ef9594cc -r d1c6e3132f6a src/semicongine/collision.nim --- 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 ) diff -r c0f8ef9594cc -r d1c6e3132f6a src/semicongine/core/matrix.nim --- 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], diff -r c0f8ef9594cc -r d1c6e3132f6a src/semicongine/mesh.nim --- 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 +