Tilt-Based Gravity

So what I am trying to do is make one object move on the bottom of the screen when the device is tilted one way of the other. I want that object to stay at the bottom of the screen, and I don’t want any other objects to move when the device is tilted. If you post a code please explain every part of the code if you can.

Thanks! [import]uid: 104376 topic_id: 22034 reply_id: 322034[/import]

And I also want it to be a sprite. Meaning that when the object moves back and forth different images appear. But when the objects not moving, I don’t want it to move.

Thanks in advance! [import]uid: 104376 topic_id: 22034 reply_id: 87585[/import]

You might consider looking at the D-pad tutorial on Techority and then adapting that for use with tilt rather than buttons.

For the sprite, you could pause it if the speed (assuming you name the variable for movement “speed”) was 0, play a right animation if it were >0 and a left animation if < 0.

Make sense? [import]uid: 52491 topic_id: 22034 reply_id: 87624[/import]

yes it does.

can you please post a code that has tilt-gravity, because I couldn’t find it anywhere. And also i will have many objects on the screen, but i only need one object to react to tilt gravity.

Thanks! [import]uid: 104376 topic_id: 22034 reply_id: 87627[/import]

Well shape tumbler sample code (in CoronaSDK > SampleCode > Physics > ShampeTumbler) has an example for tilt based gravity.

That is for multiple objects but you will see how to do it.

For only one object effected by gravity you would make the other objects static. [import]uid: 52491 topic_id: 22034 reply_id: 87643[/import]

I’ve looked at that already, but the thing is, all the other objects are supposed to be affected my regular gravity. [import]uid: 104376 topic_id: 22034 reply_id: 87671[/import]

Can anyone else help me with making tilt based gravity? [import]uid: 104376 topic_id: 22034 reply_id: 87748[/import]

Make the other objects not dynamic ?!

http://developer.anscamobile.com/reference/index/bodybodytype [import]uid: 73033 topic_id: 22034 reply_id: 87758[/import]

yeah, I am going to do that, but now I need the code for tilt based gravity. [import]uid: 104376 topic_id: 22034 reply_id: 87759[/import]

Add an event listener to listen accelerometer data,
then you can use the returned value for anything you like.
system.setAccelerometerInterval( 100 )
Runtime:addEventListener( “accelerometer”, fnTilt )
function fnTilt( event )
local xGravity = event.xGravity
local yGravity = event.yGravity
– stuff here
end
could be a good start, note that I did not tested the code. [import]uid: 73033 topic_id: 22034 reply_id: 87761[/import]

would this work?

[lua]–sprite sheet
local guysheet = sprite.newSpriteSheet(“guy_sprite.png”, 100, 100)

local guyset = sprite.newSpriteSet (guysheet, 1, 3)
sprite.add (guyset, “guyright”, 2, 2, 500, 0)
sprite.add (guyset, “guyleft”, 2, 2, 500, 0)
sprite.add (guyset, “guystop”, 1, 1, 500, 0)

–add guy
local guy = sprite.newSprite (guyset)
guy.x = 320
guy.y = 900
physics.addBody(guy)
guy.myName = “guy”

gameLayer:insert(guy)

– guy stay on x-axis
local function stay (event)
if guy.y < 900 then
guy.y = 900
elseif guy.y > 900 then
guy.y = 900
end
end

Runtime:addEventListener(“enterFrame”, stay)

– guy dont rotate
guy.isFixedRotation = true
local state = guy.isFixedRotation

– attemted guy tilt
system.setAccelerometerInterval( 70 )
function fnTilt( event )
guy.x =guy.x - (10 * event.xGravity)
end

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

I also want to make the sprites play when the ‘guy’ move either right or left. I am not sure how to do that. [import]uid: 104376 topic_id: 22034 reply_id: 87773[/import]

First of all, you need a proper spritesheet for that. As far as I see from lines 4-7, your current sheet is only 3 frames.
After getting a proper spritesheet for animation and setting it like you do in your code,you can check which side the phone is being tilted, add a if/else statement there and use
guy:play(“guyleft”) (or guy:play(“guyright”) or whatever suitable for you)

There is a good tutorial at Peach Pellen’s site about this, not gravity related but its about controlling sprites with a D-Pad,
I suggest give it a shot.

Cheers [import]uid: 73033 topic_id: 22034 reply_id: 87835[/import]