Acceleration

Hello all!

Started messing around with a new game in Codea, kind if stuck right in the beginning though…

I have a spaceship that I need to have a downward acceleration applied to it to act as gravity, but only the ship, nothing else on screen should be effected by it. Then when you touch the screen an upward acceleration is applied to the ship to counteract the downward acceleration and make the ship rise upward. Similar to the mechanics of the Helicopter Game flash game.

Is there a line of code I can use to add directional acceleration to an actor?

Thanks in advance!

Both of those examples are a little over my head, not sure what is doing what.

Since acceleration is a change in speed over time, I was messing around with changing the speed for each of the 60 frames per second. It seems to work decently for the initial falling, but when I add the touch to reverse the acceleration, I run into the problem of the acceleration not going back to falling when I release my finger. Will make more sense if I show you the code:

-- Gravity test

supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
function setup()
    rectMode(CENTER)
    heliX=WIDTH/6
    heliY=HEIGHT/2
    heliGrav=0
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    fill(255, 0, 0, 255)
    rect(heliX,heliY,128,64)
    heliGrav=heliGrav-.15
    heliY=heliY+heliGrav
    if CurrentTouch.state== BEGAN or MOVING then
        if CurrentTouch.x>0 and CurrentTouch.x<WIDTH and
        CurrentTouch.y>0 and CurrentTouch.y<HEIGHT then
            heliGrav=heliGrav+.3
            elseif CurrentTouch.state==ENDED then
            heliGrav=heliGrav+0
        end
    end
    
end

Avoid using CurrentTouch. Here is a working version, with comments explaining what I added:

-- Gravity test

supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
function setup()
    rectMode(CENTER)
    heliX = WIDTH/6
    heliY = HEIGHT/2
    heliGrav = 0
    heliAccel = -.15    -- New variable for acceleration speed
end

function draw()
    background(40, 40, 50)
    
    fill(255, 0, 0, 255)
    rect(heliX, heliY, 128, 64)
    
    heliGrav = heliGrav + heliAccel     -- Apply acceleration
    heliY = heliY + heliGrav
end

function touched(t)
    -- When you touch and when you let go, reverse the acceleration
    if t.state == BEGAN or t.state == ENDED then
        heliAccel = heliAccel * -1
    end
    -- So, when you touch it switches to going up, but when you let go it will start falling again
end

Awesome! I’m starting to understand the touch function now!

So where it says “if t.state BEGAN or t.state ENDED then”, what that is doing is switching it from positive to negative or vise versa each time either BEGAN or ENDED happens?

Also I notice that you completely removed the touch coordinate requirements. So if you don’t put in touch coordinate restrictions it uses the full screen by default?

Thanks much for the help and comments explaining it, greatly appreciate it!

Yes the if t.state == BEGAN or t.state == ENDED happens when you either place a finger on the screen or remove it from the screen, switching the acceleration each time.

Yes with no touch coordinates you can touch anywhere on the screen.

Have a look at the Gravity demo app provided with Codea

See the code I posted here.

http://codea.io/talk/discussion/5573/physics-applying-impulses#Item_4