Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Ball keeps falling through walls

edited June 2014 Posts: 16

I am able to create a ball that falls due to gravity and bounces. But I wanted to control the ball using the tilt of the screen but when I do this the ball just falls through the walls. Any help would be greatly appreciated. Here is my code:

function setup()

supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
noFill()
noSmooth()
noStroke()
pushStyle()

ball = physics.body(CIRCLE, 10)
ball.x = 500
ball.y = 500
ball.gravityScale = 1
ball.restitution = 0.5

bottom = physics.body(EDGE, vec2(0, 0), vec2(WIDTH,0) )
left = physics.body(EDGE, vec2(0, 0), vec2(0, HEIGHT))
right = physics.body(EDGE, vec2(WIDTH, 0), vec2(WIDTH, HEIGHT))
top = physics.body(EDGE, vec2(0, HEIGHT), vec2(WIDTH, HEIGHT))

end

-- This function gets called once every frame
function draw()
-- Local varialble
background(0, 62, 255, 255)

ball.x = ball.x + Gravity.x*10
ball.y = ball.y + Gravity.y*10

fill(43, 255, 0, 255)

stroke(0, 255, 89, 255)
strokeWidth(10)
line(0, 0, WIDTH, 0)
line(0, 0, 0, HEIGHT)
line(WIDTH, 0, WIDTH, HEIGHT)
line(0, HEIGHT, WIDTH, HEIGHT )

end

Tagged:

• Posts: 455

The problem is you are moving the balls not letting physics do it, so it will go through walls, because it's not physics.

You can do any of:

ball.linearVelocity = Gravity <- the ball will move at a fixed speed but should hit walls.

ball:applyForce(Gravity) <- this will push the ball in the direction of gravity same as below

physics.gravity( Gravity) <- this will make the physics gravity match the device angle

Exact syntax may vary, didn't do it on a real device.

• edited June 2014 Posts: 425

physics.gravity(GRAVITY)

Edit you already got that one

• edited June 2014 Posts: 425

@spacemonkey your first and second solutions may not work as they require a 2d vector bu Gravity is 3d. You could use vec2(Gravity.x,Gravity.y) but the third one will.

Edit: you mentioned that at the end

• Posts: 16