Understanding the Gravity example

Hi I’m very new to lua and game development.
I wonder if someone could help me with understanding what is happening in the gravity example code
I’m trying to write some code that moves a sprite about on the screen depending on how the user moves the iPad
The same as the way in which dungeon roller moves the hero about the screen.

I,d appreciate if someone could paste and comment the gravity example code so that it explains what each line is doing
I have tried looking elsewhere to try and understand each command such as popmatrix() etc but could not find an explanation.

I’m really enjoying getting to know lua but Ido think it would help for us noobies if the example codes where commented in much more detail. Thanks in advance

Here are the guts of the gravity example:

pushMatrix()

    translate(WIDTH/2, HEIGHT/2)
    grav = vec2(Gravity.x * 300, Gravity.y * 300)
    line(0, 0, grav.x, grav.y)

    -- Arrowhead
    down = vec2(1, 0)
    orient = down:angleBetween(grav)

    pushMatrix()
    resetMatrix()

    translate(WIDTH/2,HEIGHT/2)
    translate(grav.x,grav.y)
    rotate(math.deg(orient))

    line(0, 0, -25, -20)
    line(0, 0, -25,  20)    

    popMatrix()

The first pushmatrix() saves the current state of translate/rotate/scale so we can come back to it later with popmatrix() It’s not necessary in the demo, but it’s good practice if you expect your code to be reused - it lets you clean up after yourself, and by writing code with transform/rotate/scale you can avoid what can turn into some very nasty math.

A note: The complexity here comes from needing to be able to calculate the rotation for the arrow head - if you just wanted to draw a line, the x and y from gravity is enough.

Anyway - the next line saves gravity.x and y as a vector. The line after draws the shaft of the arrow.

To draw the arrowhead, we will draw a rotated V shape. To figure out how much to rotate, we need to calculate the angle between our gravity vector and a fixed vector we call down. These two lines set up down, and then figure out the angle between down and grav.

Next, the transform matrix is reset to the default (not necessary here, but again good practice), and the arrowhead is drawn by moving first to the center, then to the other end of the line, then rotating. The actual v is drawn, then the matrix is popped to put us back where we were.

That’s about it - simple gravity is looking at gravity, establishing the angle and rotating, the proceeding assuming gravity is just straight down.

Thanks very much for you explanation it now makes much more sense.
However using this code and trying to adapt it to having a sprite rolling about the screen smoothly depending on the angle of the iPad is still beating me. I’m trying to achieve kind of like the game labyrinth where a boll rolls freely around the screen and u have to guide it gingerly around maze but I can’t get this to work could you help ?. I have tried only using the translate to get the initial position on the centre of the screen and then adding + or - 1 to the gravity x and y positions depending on whether it’s negative or positive but this isn’t giving m the desired affect. If you can give me a starter for 10 it would be appreciated

I’d skip the translates and just add the gravity to the sprites position.

Translate is good for rotating (and maybe some other things I can’t remember), you can move without it.

You may have to multiply the gravity to get more movement from less tilt and/or all gravity to a speed x y to simulate inertia.

Speedx = Speedx + gravity.x (times something maybe)
Same for Speedy

SpriteX = SpriteX + Speedx

Sprite(“blah”,SpriteX,SpriteY)

Once that works then maybe add spin for effect with translates/rotates or some animation.

@Eedoboy… You may also want to look at @ruilov’s ‘Pigs in Clover’ game code for tips.

http://twolivesleft.com/Codea/Talk/discussion/20/my-first-app-pigs-in-clover

Thanks ipda41001 and blanchot that’s exactly what I’m after.

However I need more help (sorry)

I’m playing around with ruilovs code and trying to assign the various variables in his code for the balls to 36 balls I’d like if I can to assign the variables to an array that’s 6 by 6.

My code is below. The first print appears to show me what id expect however if i try to reference one of the keys within the array i.e x i get an error Can you tell me why the second print is returning an error.

I know I could copy ruilovs example and just create 36 rows in the table and reference it as he does but I’d much prefer it in a grid as this fits in with other code that I’m clumsily plundering my way through. Thanks for you patience

myxplaces={124,224,324,424,524,624}

myyplaces={128,210,294,376,456,536}

balls={}

for c = 1,6 do

balls[c] = {}

for d = 1,6 do

    balls[c][d]="x="..myxplaces[c]..",y="..myyplaces[d].."speedX=0,speedY=0,radius=33"

end

end

for c = 1,6 do

for d = 1,6 do

    print("balls "..c.." "..d.."= "..balls[c][d])

   print("balls "..c.." "..d.."x= "..balls[c][d].x) -- why does this not work

end

end

Not sure if this helps

function setup()
    print("Hello World!")
    myxplaces={124,224,324,424,524,624}
myyplaces={128,210,294,376,456,536}
balls={}
for c = 1,6 do
   balls[c] = {}
    for d = 1,6 do
        --balls[c][d]="x="..myxplaces[c]..",y="..myyplaces[d]..",speedX=0,speedY=0,radius=33"
        balls[c][d]={x=myxplaces[c],y=myyplaces[d],speedX=0,speedY=0,radius=33}
    end
end 
for c = 1,6 do
    for d = 1,6 do
        --print("balls "..c.." "..d.."= "..balls[c][d])
        print("balls "..c.." "..d.."= ",balls[c][d])
       print("balls "..c.." "..d.."x= "..balls[c][d].x) -- why does this not work
    end
end 
end