Hi all,
the other day I was trying to control a Box2d Body with the accelerometer.
The example I have found in the documentation uses setPosition() to move a Sprite, but as stated in this thread :
What is the right way to move a body? setPosition() should be avoided for Bodies.
Although I have found this article by
@ar2rsawseen :
Using Accelerometer with Box2d in Giderosthe method described in the article modifies the whole world's gravity
self.world:setGravity(self.fx*self.gravityScale, -self.fy*self.gravityScale) |
I would like the accelerometer to control only one Box2d body.
How would you solve it?
Thank you
Comments
so you can use setlinearvelocity and applyforce [actually gravity is also one kind of force multiplied with the mass of body and applied each time] for them
also as you said setPosition() "SHOULD" be avoided for Bodies.
thank you.
Well you know I already came to the same conclusions by reading the articles
The thing is : how does the formula apply with accelerometer?
If anybody's done that before that would help (if I could avoid reinvent the wheel).
If not I will try again when I have more time but I believe it involves maths which is not my forte.
Likes: Mells
Actually the effect is a bit different : to keep the body stationary, on your solution the player needs to keep the device flat, while with the other solution if the player keeps the device at an angle and doesn't move, the body doesn't move too.
I believe the solution involves calculating velocity by checking the distance between x/y Acceleration at the previous frame, and the current frame, and do something with it...
I am not sure I'm on the right track.
@ar2rsawseen do you have an article about that on your site? i think I have searched the archives and didn't find.
I will search on the forums more..
I found this helpful for acceleration to a max speed: http://www.iforce2d.net/b2dtut/constant-speed