Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore gravity is not working for matter physics #6473

Closed
peer2p opened this issue Apr 15, 2023 · 3 comments
Closed

Ignore gravity is not working for matter physics #6473

peer2p opened this issue Apr 15, 2023 · 3 comments

Comments

@peer2p
Copy link

peer2p commented Apr 15, 2023

Version

  • Phaser Version: 3.60.0
  • Operating system: MacOS Ventura 13.3
  • Browser: Chrome 112.0.5615.49

Description

The boolean property ignoreGravity on matter MatterJS.BodyType is not working anymore since release 3.60.0.
Although it is set to true, the body is affected by gravity.

Example Test Code

export class Bullet extends Phaser.Physics.Matter.Sprite {
constructor(...) {
super(...);
...
this.setIgnoreGravity(true);
...
}
}

Additional Information

The evaluation of the flag is missing in method Engine._bodiesApplyGravity.
This should be the correct code for this method, as it was before.
Also without typos in the jsdoc :)

/**
 * Applies a mass dependent force to all given bodies.
 * @method _bodiesApplyGravity
 * @private
 * @param {body[]} bodies
 * @param {vector} gravity
 */
Engine._bodiesApplyGravity = function(bodies, gravity) {
    var gravityScale = typeof gravity.scale !== 'undefined' ? gravity.scale : 0.001,
        bodiesLength = bodies.length;

    if ((gravity.x === 0 && gravity.y === 0) || gravityScale === 0) {
        return;
    }

    for (var i = 0; i < bodiesLength; i++) {
        var body = bodies[i];

        if (body.ignoreGravity || body.isStatic || body.isSleeping){
            continue;
        }
        
        // add the resultant force of gravity
        body.force.y += body.mass * gravity.y * gravityScale;
        body.force.x += body.mass * gravity.x * gravityScale;
    }
};
@Gathaa
Copy link

Gathaa commented Apr 18, 2023

Hello , i looked for the solution online in order To apply your fix, you can create a custom plugin for Phaser 3 that patches the Engine._bodiesApplyGravity method with the corrected version. Here's what i found :

Create a new file called MatterGravityFixPlugin.js:

export class MatterGravityFixPlugin extends Phaser.Plugins.ScenePlugin {
constructor(scene, pluginManager) {
super(scene, pluginManager);
}
boot() {
const Matter = Phaser.Physics.Matter.Matter;
this.applyGravityFix(Matter);
}
applyGravityFix(Matter) {
Matter.Engine._bodiesApplyGravity = function (bodies, gravity) {
var gravityScale = typeof gravity.scale !== 'undefined' ? gravity.scale : 0.001,
bodiesLength = bodies.length;

  if ((gravity.x === 0 && gravity.y === 0) || gravityScale === 0) {
    return;
  }

  for (var i = 0; i < bodiesLength; i++) {
    var body = bodies[i];

    if (body.ignoreGravity || body.isStatic || body.isSleeping) {
      continue;
    }
    body.force.y += body.mass * gravity.y * gravityScale;
    body.force.x += body.mass * gravity.x * gravityScale;
  }
};

}
}
//////////////////////////////////////////////////////////////////////////////////
Import the plugin in your main game file:
// /////////////////////////////////////////////////////////////Code
import { MatterGravityFixPlugin } from './MatterGravityFixPlugin';
Register the plugin in your game configuration:

const config = {
plugins: {
scene: [
{
key: 'MatterGravityFixPlugin',
plugin: MatterGravityFixPlugin,
mapping: 'matterGravityFix',
start: true,
},
],
},
};
/////////////////////////////////////////////////////////////////////////////////////////
Now, the custom plugin will apply the gravity fix you provided when the game starts.

@Dercetech
Copy link

const Matter = Phaser.Physics.Matter.Matter;

this line fails to pass tslint in my environment... any idea why Matter.Matter wouldn't ?

I have to force the following:

const Matter = (Phaser.Physics.Matter as any).Matter;

That being said, great fix. Thanks!

@photonstorm
Copy link
Collaborator

Thank you for submitting this issue. We have fixed this and the fix has been pushed to the master branch. It will be part of the next release. If you get time to build and test it for yourself we would appreciate that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants