Skip to content

Commit

Permalink
New action for Physics behavior: Set the linear velocity towards an a…
Browse files Browse the repository at this point in the history
…ngle (#5670)
  • Loading branch information
tristanbob authored Mar 27, 2024
1 parent 4ee4320 commit 368da1b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Extensions/Physics2Behavior/JsExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,26 @@ module.exports = {
.getCodeExtraInformation()
.setFunctionName('getLinearVelocityAngle');

aut
.addAction(
'LinearVelocityAngle',
_('Linear velocity towards an angle'),
_('Set the linear velocity towards an angle.'),
_(
'Set the linear velocity of _PARAM0_ towards angle: _PARAM2_ degrees, speed: _PARAM3_ pixels per second'
),
_('Velocity'),
'res/physics32.png',
'res/physics32.png'
)
.addParameter('object', _('Object'), '', false)
.addParameter('behavior', _('Behavior'), 'Physics2Behavior')
.addParameter('expression', _('Angle'))
.addParameter('expression', _('Speed (in pixels per second)'))
.getCodeExtraInformation()
.setFunctionName('setLinearVelocityAngle')
.setGetter('getLinearVelocityAngle');

aut
.addExpression(
'LinearVelocityAngle',
Expand Down
17 changes: 17 additions & 0 deletions Extensions/Physics2Behavior/physics2runtimebehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,23 @@ namespace gdjs {
);
}

setLinearVelocityAngle(angle: float, linearVelocity: float): void {
// If there is no body, set a new one
if (this._body === null) {
if (!this.createBody()) return;
}
const body = this._body!;

// Set the linear velocity toward an angle
angle = gdjs.toRad(angle);
body.SetLinearVelocity(
this.b2Vec2(
linearVelocity * Math.cos(angle) * this._sharedData.invScaleX,
linearVelocity * Math.sin(angle) * this._sharedData.invScaleY
)
);
}

getAngularVelocity(): float {
// If there is no body, set a new one
if (this._body === null) {
Expand Down

0 comments on commit 368da1b

Please sign in to comment.