ApplyForce function controlled by Accelerometer?

Hi, as the title implies, I’m trying to use the accelerometer to control an applyForce function so that I can move an object left and right. I’m obviously doing something wrong here, because I’m not getting any movement from using the accelerometer. Is there a preferred method for something like this? Thanks in advance.

[code]

local acc = {}
motiony = 0
motionx = 100

function acc:accelerometer(event)
motionx = motionx * event.xGravity
end
Runtime:addEventListener(“accelerometer”, acc)

function forceApplyer ( event )
print( "Value = " … tonumber( motionx ) )
ball:applyForce( motionx, motiony, ball.x, ball.y )
end
Runtime:addEventListener (“enterFrame”, forceApplyer) [import]uid: 66329 topic_id: 23435 reply_id: 323435[/import]

Try taking a look at ShapeTumbler sample code - this shows almost the exact same thing. (Only gravity is adjusted, not motionx.)

Peach :slight_smile: [import]uid: 52491 topic_id: 23435 reply_id: 94035[/import]

Thanks for the reply Peach. I did take a look at the ShapeTumbler code, but it appears that the gravity it affected in direct correlation with the accelerometer, without any sort of number or string output that I could use for my motionx. I’m going to make a couple more attempts at this, (maybe just replacing the ShapeTumbler’s gravity code with a variation of my motionx. I appreciate the help, and I will report back when I find a solution. [import]uid: 66329 topic_id: 23435 reply_id: 94043[/import]

try this

[lua] function acc:accelerometer(event)
motionx = 100
motionx = motionx * event.xGravity
end
Runtime:addEventListener(“accelerometer”, acc) [/lua] [import]uid: 12482 topic_id: 23435 reply_id: 94046[/import]

Okay, here’s where I am now. I tried (and adapted a bit) hgvyas123’s code (thanks, by the way), and I came up with the same result that I came across before: The ball only moves in one direction no matter which way I tilt the device. Any other suggestion? Here’s the code.

[code]
function acc(event)
motiony = 0
motionx = 10000
motionx = motionx * event.xGravity
ball:applyForce( motionx, motiony, ball.x, ball.y )
end

Runtime:addEventListener(“accelerometer”, acc) [import]uid: 66329 topic_id: 23435 reply_id: 94052[/import]

i dont have device to chk but try this it will work as expected

[lua]function acc(event)
motiony = 0
motionx = 100
motionx = motionx * event.xGravity
ball:setLinearVelocity( motionx, motiony )
end

Runtime:addEventListener(“accelerometer”, acc) [/lua]

[edit]

you should have to adjust value accordingly as i have not tested this [import]uid: 12482 topic_id: 23435 reply_id: 94057[/import]

Thanks hgvyas123, just tried out the code. Unfortunately, I’m still running into the issue stated in my last post. Does the accelerometer have any kind of numerical output that could be used? [import]uid: 66329 topic_id: 23435 reply_id: 94179[/import]

hi,

the problem with applyforce is something like,

at the first frame you are applying force of 50 to object as we have to put this thing into the enterframe event at each frame it will apply force of 50 so at second frame total force on object is 100 at third it is 150 and so on…(with the assumption of there is no friction or collision with other object)

now when you apply force -50 it will be simply deducted from total force (150 - 50 = 100 )so still is going to the one direction until the totalforce is negative thats why i have ask to use linearvelocity i think it will solve your problem unfortunetly still i dont have device to chk :frowning:

let me know if it cant work

:slight_smile: [import]uid: 12482 topic_id: 23435 reply_id: 94362[/import]

the accelerometer value is always between -1 to 1 may be u already knows that [import]uid: 12482 topic_id: 23435 reply_id: 94363[/import]

Ok, your post (#7) makes sense. I hadn’t even though of that. My current code is working decently, I plan to change and/or improve it later on though. Here it is:

Thank you very much hgvyas123 for the help.

[code]
bX, bY = ball:getLinearVelocity()
function accelerometerCall(e)
bX, bY = ball:getLinearVelocity()
ball:setLinearVelocity(e.yGravity * -1300, bY)
end

Runtime:addEventListener(“accelerometer”, accelerometerCall) [import]uid: 66329 topic_id: 23435 reply_id: 94375[/import]

welcome dude
:slight_smile: [import]uid: 12482 topic_id: 23435 reply_id: 94383[/import]