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
    --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

function drawpoints(body)
    translate(body.x, body.y)
    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)

function draw()
    boxy = thebox.y
    background(0, 0, 0, 255)
    stroke(255, 0, 0, 255)
    fill(255, 0, 0, 255)   
    stroke(0, 0, 255, 255)
    fill(0, 0, 255, 255)
    stroke(0, 255, 0, 255)
    fill(0, 255, 0, 255)

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


  • SimeonSimeon Admin Mod
    Posts: 5,782

    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 ?

  • 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.