diff --git a/Extensions/Physics2Behavior/JsExtension.js b/Extensions/Physics2Behavior/JsExtension.js index 4f13ed173f88..a05e60be7beb 100644 --- a/Extensions/Physics2Behavior/JsExtension.js +++ b/Extensions/Physics2Behavior/JsExtension.js @@ -794,6 +794,27 @@ module.exports = { .getCodeExtraInformation() .setFunctionName('setKinematic'); + aut + .addAction( + 'SetBodyType', + _('Set body type'), + _('Set the body type of an object.'), + _('Set the body type of _PARAM0_ to _PARAM2_'), + _('Dynamics'), + 'res/physics32.png', + 'res/physics32.png' + ) + .addParameter('object', _('Object'), '', false) + .addParameter('behavior', _('Behavior'), 'Physics2Behavior') + .addParameter( + 'stringWithSelector', + _('Body type'), + '["Static", "Dynamic", "Kinematic"]', + false + ) + .getCodeExtraInformation() + .setFunctionName('setBodyType'); + aut .addCondition( 'IsBullet', diff --git a/Extensions/Physics2Behavior/physics2runtimebehavior.ts b/Extensions/Physics2Behavior/physics2runtimebehavior.ts index 170cf86f737f..c727257db4a3 100644 --- a/Extensions/Physics2Behavior/physics2runtimebehavior.ts +++ b/Extensions/Physics2Behavior/physics2runtimebehavior.ts @@ -1139,6 +1139,21 @@ namespace gdjs { return this.bodyType === 'Kinematic'; } + setBodyType(bodyType: string): void { + switch (bodyType) { + case 'Static': + this.setStatic(); + break; + case 'Kinematic': + this.setKinematic(); + break; + case 'Dynamic': + default: + this.setDynamic(); + break; + } + } + setKinematic(): void { // Check if there is no modification if (this.bodyType === 'Kinematic') { diff --git a/Extensions/Physics3DBehavior/JsExtension.js b/Extensions/Physics3DBehavior/JsExtension.js index 26133a6c1e3b..74bd730fd884 100644 --- a/Extensions/Physics3DBehavior/JsExtension.js +++ b/Extensions/Physics3DBehavior/JsExtension.js @@ -821,6 +821,27 @@ module.exports = { .getCodeExtraInformation() .setFunctionName('isKinematic'); + aut + .addScopedAction( + 'SetBodyType', + _('Set body type'), + _('Set the body type of an object.'), + _('Set the body type of _PARAM0_ to _PARAM2_'), + _('Dynamics'), + 'JsPlatform/Extensions/physics3d.svg', + 'JsPlatform/Extensions/physics3d.svg' + ) + .addParameter('object', _('Object'), '', false) + .addParameter('behavior', _('Behavior'), 'Physics3DBehavior') + .addParameter( + 'stringWithSelector', + _('Body type'), + '["Static", "Dynamic", "Kinematic"]', + false + ) + .getCodeExtraInformation() + .setFunctionName('setBodyType'); + aut .addScopedCondition( 'IsBullet', diff --git a/Extensions/Physics3DBehavior/Physics3DRuntimeBehavior.ts b/Extensions/Physics3DBehavior/Physics3DRuntimeBehavior.ts index 6c8486651220..2c1e4951c0ae 100644 --- a/Extensions/Physics3DBehavior/Physics3DRuntimeBehavior.ts +++ b/Extensions/Physics3DBehavior/Physics3DRuntimeBehavior.ts @@ -1153,6 +1153,52 @@ namespace gdjs { return this.bodyType === 'Kinematic'; } + setBodyType(bodyType: string): void { + if ( + bodyType !== 'Static' && + bodyType !== 'Dynamic' && + bodyType !== 'Kinematic' + ) { + return; + } + + if (this.bodyType === bodyType) { + return; + } + + this.bodyType = bodyType; + + if (this._body === null) { + if (!this._createBody()) return; + } + + if (this._body === null) { + return; + } + const body = this._body!; + const bodyInterface = this._sharedData.bodyInterface; + let motionType: Jolt.EMotionType; + switch (this.bodyType) { + case 'Static': + motionType = Jolt.EMotionType_Static; + break; + case 'Kinematic': + motionType = Jolt.EMotionType_Kinematic; + break; + case 'Dynamic': + default: + motionType = Jolt.EMotionType_Dynamic; + break; + } + + bodyInterface.SetMotionType( + body.GetID(), + motionType, + Jolt.EActivation_Activate + ); + bodyInterface.SetObjectLayer(body.GetID(), this.getBodyLayer()); + } + isBullet(): boolean { return this.bullet; }