Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

jump only goes so high?

edited January 2015 in Questions Posts: 100

I hunk it has something to do with how I'm resetting the inAir variable (collide function)

-- 2D Game

function setup()
    physics.continuous=true
    diam = 60
    w1 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
    w2 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
    w3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
    w4 = physics.body(EDGE, vec2(0, HEIGHT), vec2(WIDTH, HEIGHT))
    ball = physics.body(CIRCLE, 30)
    ball.gravityScale = 1
    ball.restitution = .8
    ball.x = 50
    ball.y = 50
    direction =  "none"
    up = false
    inAir = false
end

function draw()
    local y = 0
    if up == true then
        up = false
        y = 5000
        inAir = true
    end
    if direction == "LEFT" then
        ball.linearVelocity = vec2(-75, y)
    elseif direction == "RIGHT" then
        ball.linearVelocity = vec2(75, y)
    end
    --Set up color stuff
    background(0, 0, 0)

    --Set up ball stuff
    fill(255,0,0)
    strokeWidth(0)


    --Draw the ball using x,y position of physics object
    ellipse(ball.x,ball.y,diam)

    --Set up wall stuff
    stroke(255, 255, 255, 255)
    strokeWidth(2)

    --Draw walls
    line(0, 0, WIDTH, 0)
    line(0, 0, 0, HEIGHT)
    line(WIDTH, 0, WIDTH, HEIGHT)
    line(0, HEIGHT, WIDTH, HEIGHT)
end

function touched(t)
    if t.state == BEGAN then
        if t.x < WIDTH/2 then
             direction = "LEFT"
        else
            direction = "RIGHT"
        end
        if t.y > HEIGHT/2 and inAir == false then
            up = true
        end
    else
        direction = "none"
    end
end

function collide(contact)
    if contact == BEGAN and contact.bodyA == ball and contact.bodyB == w1 then
        inAir = false
    end
end

See latest post for new question.

Comments

  • Posts: 136

    sometimes bodyA and bodyB can be hard to use, I might be wrong but when I use them, any EDGE is always bodyA, try switching the ball to bodyB and the floor to bodyA.

  • dave1707dave1707 Mod
    Posts: 7,520

    @warspyking In the function collide, you need " if contact.state==BEGAN" . Also, bodyA and bodyB aren't always ball or w1. You need to check them both ways, You can't be sure which way they'll be. Normally the first one defined will be bodyA and the second bodyB but that can change.


    if contact.bodyA==ball and contact.bodyB==w1 or contact.bodyA==w1 and contact.bodyB==ball then
  • Still don't work properly:

    -- 2D Game
    
    function setup()
        physics.continuous=true
        diam = 60
        w1 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
        w2 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
        w3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
        w4 = physics.body(EDGE, vec2(0, HEIGHT), vec2(WIDTH, HEIGHT))
        ball = physics.body(CIRCLE, 30)
        ball.gravityScale = 1
        ball.restitution = .8
        ball.x = 50
        ball.y = 50
        direction =  "none"
        up = false
        inAir = false
    end
    
    function draw()
        local y = 0
        if up == true then
            up = false
            y = 5000
            inAir = true
        end
        if direction == "LEFT" then
            ball.linearVelocity = vec2(-75, y)
        elseif direction == "RIGHT" then
            ball.linearVelocity = vec2(75, y)
        end
        --Set up color stuff
        background(0, 0, 0)
    
        --Set up ball stuff
        fill(255,0,0)
        strokeWidth(0)
    
    
        --Draw the ball using x,y position of physics object
        ellipse(ball.x,ball.y,diam)
    
        --Set up wall stuff
        stroke(255, 255, 255, 255)
        strokeWidth(2)
    
        --Draw walls
        line(0, 0, WIDTH, 0)
        line(0, 0, 0, HEIGHT)
        line(WIDTH, 0, WIDTH, HEIGHT)
        line(0, HEIGHT, WIDTH, HEIGHT)
    end
    
    function touched(t)
        if t.state == BEGAN then
            if t.x < WIDTH/2 then
                 direction = "LEFT"
            else
                direction = "RIGHT"
            end
            if t.y > HEIGHT/2 and inAir == false then
                up = true
            end
        else
            direction = "none"
        end
    end
    
    function collide(contact)
        if contact.state == BEGAN and contact.bodyA == ball and contact.bodyB == w1 or contact.bodyB == w1 and contact.bodyB == ball then
            inAir = false
        end
    end
    
  • IgnatzIgnatz Mod
    Posts: 5,396

    I think I would put brackets in to make sure Codea understands what you want

    if contact.state == BEGAN and ((contact.bodyA == ball and contact.bodyB == w1) or (contact.bodyB == w1 and contact.bodyB == ball)) then
    
  • dave1707dave1707 Mod
    edited January 2015 Posts: 7,520

    You have contact.bodyB too many times in your if statements. Try the code below.

    function collide(contact)
        if contact.state == BEGAN then
            if contact.bodyA == ball and contact.bodyB == w1 or 
                    contact.bodyA == w1 and contact.bodyB == ball then
                inAir = false
            end            
        end
    end
    
  • @dave 1707 thank you :D

  • Well no matter what I set y to:

    y = 25000
    

    It won't go higher.

  • dave1707dave1707 Mod
    Posts: 7,520

    remove the local y=0 in draw.

  • @dave1707 Why on earth would I do that? It's a default value for if I'm in air then it will use 0 as the y.

  • IgnatzIgnatz Mod
    Posts: 5,396

    @warspyking - think about it for a second.

    When you touch, and set up=true, what happens then in draw?

    First time,
    y is set to 0
    up=true, so it is set to false and y=5000

    Second time draw runs,
    y is set to 0
    up=false, so y stays at 0 !!!!!

    and that is your problem, right there. So dave was right. Next time, just work it through slowly, step by step, and you'll see it more easily. ;)

  • @Ig So in other words, I gotta reset up to true?

  • IgnatzIgnatz Mod
    Posts: 5,396

    you figure out what you want to do, I can't tell you

Sign In or Register to comment.