Object accelerometer controlled not respecting static walls

Hi fokes I’m new to Corona. So far I’m loving it. I’ve been stuck at for little while trying to solve this issue.
I have a object that i call a “ball”. It’s position is controlled by the accelerometer. Basically i can move the ball around the screen or off the screen into oblivion. So naturally I put up static walls to stop the ball from leaving the play field. However the ball ignores the physics of the static walls / bodies and passes trough them with little hesitation.

The strange thing is if i programmatically move the ball position to a wall it reacts to the static walls i’ve put up.

Any suggestions would be very helpful

Thank you in advance!

Here is the code that is the main control.

[code]
display.setStatusBar (display.HiddenStatusBar)
local physics = require (“physics”)
physics.start()
physics.setGravity(0,0)
physics.setDrawMode(“hybrid”)

local screenW, screenH = display.contentWidth, display.contentHeight

local leftWall = display.newRect(0,0,1,screenH)
local rightWall = display.newRect(screenW ,0,1,screenH)
local ceiling = display.newRect(0,0,screenW,1)
local floor = display.newRect(0,screenH,screenW,1)
physics.addBody (leftWall, “static”, {density=5, bounce=0.1, friction=0.5})
physics.addBody (rightWall, “static”, { density=5, bounce=0.1, friction=0.5})
physics.addBody (ceiling, “static”, { density=5, bounce=0.1, friction=0.5})
physics.addBody (floor, “static”, { density=5, bounce=0.1, friction=0.5})

background = display.newImage (“background.png”)
ball = display.newImage (“ball.png”)
ball.x = 160
ball.y = 200
physics.addBody(ball, “dynamic”, {density=5, friction = 1.0, bounce=0.6})
–> Adds the ball and adds physics to the ball
–< momemtarily respects the walls then falls threw them.

local motionx = 0
local motiony = 0

local function onAccelerate( event )
motionx = 35 * event.xGravity
motiony = 35 * event.yGravity
end
Runtime:addEventListener (“accelerometer”, onAccelerate)

local function moveball (event)
ball.x = ball.x + motionx
ball.y = ball.y - motiony
end
Runtime:addEventListener(“enterFrame”, moveball)
[/code] [import]uid: 2165 topic_id: 9825 reply_id: 309825[/import]

First off, to answer your question, when you are setting the position of the ball using it’s x, y coordinates, it will ignore all physics constraints.

What you want to do is check in each frame event, whether ball is out of screen and if it is reset it back to edge.

Now, some tips
Ball should have radius :stuck_out_tongue:
[lua]physics.addBody(ball, “dynamic”, {density=5, friction = 1.0, bounce=0.6, radius=100}) – Ball should have radius :P[/lua]

Since accelerometer event will be triggered almost continuosly, I don’t understand why you need to have two function to move the ball. Unless there is some specific need game logic wise, you are better off using just one function.
[lua]local function onAccelerate( event )
ball.x = ball.x + 35 * event.xGravity
ball.y = ball.y - 35 * event.yGravity

–Following 2 conditions ensures it doesn’t go out of screen
if ball.x < ball.width*0.5 then
ball.x = ball.width*0.5
elseif ball.x > display.ContentWidth-ball.width*0.5 then
ball.x = display.ContentWidth-ball.width*0.5
end

if ball.y < ball.heigth*0.5 then
ball.y = ball.height*0.5
elseif ball.y > display.ContentHeight-ball.height*0.5 then
ball.y = display.ContentHeight-ball.height*0.5
end
end
Runtime:addEventListener (“accelerometer”, onAccelerate)[/lua] [import]uid: 48521 topic_id: 9825 reply_id: 35810[/import]

Thank you. I’ll give it a try soon. One more question for now.
I also whant to put obsticals in the ball’ path to the finish. Do I need to create constraints for those also? [import]uid: 2165 topic_id: 9825 reply_id: 35818[/import]

If you are programmatically updating the x, y coordinates of object it will ignore physics.

If you want it to react using physics, then you need to let it move using gravity or any other force you apply. hope you understand what I am saying. [import]uid: 48521 topic_id: 9825 reply_id: 35844[/import]

I do. But when I try something like this. Everything flies off the page.
physics.setGravity = ( ball.x = xGravity, ball.y = xGravity)
But I can see the issue the gravity is being set by the activity of the accelerometer. [import]uid: 2165 topic_id: 9825 reply_id: 35928[/import]