flame-engine / forge2d

A Dart port of Box2D
BSD 3-Clause "New" or "Revised" License
181 stars 26 forks source link

How can I create a BodyComponent that keeps moving horizontally? #71

Closed usedatiek closed 2 years ago

usedatiek commented 2 years ago

PositionComponent was driven by redefining position as follows.

@override void update(double dt) { super.update(dt); position += Vector2( 1, 0) }

However, since BodyComponent's position is only get, it cannot be redefined and cannot be moved horizontally. If there is a way to handle this, could you please let me know?

spydon commented 2 years ago

In Forge2D you shouldn't set the position directly (you can do it with setTransform but it's not recommended), use component.body.applyImpulse or component.body.applyForce is the real way to go. These docs are pretty good for box2d and fairly easy to understand and translate to Forge2D: https://www.iforce2d.net/b2dtut/constant-speed

usedatiek commented 2 years ago

Now I can do the moves I expected! Thanks for the quick answer 😄