Update leenkx/Sources/leenkx/trait/physics/bullet/PhysicsWorld.hx

This commit is contained in:
2025-10-03 05:02:28 +00:00
parent ac5aa3d19c
commit 5cf33724e4

View File

@ -7,6 +7,7 @@ import iron.system.Time;
import iron.math.Vec4;
import iron.math.Quat;
import iron.math.RayCaster;
import iron.object.Object;
class Hit {
@ -72,6 +73,10 @@ class PhysicsWorld extends Trait {
public var convexHitNormalWorld = new Vec4();
var pairCache: Bool = false;
// Performance optimization: Global RigidBody trait cache
public static var rigidBodyCache: Map<Object, RigidBody> = new Map();
public static var cacheObjectRefs: Map<Object, Int> = new Map(); // Track trait counts
static var nullvec = true;
static var vec1: bullet.Bt.Vector3 = null;
static var vec2: bullet.Bt.Vector3 = null;
@ -148,6 +153,33 @@ class PhysicsWorld extends Trait {
});
}
public static function getCachedRigidBody(object: Object): RigidBody {
if (object == null) return null;
var shouldUpdate = false;
var cachedTraitCount = cacheObjectRefs.get(object);
if (cachedTraitCount == null || cachedTraitCount != object.traits.length) {
shouldUpdate = true;
} else if (rigidBodyCache.get(object) == null) {
var rb = object.getTrait(RigidBody);
if (rb != null) shouldUpdate = true;
}
if (shouldUpdate) {
var rb = object.getTrait(RigidBody);
rigidBodyCache.set(object, rb);
cacheObjectRefs.set(object, object.traits.length);
}
return rigidBodyCache.get(object);
}
public static inline function hasContactWith(contacts: Array<RigidBody>, target: RigidBody): Bool {
return contacts != null && target != null && contacts.indexOf(target) >= 0;
}
public function reset() {
for (rb in active.rbMap) removeRigidBody(rb);
}