floating ball in water

I am trying to create some kind of water level were the ball floats upwards and I tried making the gravity alternate when it hits the air or the water, it doesn't work like I wanted to, does anyone have another way to do this, here is the code for the gravity changes: (copy paste freindly)

local physics = require ("physics")
physics.start(true)
--physics.setDrawMode "hybrid"
physics.setGravity(0, -5)

local square = display.newRect( 30, 41, 250, 435 )
square:setFillColor( 255,255,255 )
physics.addBody( square, "static", {friction =0.1, isSensor = true, bounce = 0.1})
local square1 = display.newRect( 0, 2, 400, 38 )
square:setFillColor( 255,0,0 )
physics.addBody( square1, "static", {friction =0.1, isSensor = true, bounce = 0.1})

local ball = display.newCircle( 0, 0, 25)
ball:setFillColor(255, 255, 0)
ball.x = display.contentWidth / 2
ball.y = display.contentHeight / 2
physics.addBody ( ball, {bounce = 0, friction = 1, density = 1 })

local function water( self, event )

physics.setGravity (0, -10.8)

end

square.collision = water
square:addEventListener( "collision", square)

local function air( self, event )
physics.setGravity (0, 10 )
end

square1.collision = air
square1:addEventListener( "collision", square1)

views:1934 update:2011/9/18 10:09:28
corona forums © 2003-2011