Matter-js: How to lock the player's rotation so that he remains vertical at all times?

Created on 16 Aug 2019  路  6Comments  路  Source: liabru/matter-js

I want to keep the player sprite vertical at all times, regardless. Right now, if it comes in contact with certain objects it tips over on its side. I found a lot of documents on the web related to this, but I'm having a recurrent problem with the documents I find using the primary Matter namespace. The examples in those documents don't match interfaces I find with inspecting the matter property attached to a Phaser 3 object. When I try the techniques described in the Matter namespace documents they don't work, usually with the demonstrated property being undefined.

I tried manually setting the angle property attached to the player body property, but Matter JS appears to interpret that sudden change in angle as a massive velocity event and sends the player sprite flying off the visible world:

player.body.angle = 0;

I tried setting the player's inertia to infinity, but then it won't move:

player.body.setInertia('Infinity');

How can I keep the player always vertical?

Also, if someone could point me to a document that explains how to use Matter JS specifically when operating in the Phaser JS environment, I'd really appreciate it. I've lost literally days learning how to do various operations that should have been straightforward. For example, I still haven't figured out how to access the Matter-JS SAT module from within a Phaser 3 app so I can do advanced collision detection like detecting when my player is colliding with any game objects or not. The matter property attached to the Phaser 3 instance does not have a SAT property.

question

All 6 comments

wow unanswered qustion. not good, not good....

U can set angle every tick
Events.on(engine, 'afterUpdate', function(){player.body.angle = 0;});

i would do some extra options in this library if i where developer

update

still there's no way to disable body angular velocity

hope developers will respond something

i have a proposal - add to player rectangle another small rectangle at the bottom over the main one and set mass of this new one to some high value. So it will work as an anchor

got my idea?

found another solution

Events.on(engine, 'beforeUpdate', function(){
Matter.Body.setAngularVelocity(body, 0)
});

@roschler

You can set the inertia to infinity, and that should prevent your body from rotating.

Matter.Body.setInertia(body, Infinity);

@speekadievs thanks for answering, that's usually what I suggest. I will add an example for this since it gets asked so often.

I'm unable to help with Phaser issues sorry, please post those in the Phaser issue tracker.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TimuJiang picture TimuJiang  路  4Comments

elrodsj picture elrodsj  路  3Comments

cluber22 picture cluber22  路  3Comments

253153 picture 253153  路  3Comments

liabru picture liabru  路  3Comments