admin管理员组

文章数量:1426810

I am specifically looking for body options that would work when the world gravity (both x and y) is 0. My current body options are as follows:

physics: {
    frictionAir: 0,
    friction: 0,
    frictionStatic: 0,
    inertia: Infinity,
    restitution: 1,
    label: 'circle'+Date.now()+Math.random(),
    collisionFilter: {
      mask: 0x001
    },
},

Tried various binations, including continuously applying setVelocity and/or applyForce but these don't work as expected. I would expect that applying setVelocity once only would make all the bodies keep moving forever. So in my update function I do something like this:

        if(!this.setInMotion){
            Matter.Body.setVelocity(myBody, 
                {x: (Math.random() - 0.5) * 2, 
                y: (Math.random() - 0.5) * 2})
            this.setInMotion = true
        }

but the bodies simply move (slowly) to sides of the box (which is made of static rectangles) and either slide along them to corners thereof, or stop pletely without bouncing at all. Setting the setAngularVelocity does make things bounce, but then the directions and velocities after each collision bee not what's expected.

Thank you for your time.

Jared

I am specifically looking for body options that would work when the world gravity (both x and y) is 0. My current body options are as follows:

physics: {
    frictionAir: 0,
    friction: 0,
    frictionStatic: 0,
    inertia: Infinity,
    restitution: 1,
    label: 'circle'+Date.now()+Math.random(),
    collisionFilter: {
      mask: 0x001
    },
},

Tried various binations, including continuously applying setVelocity and/or applyForce but these don't work as expected. I would expect that applying setVelocity once only would make all the bodies keep moving forever. So in my update function I do something like this:

        if(!this.setInMotion){
            Matter.Body.setVelocity(myBody, 
                {x: (Math.random() - 0.5) * 2, 
                y: (Math.random() - 0.5) * 2})
            this.setInMotion = true
        }

but the bodies simply move (slowly) to sides of the box (which is made of static rectangles) and either slide along them to corners thereof, or stop pletely without bouncing at all. Setting the setAngularVelocity does make things bounce, but then the directions and velocities after each collision bee not what's expected.

Thank you for your time.

Jared

Share Improve this question edited Apr 3, 2018 at 17:56 Jared Tomaszewski asked Jan 16, 2018 at 19:22 Jared TomaszewskiJared Tomaszewski 8034 gold badges19 silver badges31 bronze badges 3
  • 1 Hi Jared - I'm also interested in this and was wondering if you have had any luck? – Frank Commented Feb 16, 2018 at 4:58
  • Still working on it :) – Jared Tomaszewski Commented Apr 3, 2018 at 17:54
  • See MJS issue #256. – ggorlen Commented Jun 22, 2024 at 18:52
Add a ment  | 

3 Answers 3

Reset to default 2

but the bodies simply move (slowly) to sides of the box (which is made of static rectangles) and either slide along them to corners thereof, or stop pletely without bouncing at all

Late to the party, but this is resolved by restingThresh in matterjs config to a value lower than the default (4). In my case (a breakout game), setting this to 0.1 did the job for me

See the following issue: https://github./liabru/matter-js/issues/394

replace

    if(!this.setInMotion){
        Matter.Body.setVelocity(myBody, 
            {x: (Math.random() - 0.5) * 2, 
            y: (Math.random() - 0.5) * 2})
        this.setInMotion = true
    }

with

if (!this.setInMotion) {
    this.setInMotion = true
    var vx = 0.001 * (Math.random() - 0.5)
    var vy = 0.001 * (Math.random() - 0.5)
    Matter.Engine._bodiesApplyGravity([myBody], { x: vx, y: vy })
}

this hack will use applyGravity inner matterjs engine function to properly apply push motion in direction vx, vy when world.gravity is set to 0

Regards, Jacek

You need to set frictionStatic to 1

physics: {
    frictionAir: 0,
    friction: 0,
    frictionStatic: 1,
    inertia: Infinity,
    restitution: 1,
    label: 'circle'+Date.now()+Math.random(),
    collisionFilter: {
       mask: 0x001
    },
},

本文标签: