Accelerometer Acceleration Speed? How do you change speed?

Hi guys,

I'm having a problem with the speed the object moves in the X direction. The accelerometer acceleration seems to be too slow. Does anyone know how I can increase the sensitivity so the object can fly across the screen fast.

Thanks for all you help and support,

Regards,
Jordan Schuetz
Ninja Pig Studios

*My code for reference*

1
2
3
4
5
6
7
8
9
10
11
12
13
14
system.setAccelerometerInterval( 50 )
 
pig_1 = display.newImage("annoyingpig.png")
        pig_1.xScale = .17; pig_1.yScale = .17;
        local centerX = 50
        local centerY = 170
        pig_1.x = centerX
        pig_1.y =centerY
 
local function onAccelerate( event )
        pig_1.y = centerY + (centerY * event.xGravity)
end
 
Runtime:addEventListener ("accelerometer", onAccelerate)

You can use this and just increase the number to make it move quicker.

1
2
3
4
5
local function onAccelerate( event )
        pig_1.y = (10 * event.xGravity)
end
 
Runtime:addEventListener ("accelerometer", onAccelerate)

Oh never mind, I see what the problem is. I'm going to sleep now (Days of nonstop coding :) ), i'll look into it tomorrow.

Try this, and play around with the number 3. The higher it is the quicker it will move. Let me know if it works for you.

1
2
3
4
5
6
7
8
9
10
11
12
pig_1 = display.newImage("annoyingpig.png")
        pig_1.xScale = .17; pig_1.yScale = .17;
        pig_1.x = 160
        pig_1.y = 240
        centerX = pig_1.x
        centerY = pig_1.y
 
local function onAccelerate( event )
        pig_1.y =  (centerY * event.xGravity) * 3
end
 
Runtime:addEventListener ("accelerometer", onAccelerate)

No that doesn't work. The reason it doesn't is because then it makes the pig start at the top of the screen why it should start in the middle. It then makes the object controled by the accelerometer fly off the top of the screen and not the bottom.

Hope you can take another look.

Regards,
Jordan Schuetz
Ninja Pig Studios

Correct me if I am wrong, you want an object to start in the middle of the screen and then when you move the device on the y-axis you want the object to move right and left with increasing speed, correct? I can't sleep :/ haha

Yes, but the phone has to move on the x Axis to move the pig on the Y axis. I just want the pig to move fast.

try something like this

1
2
3
4
5
6
7
8
9
local rect = display.newRect(0,0,100,100)
rect.x = 240
rect.y = 70
local function onAccelerate( event )
        rect.y =  rect.y + event.xGravity * 10
--increase 10 if want to move more faster
end
 
Runtime:addEventListener ("accelerometer", onAccelerate)

No see that doesn't work. That only changes the Y cordinates for the object. It doesn't increase the speed, it just makes the object start off screen. I need something to make the accelerometer more sensitive.

Here you go, as smooth as can be.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
_W = display.contentWidth;
_H = display.contentHeight;
 
system.setAccelerometerInterval( 100 )
 
local physics = require ("physics");
physics.start();
physics.setGravity(-9.8 , 0);
physics.setDrawMode("");
 
cannonBall2 = display.newCircle(0, 0, 4);
cannonBall2.x = _W * 0.5;
cannonBall2.y = _H * 0.5;
physics.addBody(cannonBall2, "kinematic");
 
local function onAccelerate( event )
        cannonBall2.y =  cannonBall2.y + (event.xGravity * 10)
end
 
Runtime:addEventListener ("accelerometer", onAccelerate)

Khaodik,

Thank you so much! It works! The problem was this:
 pig_1.y = centerY + (centerY * event.xGravity)

Now when I delete the (centerY) and add (event.xGravity * 40) I get the tilt that I'm looking for. I would like to thank you so much for your time. I really do appreciate it :) I hope to see you at the Ansca meetup in Palo Alto this August 11th.

Regards,
Jordan Schuetz
Ninja Pig Studios

Everyone,

Please note that the accelerometer listener can be called late on different devices. This is a limitation of the operating system on iOS and Android, not in Corona. Since this listener provides accelerometer data as "acceleration" (meter / seconds squared) and not distance actually traveled, you may find that moving you display object as mentioned above will be slower/faster on different devices.

To help compensate for this, the latest daily builds of the Corona SDK now provides "event.deltaTime" for the accelerometer. This provides amount of time since the last measurement in seconds, where the fractional part of the number is sub-seconds. Now, you shouldn't use deltaTime to calculate distance traveled since only acceleration is known, not the velocity, but you CAN use it to normalize your calculations to work across all devices.

Here is a link to documentation about the new "event.deltaTime" property...
http://developer.anscamobile.com/reference/index/events/accelerometer/eventdeltatime

No problem, i'm happy I could help! I'll try to be there.

views:1879 update:2011/10/10 9:00:20
corona forums © 2003-2011