I continue to work with explosion. Now I am in a process of creation of so called chain explosions.
I created an algorithm: https://www.babylonjs-playground.com/#3GBTN9#1 (press ‘spacebar’ to launch the chain).
I want to discuss this algorithm, but unfortunately I am faced with a bug that could be reproduced only in playground. There is no such bug on my local machine with the same algorithm.
I am receiving ‘Uncaught TypeError: Cannot read property ‘dispose’ of undefined at babylon.js:16’. It’s trying to dispose eventData.sphere that doesn’t exist anymore. I believe it might be connected with some sort of garbage collection issues.
Key points of the algorithm (very briefly, will explain more when bug will be fixed):
An instance of this class should be assigned as property ‘destructibleObject’ to an object that we want to mark as destructible. But it’s necessary to make it properly.
myObject.destructibleObject = new DestructibleObject(...args)
createFromObject method from
Tracks all destructible objects in scene. Removes object when destroyed.
class ExplosionManager: executes and debugs explosions. The main thing here is
createExplosion method that receives the collection of destructible objects on the scene as one of arguments. What’s going on inside:
- Physics is created (using
- Casting the ray to every destructible object that were received as argument.
- Getting picked objects with ray (
- An array of picked objects should be cleaned from ‘explosion transparent’ objects and sorted by distance (
_cleanHitsArray method is for it).
- Getting the closest object hit by ray (
_getClosestHit method is for it)
- Calculating damage to the object that we picked. We are using linear falloff to calculate splash damage.
- Here are 3 important checks for the picked object:
- Is it destructible object?
- Is picked object is an object the ray was cast to. Here we are checking that we hit the target object on the ray, not any other destructible object on the way.
- Is it not in explosion stack already?
- If all checks are true, then we put picked object in explosion stack with corresponding damage that will be applied later.
- Process the explosion stack.
I don’t like how I organized and execute explosion stack. It has some issues. But I need to find a way how to avoid the bug in playground that I mentioned above before I continue. Please help with a bug.