Howdy, Stranger!

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

Physics help

edited January 2012 in Code Sharing Posts: 622

Possibly I'm just thick, but I didn't really understand the fundamentals of gravity, bodies, and drawing them until I got this done.

function setup()
    thebox = physics.body(POLYGON, vec2(0,3), vec2(0,10), vec2(10,10), vec2(10,3))
    thebox.x = 20
    thebox.y = 100
    floor = physics.body(POLYGON, vec2(1,20), vec2(1,0), vec2(WIDTH,0), vec2(WIDTH,20))
    floor.type = STATIC  
    --floor.x = 0 -- 0 is default
    --floor.y = 0 -- 0 is default
    watch("boxy") 
    --stops at 17.59
    --top of floor is 0 + 20
    --bottom of thebox is y + 3
    -- 20 - 3 = 17
    --there is no distance between 
    --the top of the floor and 
    --the bottom of the box
    --when y = 17
    --.59 the leftover partial distance when y was < 1
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

function draw()
    scale(8)
    boxy = thebox.y
    noSmooth()
    background(0, 0, 0, 255)
    strokeWidth(1)
    stroke(255, 0, 0, 255)
    fill(255, 0, 0, 255)   
    drawpoints(thebox)
    stroke(0, 0, 255, 255)
    fill(0, 0, 255, 255)
    drawpoints(floor)
    stroke(0, 255, 0, 255)
    fill(0, 255, 0, 255)
    ellipse(thebox.x,thebox.y,3,3) 
end

The in app example is great for showing what can be done. This may help with basics/minimum to do.

Comments

  • SimeonSimeon Admin Mod
    Posts: 5,013

    That's a great little example. You can change the bounciness of the box by doing

    thebox.restitution = 0.5 --or other value
    
  • what is bounciness of the box, what pice of code ?
    Thanks

  • Posts: 447

    it's how much they bounce. After you created a body you can set this parameter by setting box.restitution as Simeon showed

  • I just wanted to know if the resitution is used like this, because of this example code, that i know what values i have to define :)

    the startposition of the Body is saved, then the body moves down until it hits sth, then the endposition is saved, then the startposition minus the endposition multiplicated by the resitution is the length of the Way the body is going up again.

    Is that right?

  • Posts: 118

    That is how I always thought it was defined. The fraction of kinetic energy left after the bounce.

Sign In or Register to comment.