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
3 Answers
Reset to default 2but 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
},
},
本文标签:
版权声明:本文标题:javascript - Matter.js How to make bodies (circles) bounce endlessly (perfect elasticity) in a box with 0 gravity - Stack Overfl 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745485681a2660372.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论