Working with Gravity

Can anyone point me to some code or documentation that can get me going with Gravity?

I’ve tried working backward from Dungeon Roller but I can’t find the exact piece of code that lets a sprite move around via tilting the iPad. The in-app documentation is pretty sparse too.

I’m new to lua and couldn’t find anything via a google search either.

Thanks!
Nick

Hi @nsbarr

Here’s the relevant code from Dungeon Roller with some explanation

-- Assume we have some variable, self.position, which represents
-- the position of our object

self.position = vec2( 0, 0 )

-- Here we figure out the direction to move given the current gravity
-- The "+ vec2(0, 0.6)" simply offsets our Y-component
-- we do this to allow the game to be played with the iPad slightly tilted
-- you can think of it as a calibration amount

moveVec = vec2(Gravity.x, Gravity.y) + vec2(0,0.6)

-- Update our position using moveVec
-- We multiply by 20 to increase the strength of the gravity vector

self.position = self.position + moveVec * 20

Here it is put into an example project (Note: untested, may not work)

function setup()
    -- Start in the center of the screen
    position = vec2( WIDTH/2, HEIGHT/2 )
    
    -- Create two parameters to play with
    parameter( "yCalibration", 0, 2.0, 0.6 )
    parameter( "tiltStrength", 1, 40, 10 )
end

function draw()
    background(0)
    fill(255,0,0)
    
    moveVec = vec2(Gravity.x, Gravity.y) + vec2( 0, yCalibration )
    
    position = position + moveVec * tiltStrength
    
    ellipse( position.x, position.y, 100 )
end

You can experiment with this by changing the yCalibration and tiltStrength parameters to see how it effects the tilt controls.

Thanks Simeon! This is really helpful.

Unfortunately when I put this code into my project, it crashes and quits the app when I try to run it. I actually had the same problem when I was working backward from Dungeon Roller, so there may be a specific bug here.

I made sure I had the latest version of the app and also tried deleting/reinstalling it, with no luck.

Happy to provide any more useful information!

Thanks,
N

Here’s a silly little program I wrote for figuring out how gravity works.

-- Use this function to perform your initial setup
function setup()
    x = vec2(WIDTH/2,HEIGHT/2)
    v = vec2(0,0)
    c = color(255,0,0,255)
    r = 30
    g = vec2(0,0)
    h = 0.1
    gx = 0
    gy = 0
    gz = 0
    gl = 0
    watch("gx")
    watch("gy")
    watch("gz")
    watch("gl")
end

-- This function gets called once every frame
function draw()
    background(0,0,0,255)
    fill(c)
    ellipse(x.x,x.y,r)
    g = 2*vec2(Gravity.x,Gravity.y)
    v = v + h * ( g - 0.001 * v:len() * v )
    x = x + h * v
    if x.x < r or x.x > WIDTH - r then
        v.x = - v.x
    end
    if x.y < r or x.y > HEIGHT - r then
        v.y = - v.y
    end
    gx = Gravity.x
    gy = Gravity.y
    gz = Gravity.z
    gl = math.sqrt(gx*gx + gy*gy + gz*gz)
end

It’s a ball that rolls around the screen as you tip the iPad. There’s some built-in “friction” and it bounces off the sides. It also displays the components and length of the gravity vector (the latter being a little tedious; it was mainly because I see reports that the iPad can measure the strength of the gravitational field, which seems absurd.)

Nice, thanks Andrew! Interestingly this snippet didn’t crash the app…

Sorry @nsbarr I think that code I posted exhibits the vec2 bug in the current version of Codea. The new version awaiting approval fixes the bug.

Cool, looking forward to the fix. In the meantime will continue to play with the gravity stuff, thanks for all the help!