liabru / matter-js

a 2D rigid body physics engine for the web ▲● ■
MIT License
16.65k stars 1.96k forks source link

[Question] Apply Constant force #279

Closed tevch closed 8 years ago

tevch commented 8 years ago

I know how to apply force, but looks like it applies once, losing momentum with time

what i want to ask is - how to apply(introduce) constant force, like gravity. for instance - lift, or thrust, or drag of flying body.

Thanks

liabru commented 8 years ago

You should use an engine event to apply it on every update.

liabru commented 8 years ago

Hopefully that answered your question so I'll close this, but feel free to comment if you still need more info.

courtneyvigo commented 7 years ago

I have a similar question where i would like to add gravity once an animated object reaches the center of the screen. Is this possible?

liabru commented 7 years ago

Sure you can use events for this, check the distance of body.position to the target point every tick, when it gets lower than a threshold set a flag and then add forces.

courtneyvigo commented 7 years ago

Thank you! Ill be trying this today :)

courtneyvigo commented 7 years ago

I think I might be confused as to how to check the position of the body and how to apply the gravity force once it is in the ideal position. Syntax-wise

goal:

example.zip

beetrandahiya commented 3 years ago

Hey, I have a question, can u please just take a second and tell me how to use the engine event to apply constant force, for instance say , I want to apply force on a particle in electric field, so, How do I apply the force on that particle. Thanks

bouraqadi commented 3 years ago
Matter.Events.on(engine, 'beforeUpdate', function(event) {
//Apply force
});

This event is sent on every iteration of the simulation loop. More at: https://brm.io/matter-js/docs/classes/Engine.html#events

VinamraGoyal29 commented 3 years ago

How to applyForce on an circular object

bouraqadi commented 3 years ago

https://brm.io/matter-js/docs/classes/Body.html#method_applyForce

davidreis97 commented 3 years ago

I feel like this is a bit weird. Why do I have to iterate and set force in all my physics entities with forces applied to them before an engine update? I feel like body.force shouldn't be reset to (0,0) after a physics step.

liabru commented 3 years ago

@davidreis97 there's a plugin that makes things easier check out matter-attractors