[Physics2] Avoid an exception when the object position is not finite (#7017)

This commit is contained in:
D8H
2024-10-07 11:11:36 +02:00
committed by GitHub
parent 6321e82f63
commit a90b9a27e9

View File

@@ -897,16 +897,21 @@ namespace gdjs {
// Generate the body definition
const bodyDef = new Box2D.b2BodyDef();
const x =
(this.owner.getDrawableX() + this.owner.getWidth() / 2) *
this._sharedData.worldInvScale;
const y =
(this.owner.getDrawableY() + this.owner.getHeight() / 2) *
this._sharedData.worldInvScale;
// Set the initial body transformation from the GD object
bodyDef.set_position(
this.b2Vec2(
(this.owner.getDrawableX() + this.owner.getWidth() / 2) *
this._sharedData.worldInvScale,
(this.owner.getDrawableY() + this.owner.getHeight() / 2) *
this._sharedData.worldInvScale
)
this.b2Vec2(Number.isFinite(x) ? x : 0, Number.isFinite(y) ? y : 0)
);
bodyDef.set_angle(gdjs.toRad(this.owner.getAngle()));
const angle = gdjs.toRad(this.owner.getAngle());
if (Number.isFinite(angle)) {
bodyDef.set_angle(angle);
}
// Set body settings
bodyDef.set_type(
@@ -1014,13 +1019,21 @@ namespace gdjs {
this._objectOldY !== this.owner.getY() ||
this._objectOldAngle !== this.owner.getAngle()
) {
const pos = this.b2Vec2(
const x =
(this.owner.getDrawableX() + this.owner.getWidth() / 2) *
this._sharedData.worldInvScale,
this._sharedData.worldInvScale;
const y =
(this.owner.getDrawableY() + this.owner.getHeight() / 2) *
this._sharedData.worldInvScale
this._sharedData.worldInvScale;
const pos = this.b2Vec2(
Number.isFinite(x) ? x : body.GetPosition().x,
Number.isFinite(y) ? y : body.GetPosition().y
);
const angle = gdjs.toRad(this.owner.getAngle());
body.SetTransform(
pos,
Number.isFinite(angle) ? angle : body.GetAngle()
);
body.SetTransform(pos, gdjs.toRad(this.owner.getAngle()));
body.SetAwake(true);
}
}