Matter-js: [Question] Move object with a platform

Created on 19 Dec 2016  路  11Comments  路  Source: liabru/matter-js

Hello! I'm wondering if it's possible to have a body move with a body that is underneath it.

I've seen this working well in the soft bodies demo. In the other rigid body demos it seems to
be the case that a body is moved by a body underneath when the body underneath is tilted slightly while moving.

My scenario is a rectangle on top of another rectangle where the lower rectangle moves exactly horizontally. Also, both rectangles have no rotation. (inertia = Infinity)

I expected that giving both objects sufficient static friction would work, but I still get the same result (lower body slides out from underneath the upper body, instead of dragging it along).

question

All 11 comments

Ah, I see your problem, it seems that the problem is more that matter.js doesn't truly support non rotating bodies. When you set the inertia of an object to infinity, it means that it is impossible for the object to be effected by outside forces. In this case, yes, that prevents it from rotating, but it also prevents it from any other sort of movement, for example in your case, force caused by friction.
Now the only way you can move an object with infinite inertia would be to directly modify its velocity or translate it's position. (gravity still effects infinite inertia objects because gravity just changes velocity directly)

What I would do to solve your case, is instead of setting the top object's inertia to infinity, manually set it's angularVelocity to 0 every step and do the same to it's angle
This should fix the issue.

I just did another test without having infinite inertia on either body - the body moving underneath the other is lying on a static surface so that it doesn't rotate. The same thing still happens.

I'm using Matter.Body.translate() to move the one that's underneath - is it possible that's the issue?

ah, yeah that might also cause some problems. You should try setting the bottom rectangle's velocity instead of it's position. If you just change it's position it basically acts as it's teleporting from one point to another, in which case the physics engine wouldn't know that it's even moving.

I tried a few more things. With velocity, no dice. Pushing the body at the bottom using another body didn't make a difference either. I'm beginning to think that the physics engine doesn't support this situation - it does make sense that if a body was moving perfectly horizontally underneath another it wouldn't be applying any force on it. That said, with gravity and friction factored in it's not what I'd expect.

Hm. That's very strange. You should make a demo on jsfiddle or codepen.io and post a link to it here.

I finally got around to recreating my scenario in jsfiddle: https://jsfiddle.net/1dayh4ef/1/

As an aside, that's the first time I've used an external library in jsfiddle before... that was surprisingly painless :D

I've tried setting friction as both 1 and Infinity with no luck

Good job haha, jsfiddle is pretty great.

Okay cool, so it looks like there are a few issues here, first of all friction values must be kept between 0 and 1 (inclusive), otherwise it's just set back to zero. Second of all, on line 36, where it says movingBox.friction = Infinity, it should be boringBox instead of movingBox, and third of all, even with seemingly everything set correctly, friction still hardly effects the boringBox. It moves a little bit(yay progress!), but then slides off. Why? I have no idea. Probably something wrong with the engine's algorithm of calculating force between two dynamic bodies, because if you set one to static, it seems to work fine.

In the meantime, I jury rigged a sort of fix-up that will work for this specific case: https://jsfiddle.net/odantnqr/

Basically you have to set the movingBox.isStatic property to true and update the moving box's velocity and translate it each step. I'm gonna go dig through the static friction demo to see if I'm missing something here. But so far it definitely seems like the engine is at fault here, not you.

Thanks, I would have never thought of that! It works great!

After staring at it a few minutes I did notice that boringBox still moves a bit; whenever the platform changes direction it takes a moment to catch up. Setting frictionStatic to Infinity actually does make it a little better - I took a quick look at Resolver and it looks like it blindly multiplies the values.

I imagine fixing friction completely for this case might be pretty difficult. If that turns out to be the case I'm considering making a plugin called matter-platformer-tools that could add a "platform listener" (which would update the position of colliding entities until they jump off)

Edit: I just realised the genius of this; setting the velocity of the static platform makes the box on top of it move, while translating the static platform only moves the platform without the box. That's really clever!

I'm a little confused, does the static friction demo not do exactly what you want here?

If it does here's the key:

  • set the moving body to static and control its position
  • also update the velocity manually

Because exactly as @Technostalgic says:

If you just change it's position it basically acts as it's teleporting from one point to another, in which case the physics engine wouldn't know that it's even moving.

You can also see the same approach used in the body manipulation demo.

Does that help? (thanks for your input here @Technostalgic)

It pretty much does, but there's one caveat - when the platform starts moving the lowest block pops out a bit (eventually causing the tower to collapse). If the static friction is set to Infinity (like in the example) I would expect it to take Infinity newtons to move the block across the platforms surface.

This is a limitation of a simple friction model I'm afraid, you'll probably need a few tricks make this work (to be expected with game physics engines).

For one thing, make the platform slowly accelerate and decelerate over a few frames rather than instantaneous acceleration, also try set the density of the bottom one to be really high at the start, then quickly switch it back.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Zhaopengyang picture Zhaopengyang  路  3Comments

drachehavoc picture drachehavoc  路  4Comments

koko236 picture koko236  路  3Comments

moe091 picture moe091  路  3Comments

kunchenguid picture kunchenguid  路  3Comments