Physics Noob


function setup()
    
    supportedOrientations(LANDSCAPE_ANY)
 --   displayMode(FULLSCREEN_NO_BUTTONS)
    
    top = physics.body(EDGE, vec2(0, HEIGHT), vec2(WIDTH, HEIGHT))
    top.type = STATIC
    top.restitution = 0
    bottom = physics.body(EDGE, vec2(0, 0), vec2(WIDTH, 0))
    bottom.type = STATIC
    bottom.restitution = 0
    
    shipPoints = {
    --Nose
    vec2(WIDTH/10,0),
    --Rect top
    vec2(WIDTH/20,HEIGHT/20),
    vec2(-WIDTH/20,HEIGHT/20),
    --Wing top
    vec2((-WIDTH/20)-(WIDTH/30),HEIGHT/10),
    vec2((-WIDTH/10)-(WIDTH/25),HEIGHT/10),
    vec2(-WIDTH/10,HEIGHT/20),
    --Butt
    vec2(-WIDTH/10,(HEIGHT/20)-(HEIGHT/50)),
    vec2((-WIDTH/10)-(WIDTH/50),(HEIGHT/20)-(HEIGHT/50)),
    vec2((-WIDTH/10)-(WIDTH/50),(-((HEIGHT/20)-(HEIGHT/50)))),
    vec2(-WIDTH/10,(-((HEIGHT/20)-(HEIGHT/50)))),
    --Bottom wing
    vec2(-WIDTH/10,(-(HEIGHT/20))),
    vec2((-WIDTH/10)-(WIDTH/25),(-(HEIGHT/10))),
    vec2((-WIDTH/20)-(WIDTH/30),(-(HEIGHT/10))),
    --Rect bottom
    vec2(-WIDTH/20,(-(HEIGHT/20))),
    vec2(WIDTH/20,(-(HEIGHT/20)))
    }
    
    ship = physics.body(POLYGON,unpack(shipPoints))
    ship.x = WIDTH/2
    ship.y = HEIGHT/2
    ship.type = DYNAMIC
    ship.interpolate = true
    ship.gravityScale = 0
    ship.restitution = 0
    
end

function draw()
 
    background(0, 0, 0, 255)
    
    if CurrentTouch.state == BEGAN or CurrentTouch.state == MOVING then
        ship:applyForce(vec2(0,800))
        if ship.angle <= 45 then
        ship.angle = ship.angle + 1
        end
    else
        ship:applyForce(vec2(0,-800))
        if ship.angle >= -45 then
        ship.angle = ship.angle - 1
        end
    end
    
    drawBody(ship)
    drawBody(top)
    drawBody(bottom)
    
end

function drawBody(body)
   
    pushStyle()
    pushMatrix()
    
    strokeWidth(5)
    stroke(255, 255, 255, 255)
    translate(body.x, body.y)
    rotate(body.angle)
    
    if body.shapeType == POLYGON then
        strokeWidth(3.0)
        local points = body.points
        for j = 1,#points do
            a = points[j]
            b = points[(j % #points)+1]
            line(a.x, a.y, b.x, b.y)
        end
        
    elseif body.shapeType == CHAIN or body.shapeType == EDGE then
        strokeWidth(3.0)
        local points = body.points
        for j = 1,#points-1 do
            a = points[j]
            b = points[j+1]
            line(a.x, a.y, b.x, b.y)
        end
        
    elseif body.shapeType == CIRCLE then
        strokeWidth(3.0)
        line(0,0,body.radius-3,0)
        ellipse(0,0,body.radius*2)
    end

    popMatrix()
    popStyle()
end

If you run this the “ship” moves backwards and doesn’t seem to land normally all the time. Can anyone explain this and offer a solution? Thanks!

Try replacing your draw function with this. Move your finger on the left side of the screen to rotate the ship left, on the right side to rotate the ship right.

function draw()
    background(0, 0, 0, 255)
    if CurrentTouch.state == BEGAN or CurrentTouch.state == MOVING then
        ship:applyForce(vec2(0,800))
        if CurrentTouch.x<WIDTH/2 then
            ship.angle = ship.angle + 1
        else
            ship.angle = ship.angle - 1
        end
    else
        ship:applyForce(vec2(0,-800))
    end
    drawBody(ship)
    drawBody(top)
    drawBody(bottom)
end

I can’t seem to copy the code in the first code block on this page, on my iPad - but I CAN copy the code in the second block. Is there a difference between the two code blocks here? I’d like to try running this program - guess I might have to wait until I get home to a browser on my Mac.

I’ve had much better luck with code hosted on GitHub - whether it be a full repository, or a single Gist file. Just sayin’!

Yes, I have no idea why, in 2016, copy-and-paste on iOS is still so utterly gimped. When Tim Cook says “I do all my work on an iPad!” I just think “you mustn’t ever have to select text then”.

Try selecting a word outside of the code block, and then slowly dragging up/ down into the block. You might have to then delete the extra word once you get it in to Codea.

The way I copy code is to go a few lines from the bottom of the code. I do a double tap and on the second tap I drag my finger down a little. I then drag one of the slider points all the way down. Once it reaches the bottom, the rest of the code to the top is usually selected. I then hit copy and do whatever with it. It takes a little getting used to, but once you get the hang of it, it’s not a problem. I also do everything on the iPad and don’t use a PC anymore. Well maybe just to backup my iPad with iTunes.

Thanks for the tips guys!

Hm, I think that you should leave it as it is, for it looks good when it lands and flys backwards. Let me explain. So, when it lands and moves backwards across the screen, it looks as if the camera kept on going, and the ship crashed. If you don’t want it to look like that, then check your ‘draw’ function. You need to interpret that the ship has hit the ground, and for it to stop moving. Hope this helped! :slight_smile: