Howdy, Stranger!

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

In this Discussion

Hitting Physics Body With Current Touch

Hey everyone. I wrote this demo code to better demonstrate my challenge. I'm trying to "hit" a physics body with my finger. Not slide it by dragging it. So ideally it would simulate a tennis racquet hitting a ball. The code is simply a container where you could hit the ball using the drawBody function pulled from Codea. Thanks!

function setup()
    supportedOrientations(PORTRAIT)
    j = physics.body(CIRCLE,WIDTH/20)
    j.type = DYNAMIC
    j.x = WIDTH/2
    j.y = HEIGHT/2
    left = physics.body(EDGE,vec2(0,HEIGHT),vec2(0,0))
    left.type = STATIC
    center = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
    center.type = STATIC
    right = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
    right.type = STATIC
end

function draw()
    background(0, 0, 0, 255)
    drawBody(left)
    drawBody(center)
    drawBody(right)
    drawBody(j)
end

function drawBody(body)
    pushStyle()
    pushMatrix()
        stroke(255, 255, 255, 255)
        strokeWidth(3.0)
        translate(body.x, body.y)
        if body.shapeType == POLYGON then
            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
            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
            fill(255, 255, 255, 255)
            ellipse(0,0,body.radius*2)
        end
    popMatrix()
    popStyle()
end

Comments

  • dave1707dave1707 Mod
    edited November 2016 Posts: 8,464

    @bhob12 Here's your code where I added a tennis recquet that you move with your finger. You can move the racquet by sliding your finger around the screen, or by flicking your finger in the direction you want the racquet to move. You'll have to adjust the linearVelocity statement or the j.restitution value for your needs.

    displayMode(FULLSCREEN)
    
    function setup()
        supportedOrientations(PORTRAIT)
        j = physics.body(CIRCLE,20)
        j.type = DYNAMIC
        j.x = WIDTH/2
        j.y = HEIGHT/2
        j.restitution=.8
        left = physics.body(EDGE,vec2(0,HEIGHT),vec2(0,0))
        left.type = STATIC
        center = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
        center.type = STATIC
        right = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
        right.type = STATIC
    
        m=physics.body(EDGE,vec2(0,0),vec2(100,0))
        m.x=WIDTH/2
        m.y=200
        m.type=KINEMATIC
    end
    
    function draw()
        background(0, 0, 0, 255)
        drawBody(left)
        drawBody(center)
        drawBody(right)
        drawBody(j)
    
        stroke(255)
        strokeWidth(5)
        line(m.x,m.y,m.x+100,m.y)
    end
    
    function drawBody(body)
        pushStyle()
        pushMatrix()
            stroke(255, 255, 255, 255)
            strokeWidth(3.0)
            translate(body.x, body.y)
            if body.shapeType == POLYGON then
                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
                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
                fill(255, 255, 255, 255)
                ellipse(0,0,body.radius*2)
            end
        popMatrix()
        popStyle()
    end
    
    function touched(t)
        if t.state==MOVING then
            m.linearVelocity=vec2(t.deltaX*50,t.deltaY*50)
        end    
    end
    
Sign In or Register to comment.