Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to use Accelerometer with a Box2d Body? — Gideros Forum

How to use Accelerometer with a Box2d Body?

MellsMells Guru
edited March 2013 in General questions
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 Gideros
the 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
twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps

Comments

  • hgvyas123hgvyas123 Guru
    edited March 2013
    in the case there are more than one dynamic body then obviously you can't use setgravity to control only one dynamic body

    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.


  • MellsMells Guru
    edited March 2013
    @hgvyas
    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.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • hgvyas123hgvyas123 Guru
    edited March 2013
    i think putting this lines in enterframe will do the work
    local x, y, z = accelerometer:getAcceleration()
     
    local tempX,tempY = body:getLinearVelocity() -- to store the current y speed
    body:setLinearVelocity(x*10,tempY) -- change number 10 as per your need

    Likes: Mells

    +1 -1 (+1 / -0 )Share on Facebook
  • hgvyas123hgvyas123 Guru
    edited March 2013
    ofcourse after starting the accelerometer with below code
    local accelerometer = Accelerometer.new()
    accelerometer:start()
  • MellsMells Guru
    edited March 2013
    @hgvyas123 thank you for trying many times :)

    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..
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • MellsMells Guru
    edited March 2013
    For now this is the closest that I could get but I am not sure it's the best way to do it :
    local prevX, prevY, prevZ, sensitivity
     
    sensitivity = 3000
     
    -- Get accelerometer values on first frame
    if (prevX == nil) and (prevY == nil) and (prevZ == nil) then
    	prevX, prevY, prevZ = accelerometer:getAcceleration()	
    end
     
    -- get accelerometer values
    x, y, z = accelerometer:getAcceleration()
     
    -- do the low-pass filtering
    fx = x * filter + fx * (1 - filter)
    fy = y * filter + fy * (1 - filter)
    fz = z * filter + fz * (1 - filter)
     
    prevFx = prevX * filter + fx * (1 - filter)
    prevFy = prevY * filter + fy * (1 - filter)
    prevFz = prevZ * filter + fz * (1 - filter)
     
    -- Position
    self.playerRef.body:setLinearVelocity((prevFx-fx) * sensitivity, -(prevFy-fy) * sensitivity)
     
    -- get accelerometer values						
    prevX, prevY, prevZ = accelerometer:getAcceleration()
    But anyway, if it helps someone...
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • zvardinzvardin Member
    edited March 2013
    If you want the body to accelerate to its top speed you will probably want to use applyLinearImpulse, otherwise I'd stick with the setLinearVelocity like you're doing.

    I found this helpful for acceleration to a max speed: http://www.iforce2d.net/b2dtut/constant-speed
Sign In or Register to comment.