From 8d1c2c51bd1c11a3c745a4e68d26e6b0023f56a4 Mon Sep 17 00:00:00 2001 From: Onek8 Date: Fri, 11 Apr 2025 08:18:30 +0000 Subject: [PATCH] Update leenkx/Sources/leenkx/trait/physics/bullet/PhysicsWorld.hx --- .../trait/physics/bullet/PhysicsWorld.hx | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/leenkx/Sources/leenkx/trait/physics/bullet/PhysicsWorld.hx b/leenkx/Sources/leenkx/trait/physics/bullet/PhysicsWorld.hx index 333c410..4d205a4 100644 --- a/leenkx/Sources/leenkx/trait/physics/bullet/PhysicsWorld.hx +++ b/leenkx/Sources/leenkx/trait/physics/bullet/PhysicsWorld.hx @@ -72,7 +72,7 @@ class PhysicsWorld extends Trait { public var convexHitNormalWorld = new Vec4(); var pairCache: Bool = false; public var drawRaycasts: Bool = false; - + static var nullvec = true; static var vec1: bullet.Bt.Vector3 = null; static var vec2: bullet.Bt.Vector3 = null; @@ -122,7 +122,7 @@ class PhysicsWorld extends Trait { this.maxSteps = maxSteps; this.solverIterations = solverIterations; this.drawRaycasts = drawRaycasts; - + // First scene if (active == null) { createPhysics(); @@ -397,6 +397,7 @@ class PhysicsWorld extends Trait { rayTo.setValue(to.x, to.y, to.z); var rayCallback = new bullet.Bt.ClosestRayResultCallback(rayFrom, rayTo); + #if js rayCallback.set_m_collisionFilterGroup(group); rayCallback.set_m_collisionFilterMask(mask); @@ -406,12 +407,20 @@ class PhysicsWorld extends Trait { #end var worldDyn: bullet.Bt.DynamicsWorld = world; var worldCol: bullet.Bt.CollisionWorld = worldDyn; + + if (this.drawRaycasts && this.debugDrawHelper != null) { + this.debugDrawHelper.drawRayCast( + rayFrom.x(), rayFrom.y(), rayFrom.z(), + rayTo.x(), rayTo.y(), rayTo.z(), + 0.73, 0.341, 1.0 + ); + } + worldCol.rayTest(rayFrom, rayTo, rayCallback); var rb: RigidBody = null; var hitInfo: Hit = null; - var rc: bullet.Bt.RayResultCallback = rayCallback; - if (rc.hasHit()) { + if (rayCallback.hasHit()) { #if js var co = rayCallback.get_m_collisionObject(); var body = untyped bullet.Bt.Ammo.btRigidBody.prototype.upcast(co); @@ -471,8 +480,7 @@ class PhysicsWorld extends Trait { var hitInfo: ConvexHit = null; - var cc: bullet.Bt.ClosestConvexResultCallback = convexCallback; - if (cc.hasHit()) { + if (convexCallback.hasHit()) { #if js var hit = convexCallback.get_m_hitPointWorld(); convexHitPointWorld.set(hit.x(), hit.y(), hit.z()); @@ -509,16 +517,22 @@ class PhysicsWorld extends Trait { public function setDebugDrawMode(debugDrawMode: DebugDrawMode) { if (debugDrawHelper == null) { + // Initialize if helper is null AND (standard debug mode is requested OR our custom raycast drawing is requested) if (debugDrawMode != NoDebug || this.drawRaycasts) { initDebugDrawing(); } else { + // Helper is null and no debug drawing needed, so exit return; } } + // If we reached here, the helper is initialized (or was already) + // Now set the standard Bullet debug mode on the actual drawer #if js - world.getDebugDrawer().setDebugMode(debugDrawMode); + // Ensure drawer exists before setting mode (might have just been initialized) + var drawer = world.getDebugDrawer(); + if (drawer != null) drawer.setDebugMode(debugDrawMode); #elseif hl hlDebugDrawer_setDebugMode(debugDrawMode); #end @@ -665,10 +679,7 @@ enum abstract DebugDrawMode(Int) from Int to Int { // We could use it in the future to toggle depth testing for lines, i.e. draw actual 3D lines if not set and Kha's g2 lines if set. var FastWireframe = 1 << 13; - /** - Draw the normal vectors of the triangles of the physics collider meshes. - This only works for `Mesh` collision shapes. - **/ + /** Draw the normal vectors of the triangles of the physics collider meshes. **/ // Outside of Leenkx this works for a few more collision shapes var DrawNormals = 1 << 14;