dpham… since I was shooting in the dark, I decided to work up some example code of your issue since I have never played with imput from the accelerometer before and really didn’t understand what I was talking about.
I replaced the listener function. When I left ballObject1.x = ballObject1.x + yGravity
I could get the ball to “slide” down the platform, but it didn’t roll. LOL
So I have applied physics force to the object and it works as expected I believe.
Build this code and load it onto your device, it works with iOS as is if build.settings is set to landscape view.
You can play with all the parameters, I think it’s cool how the ball spins, as if a tire on ice, to gain traction when the device is tilted.
[code]–dpham Ball Roll project
local perspective = require(“perspective”)
local physics = require(“physics”);
local defaultWidth = 480
local defaultHeight = 320
local displayWidth = display.viewableContentWidth
local displayHeight = display.viewableContentHeight
local yMargin = 20
local centerX = defaultWidth/2;
local centerY = defaultHeight/2;
physics.start();
physics.setScale( 60 ) --was 60 – a value that seems good for small objects (based on playtesting)
physics.setGravity( 0, 9.8 )
local BallObjectCollisionFilter = { categoryBits = 1, maskBits = 3 }
local FloorCollisionFilter = { categoryBits = 2, maskBits = 1 }
local FloorGroup = display.newGroup()
local camera
local ballObject1
local function onTilt( event )
ballObject1:applyForce( 8 * event.yGravity)
end
local function buildStage()
camera=perspective.createView()
ballObject1 = display.newImageRect (“ballobject.png”,42,42)
ballObject1.x = 200
ballObject1.y = 50
physics.addBody( ballObject1, “dynamic”, { density=10.0, bounce=0, friction=.25, radius=21, filter = BallObjectCollisionFilter} )
ballObject1.linearDamping = .3
local FloorShape = { -240,-7, 240,-7, 240,7, -240,7 }
local FloorBody = { density=1, friction=.5, bounce=0, shape = FloorShape, filter = FloorCollisionFilter }
local FloorDemo = display.newRect( 0, 0, 480, 10 )
camera:add(FloorDemo, 2, false)
physics.addBody(FloorDemo, “static”, FloorBody)
FloorDemo.x = centerX
FloorDemo.y = centerY + 155
FloorDemo:setFillColor(50, 190, 235 )
FloorDemo.alpha = 1
FloorDemo.isVisible = true
FloorDemo.alpha = 1
FloorDemo.ID = “Floor”
FloorDemo.isBodyActive = true
Runtime:addEventListener( “accelerometer”, onTilt )
camera:add(ballObject1, 4, true)
camera:setBounds(-1000, 1000, -1000 , 1000)
–camera:setBounds(0,480,0,480)
camera:track()
end
local function reset(event)
if event.phase == “ended” then
camera:destroy()
buildStage()
end
end
local TextGroup = display.newGroup()
local Reset = display.newText(“RESET”, 0, 0, “Helvetica”, 20 )
Reset:setReferencePoint(display.CenterCenterReferencePoint)
Reset.x = 50
Reset.y = centerY - 100
Reset:setTextColor (255, 255, 255 )
Reset.isVisible = true
TextGroup:insert(Reset)
Reset:addEventListener(“touch”, reset)
buildStage()
[/code]
I’m not sure if the collision filters are needed or not, I just wrote what I thought would work.
Hope this helps,
Nail [import]uid: 106779 topic_id: 34408 reply_id: 137058[/import]