how to use gyroscope and accelerometer to move a ball ?

how can i a move ball on a board using gyroscope and accelerometer like this game:

https://play.google.com/store/apps/details?id=se.illusionlabs.labyrinth.lite

i saw the sample code for gyroscope and accelerometer and try edit it but not work
also i saw the sample code for accelerometer which come with android sdk but it difficult to translate to corona [import]uid: 118973 topic_id: 35113 reply_id: 335113[/import]

If you go into the SampleCode with your Corona SDK install, then go into the Hardware/Accelerometer1 folder you should fine a sample that talks about using the accelerometer.

[import]uid: 199310 topic_id: 35113 reply_id: 139591[/import]

Thank you for replay …
I tried the sample code but the movement of the ball not accurate it always return to the center after the device rest :confused:
[import]uid: 118973 topic_id: 35113 reply_id: 139615[/import]

finally it work

i just change the code in the sample code for accelerator from :
[lua]Circle.x = centerX + (centerX * event.xGravity)
Circle.y = centerY + (centerY * event.yGravity * -1)[/lua]

to:
[lua]Circle.x = Circle.x + event.xGravity * 20
Circle.y = Circle.y + event.yGravity * -20 [/lua] [import]uid: 118973 topic_id: 35113 reply_id: 139624[/import]

the best example is “ShapeTumbler” under physic it use accelerometer to move the objects
thank you corona
:slight_smile: [import]uid: 118973 topic_id: 35113 reply_id: 139715[/import]

If you go into the SampleCode with your Corona SDK install, then go into the Hardware/Accelerometer1 folder you should fine a sample that talks about using the accelerometer.

[import]uid: 199310 topic_id: 35113 reply_id: 139591[/import]

Thank you for replay …
I tried the sample code but the movement of the ball not accurate it always return to the center after the device rest :confused:
[import]uid: 118973 topic_id: 35113 reply_id: 139615[/import]

finally it work

i just change the code in the sample code for accelerator from :
[lua]Circle.x = centerX + (centerX * event.xGravity)
Circle.y = centerY + (centerY * event.yGravity * -1)[/lua]

to:
[lua]Circle.x = Circle.x + event.xGravity * 20
Circle.y = Circle.y + event.yGravity * -20 [/lua] [import]uid: 118973 topic_id: 35113 reply_id: 139624[/import]

the best example is “ShapeTumbler” under physic it use accelerometer to move the objects
thank you corona
:slight_smile: [import]uid: 118973 topic_id: 35113 reply_id: 139715[/import]

i can move ball using accelerometer(thnx for corona sample program) but i cant limit the movement of the ball within screen , any idea ???

Hi @nowfalsalam,

If you want to keep the ball inside the bounds of the screen, you should probably construct 4 invisible walls, just outside the screen edges, so the ball collides with them and stays inside the screen.

Please check out all of the physics guides to help accomplish this:

http://www.coronalabs.com/resources/tutorials/physics-box2d/

Take care,

Brent

When you move your ball, in that function you can test to see if the X, Y of the ball is outside of some bounds you’ve defined. If keeping it on the screen is your goal you can do:

if ball.x > ball.width/2 and

   ball.x < display.contentWidth - ball.width/2 and

   ball.y < ball.height/2 and

   ball.y > display.contentHeight - ball.height/2 then

       – allow your ball to move

end

Thanks Brent , for your reply,

now it works good, i have another problem , in my app there is two balls both moves with the help of accelerometer simultaneously, now, when one ball collide with any wall or any other object, second ball continoue  its movement independently. But i want stop movement of second one too when first one collide with wall and  vice versa. I want simultaneous  movement of balls in all time. can you help me please 

Than you for your reply

Thank God and Corona, Finally my problem solved thanks to corona global collision detection

i can move ball using accelerometer(thnx for corona sample program) but i cant limit the movement of the ball within screen , any idea ???

Hi @nowfalsalam,

If you want to keep the ball inside the bounds of the screen, you should probably construct 4 invisible walls, just outside the screen edges, so the ball collides with them and stays inside the screen.

Please check out all of the physics guides to help accomplish this:

http://www.coronalabs.com/resources/tutorials/physics-box2d/

Take care,

Brent

When you move your ball, in that function you can test to see if the X, Y of the ball is outside of some bounds you’ve defined. If keeping it on the screen is your goal you can do:

if ball.x > ball.width/2 and

   ball.x < display.contentWidth - ball.width/2 and

   ball.y < ball.height/2 and

   ball.y > display.contentHeight - ball.height/2 then

       – allow your ball to move

end

Thanks Brent , for your reply,

now it works good, i have another problem , in my app there is two balls both moves with the help of accelerometer simultaneously, now, when one ball collide with any wall or any other object, second ball continoue  its movement independently. But i want stop movement of second one too when first one collide with wall and  vice versa. I want simultaneous  movement of balls in all time. can you help me please 

Than you for your reply