[Physics2] Fix a memory leak on object instances (#7136)

This commit is contained in:
D8H
2024-11-08 14:53:38 +01:00
committed by GitHub
parent 223268554b
commit a3f7176c42

View File

@@ -35,19 +35,23 @@ namespace gdjs {
invScaleX: float;
/** @deprecated Use `worldInvScale` instead */
invScaleY: float;
timeStep: float;
frameTime: float = 0;
stepped: boolean = false;
timeScale: float = 1;
world: Box2D.b2World;
staticBody: Box2D.b2Body;
/** Contact listener to keep track of current collisions */
contactListener: Box2D.JSContactListener;
_nextJointId: number = 1;
/** Start with 1 so the user is safe from default variables value (0) */
joints: { [key: string]: Box2D.b2Joint } = {};
/** Avoid creating new vectors all the time */
_tempb2Vec2 = new Box2D.b2Vec2(0, 0);
/** Sometimes two vectors are needed on the same function call */
_tempb2Vec2Sec = new Box2D.b2Vec2(0, 0);
/**
* List of physics behavior in the runtimeScene. It should be updated
@@ -68,9 +72,7 @@ namespace gdjs {
sharedData.worldScale || Math.sqrt(this.scaleX * this.scaleY);
this.worldInvScale = 1 / this.worldScale;
this.timeStep = 1 / 60;
this.world = new Box2D.b2World(
new Box2D.b2Vec2(this.gravityX, this.gravityY)
);
this.world = new Box2D.b2World(this.b2Vec2(this.gravityX, this.gravityY));
this.world.SetAutoClearForces(false);
this.staticBody = this.world.CreateBody(new Box2D.b2BodyDef());
this.contactListener = new Box2D.JSContactListener();
@@ -135,6 +137,13 @@ namespace gdjs {
this.world.SetContactListener(this.contactListener);
}
b2Vec2(x: float, y: float): Box2D.b2Vec2 {
const tempb2Vec2 = this._tempb2Vec2;
tempb2Vec2.set_x(x);
tempb2Vec2.set_y(y);
return tempb2Vec2;
}
// (string)jointId -> (b2Joint)b2Joint
static getSharedData(
runtimeScene: gdjs.RuntimeScene,
@@ -300,14 +309,11 @@ namespace gdjs {
}
}
gdjs.registerRuntimeSceneUnloadedCallback(function (runtimeScene) {
if (
// @ts-ignore
runtimeScene.physics2SharedData &&
// @ts-ignore
runtimeScene.physics2SharedData.world
) {
// @ts-ignore
Box2D.destroy(runtimeScene.physics2SharedData.world);
const physics2SharedData = runtimeScene.physics2SharedData;
if (physics2SharedData && physics2SharedData.world) {
Box2D.destroy(physics2SharedData.world);
Box2D.destroy(physics2SharedData._tempb2Vec2);
Box2D.destroy(physics2SharedData._tempb2Vec2Sec);
}
});
@@ -358,8 +364,6 @@ namespace gdjs {
currentContacts: Array<Physics2RuntimeBehavior>;
destroyedDuringFrameLogic: boolean;
_body: Box2D.b2Body | null = null;
/** Avoid creating new vectors all the time */
_tempb2Vec2: Box2D.b2Vec2;
/**
* sharedData is a reference to the shared data of the scene, that registers
@@ -367,8 +371,6 @@ namespace gdjs {
* before stepping the world.
*/
_sharedData: Physics2SharedData;
/** Sometimes two vectors are needed on the same function call */
_tempb2Vec2Sec: Box2D.b2Vec2;
_objectOldX: number = 0;
_objectOldY: number = 0;
@@ -414,22 +416,22 @@ namespace gdjs {
instanceContainer.getScene(),
behaviorData.name
);
this._tempb2Vec2 = new Box2D.b2Vec2();
this._tempb2Vec2Sec = new Box2D.b2Vec2();
this._sharedData.addToBehaviorsList(this);
}
// Stores a Box2D pointer of created vertices
b2Vec2(x: float, y: float): Box2D.b2Vec2 {
this._tempb2Vec2.set_x(x);
this._tempb2Vec2.set_y(y);
return this._tempb2Vec2;
const tempb2Vec2 = this._sharedData._tempb2Vec2;
tempb2Vec2.set_x(x);
tempb2Vec2.set_y(y);
return tempb2Vec2;
}
b2Vec2Sec(x: float, y: float): Box2D.b2Vec2 {
this._tempb2Vec2Sec.set_x(x);
this._tempb2Vec2Sec.set_y(y);
return this._tempb2Vec2Sec;
const tempb2Vec2Sec = this._sharedData._tempb2Vec2Sec;
tempb2Vec2Sec.set_x(x);
tempb2Vec2Sec.set_y(y);
return tempb2Vec2Sec;
}
updateFromBehaviorData(oldBehaviorData, newBehaviorData): boolean {