how to use gyroscope and accelerometer to move a ball ?

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

hey rob …

I just tried the sample code … no issues in keeping the ball within boundary limits … but as soon as I replaced the circle with an image … the movement lost its smoothness and kind of doesn’t follow proper directions …

here’s what I did :

display.setStatusBar( display.HiddenStatusBar ) local centerX = display.contentCenterX local centerY = display.contentCenterY local physics = require "physics" physics.start() \_H=display.contentHeight \_W=display.contentWidth -- used to update our Text Color (once per frame) display.setDefault( "anchorX", 0.0 ) -- default to TopLeft anchor point for new objects display.setDefault( "anchorY", 0.0 ) ball = display.newImageRect("ball.png", 30, 30) ball.x = centerX ball.y = centerY physics.addBody( ball, "static",{ density=0.9, friction=0.5, bounce=0.5, radius=15 } ) local function onAccelerate( event ) ball.x = ball.x + event.xGravity \* 20 ball.y = ball.y + event.yGravity \* -20 end system.setAccelerometerInterval( 75 ) Runtime:addEventListener ("accelerometer", onAccelerate);

it just doesn’t run like the sample code … 

Hi @jitenahuja,

This is a very old thread. What sample code are you speaking of? In this case, the ball definitely should not be “static” body type. If you’re borrowing this code from a non-physics accelerometer sample, then it would need to be adapted to use physics properly.

Best regards,

Brent

Yes I’m using the accelerometer sample code under hardware … I just wanted to do the same thing that the sample code does just with an image instead of circle … and I used the ball as static because I don’t want any gravitational forces to act on it … It should only move based on accelerometer values … 

Y would it have problem if I use a img instead of a circle … For the same code if i use circle , it works like charm but very irrational behaviour with img ?

If you are not using gravity, why are you using physics?

If you need physics but don’t want gravity, you can still use “dynamic”, but set the gravity scale on the object to 0.   See: https://docs.coronalabs.com/api/type/Body/gravityScale.html for more information.

Rob

hey rob …

I just tried the sample code … no issues in keeping the ball within boundary limits … but as soon as I replaced the circle with an image … the movement lost its smoothness and kind of doesn’t follow proper directions …

here’s what I did :

display.setStatusBar( display.HiddenStatusBar ) local centerX = display.contentCenterX local centerY = display.contentCenterY local physics = require "physics" physics.start() \_H=display.contentHeight \_W=display.contentWidth -- used to update our Text Color (once per frame) display.setDefault( "anchorX", 0.0 ) -- default to TopLeft anchor point for new objects display.setDefault( "anchorY", 0.0 ) ball = display.newImageRect("ball.png", 30, 30) ball.x = centerX ball.y = centerY physics.addBody( ball, "static",{ density=0.9, friction=0.5, bounce=0.5, radius=15 } ) local function onAccelerate( event ) ball.x = ball.x + event.xGravity \* 20 ball.y = ball.y + event.yGravity \* -20 end system.setAccelerometerInterval( 75 ) Runtime:addEventListener ("accelerometer", onAccelerate);

it just doesn’t run like the sample code … 

Hi @jitenahuja,

This is a very old thread. What sample code are you speaking of? In this case, the ball definitely should not be “static” body type. If you’re borrowing this code from a non-physics accelerometer sample, then it would need to be adapted to use physics properly.

Best regards,

Brent

Yes I’m using the accelerometer sample code under hardware … I just wanted to do the same thing that the sample code does just with an image instead of circle … and I used the ball as static because I don’t want any gravitational forces to act on it … It should only move based on accelerometer values … 

Y would it have problem if I use a img instead of a circle … For the same code if i use circle , it works like charm but very irrational behaviour with img ?

If you are not using gravity, why are you using physics?

If you need physics but don’t want gravity, you can still use “dynamic”, but set the gravity scale on the object to 0.   See: https://docs.coronalabs.com/api/type/Body/gravityScale.html for more information.

Rob