Top Down Physics

This isn’t super amazing just an attempt to show an alternative.

Just because physics is 2D and has gravity doesn’t mean it’s only useful for sidescrollers. By using physics in a top down mode you loose gravity (have to turn it off) and friction (at least against the ground, not other objects). That still leaves many features like collision and acceleration.

This example just contains two rectangular physics bodies (cars aka autos) and a circle for an accelerator. Notice when the p(player)car hits the o(other) car it’s speed reduces by half.

Now the sidescroller car in the included example is cooler since it uses an engine and the tires actually rotate and move via friction.

But even just having built in acceleration and collision of objects built into Codea is great.

--physics car
supportedOrientations(PORTRAIT)

function setup()
    gr = (1+math.sqrt(5))/2 --golden rectangle
    displayMode(FULLSCREEN)
    physics.gravity(0,0) --no gravity since this is top down
    s = 25 --car size
    pcar = physics.body(POLYGON, vec2(0,s*gr), vec2(s,s*gr), vec2(s,0), vec2(0,0))
    pcar.x = WIDTH/2
    pcar.y = 100
    gas = vec2(WIDTH -100,100)
    og = false --on gas
    ogt = nil --on gas touch id
    ocar = physics.body(POLYGON, vec2(0,s*gr), vec2(s,s*gr), vec2(s,0), vec2(0,0))
    ocar.x = WIDTH/2
    ocar.y = 300
end

function draw()
    background(40, 40, 50,255)
    noSmooth()
    strokeWidth(1)
    stroke(255, 0, 0, 255)
    fill(255, 0, 0, 255)   
    drawpoints(pcar)
    drawpoints(ocar)
    if og then 
        text("gas",300,300) 
        pcar:applyForce(vec2(0,10))
    end
    text(pcar.linearVelocity.y,100,200) 
    fill(0, 255, 35, 255) --gas button color
    ellipse(gas.x,gas.y,100,100)
end

function touched(touch)
    if og then
        if touch.id == ogt then
            if gas:dist(vec2(touch.x,touch.y)) > 50 --slipped off gas
            or touch.state == ENDED 
            or touch.state == CANCELLED then 
                og = false
            end
        end
    elseif gas:dist(vec2(touch.x,touch.y)) < 50 
    and (touch.state == BEGAN or touch.state == MOVING) then
        og = true
        ogt = touch.id
    end
end

function drawpoints(body)
    pushMatrix()
    translate(body.x, body.y)
    rotate(body.angle)
    local points = body.points
    local a, b
    for j = 1,#points do
        a = points[j]
        b = points[(j % #points)+1]
        line(a.x, a.y, b.x, b.y)
    end
    popMatrix()
end

I was thinking of adding a shoot button and showing bullet collision as well but just like the included example putting too much in one example confuses things.

Everything you need for pinball.