Physics Short Cutt

In the subject of aniamating things with gravity, velocity, friction, amd collision/rebound, is there things built into lua that can already do these things for me and do I have to re-write the laws of phyics?

It’s built in. Look at the physics example provided with Codea

Ok, I’ll have a look.

.@Goatboy76 It doesn’t take a lot of code to create realistic simulations with the physics engine.


supportedOrientations(PORTRAIT)
displayMode(FULLSCREEN)

function setup()
    c1 = physics.body(CIRCLE,0)
    c1.x = 400
    c1.y = 1000
    c1.type = STATIC    
    c2 = physics.body(CIRCLE,25)
    c2.x = 200
    c2.y = 900    
    c3 = physics.body(CIRCLE, 25)
    c3.x = 500
    c3.y = 950    
    c4 = physics.body(CIRCLE, 25)
    c4.x = 300
    c4.y = 850    
    p1=physics.body(POLYGON,vec2(-10,25),vec2(-10,-25),vec2(10,-25),vec2(10,25)) 
    p1.x=400
    p1.y=850            
    joint1 = physics.joint(REVOLUTE,c1,c2,c1.position)
    joint2 = physics.joint(REVOLUTE, c2, c3,vec2(c2.x,c2.y))   
    joint3 = physics.joint(REVOLUTE, c3, c4,vec2(c3.x,c3.y)) 
    joint4 = physics.joint(REVOLUTE, c4, p1,vec2(c4.x,c4.y))           
end

function draw()
    background(40, 40, 50)
    fill(255)
    strokeWidth(2)    
    line(c1.x,c1.y,c2.x,c2.y)
    line(c2.x,c2.y,c3.x,c3.y) 
    line(c3.x,c3.y,c4.x,c4.y) 
    line(c4.x,c4.y,p1.x,p1.y)           
    ellipse(c1.x,c1.y,8)        
    ellipse(c2.x,c2.y,8)     
    ellipse(c3.x,c3.y,8)    
    ellipse(c4.x,c4.y,8)    
    translate(p1.x,p1.y)
    rotate(p1.angle)
    points = p1.points
    for j = 1,#points do
        a = points[j]
        b = points[(j % #points)+1]
        line(a.x, a.y, b.x, b.y)    
    end
end

I’ve written a couple of tutorials on physics, starting here

http://coolcodea.wordpress.com/2013/03/21/applying-gravity/

A good time to ask:

Adding physics.gravity(Gravity) to the draw() function means that the objects respond to the iPad orientation - much more fun.

By why the draw()? Why not do it once in setup()? The latter doesn’t work.

That’s maybe because the draw function is called every frame whereas the setup function is called only once. So if you set the gravity in the setup function it will be set according to the orientation when the program starts and will not change afterwards(since its called only once). But if you do it in the draw function it will change according to the orientation when the draw function is called i.e once every frame

Thanks both of you. But @dave1701, thanks and all but I don’t know how to even began to use that code because it’s all forgein. Maybe @Ignatz guide will help me out.