Having a hard time using the accelerometer to move my character around the screen.

I have a ball with dynamic physics applied so it can collide with walls and the floor. I am trying to move the ball around obstacles I have on the screen, however the movement is pretty wild if physics is set to dynamic, if I set the ball to static, the movement is jerky and hard to control, and it wont collide with any of my static obstacles.

Any help is appreciated

Here is my code:

--Creates Hero local function player(xCenter, yCenter, radius ) local player1 = display.newImageRect( "images/hero.png", 32, 31 ) player1.x = xCenter player1.y = yCenter player1:setFillColor( 100,100,100 ) physics.addBody( player1, "dynamic", {bounce = 0, density=1, friction=.1, radius=radius} ) return player1 end local hero = player(startPlatform.x+20, startPlatform.y-15, 15) -- Give hero movement local function heroMovex(event) hero.x = hero.x + (hero.x\*event.xGravity) hero.y = hero.y - (hero.y \* event.yGravity-1) end Runtime:addEventListener("accelerometer", heroMovex)

Hi @sebastianflores,

I’m not sure if I already gave you this advice, but you should be moving the object via normal physics gravity, not by manually setting the X and Y values on it. If you read the recent tutorial on this, you’ll see that manipulating physics objects by manual positioning is not ideal and it “fights against” the physics engine somewhat. Instead, you should adjust the gravity of the actual world depending on the accelerometer’s response/position and let Box2D naturally handle the player’s behavior.

Brent

Hello Brent,

Thank you for the reply, I took a look at that tutorial and I’m feeling a little lost here, if its not asking too much could you give me an idea on how to do this please? I’m not sure how to approach changing the worlds gravity to move the object around.

Thank you.

Hi @sebastianflores,

Fortunately, we have a sample project which shows this type of functionality. It’s located in your local Corona application folder:

CoronaSDK > SampleCode > Physics > ShapeTumbler

Note that you’ll need to install it on a real device, because the accelerometer doesn’t work in the Simulator.

Take care,

Brent

Hi @sebastianflores,

I’m not sure if I already gave you this advice, but you should be moving the object via normal physics gravity, not by manually setting the X and Y values on it. If you read the recent tutorial on this, you’ll see that manipulating physics objects by manual positioning is not ideal and it “fights against” the physics engine somewhat. Instead, you should adjust the gravity of the actual world depending on the accelerometer’s response/position and let Box2D naturally handle the player’s behavior.

Brent

Hello Brent,

Thank you for the reply, I took a look at that tutorial and I’m feeling a little lost here, if its not asking too much could you give me an idea on how to do this please? I’m not sure how to approach changing the worlds gravity to move the object around.

Thank you.

Hi @sebastianflores,

Fortunately, we have a sample project which shows this type of functionality. It’s located in your local Corona application folder:

CoronaSDK > SampleCode > Physics > ShapeTumbler

Note that you’ll need to install it on a real device, because the accelerometer doesn’t work in the Simulator.

Take care,

Brent