How do I add a border so that the sprite will bounce off of them?


displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)

function setup()
    x=WIDTH/2
    y=HEIGHT/2
end

function draw()
    background(40,40,50)
    x=x+Gravity.x*10
    y=y+Gravity.y*10
    sprite("Planet Cute:Character Boy",x,y)
    end

If you want everything automatic, you have to create ‘body’ rectangle objects and draw the sprite at their location (bodies do not have sprite associated by default). Look at the Physics Lab project (included in codea). To be more specific i’ve extracted that from the project:

function createBox(x,y,w,h)
    -- polygons are defined by a series of points in counter-clockwise order
    local box = physics.body(POLYGON, vec2(-w/2,h/2), vec2(-w/2,-h/2), vec2(w/2,-h/2), vec2(w/2,h/2))
    box.interpolate = true
    box.x = x
    box.y = y
    box.restitutions = 0.25
    box.sleepingAllowed = false
    debugDraw:addBody(box)
    return box
end

function createGround()
    local ground = physics.body(POLYGON, vec2(0,20), vec2(0,0), vec2(WIDTH,0), vec2(WIDTH,20))
    ground.type = STATIC
    debugDraw:addBody(ground)
    return ground
end

You should read some of the tutorials about Codea first, see the wiki link above. There are several ways of doing this.

Thanks! @Jmv38

@Willkassens Here’s a small example.


displayMode(FULLSCREEN)   
 
function setup()
    -- set up the edge lines
    line1 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
    line2 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
    line3 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
    line4 = physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))  

    -- setup physics object
    b1=physics.body(CIRCLE,20) 
    b1.x=WIDTH/2
    b1.y=HEIGHT/2
    b1.restitution=1
    b1.linearVelocity=vec2(400,400)
    b1.gravityScale=0
    b1.friction=0
end

function draw()
    background(50, 50, 50) 
    fill(255)
    sprite("Planet Cute:Tree Short",b1.x,b1.y,40)
    
    -- draw edge lines
    stroke(255) 
    strokeWidth(2)
    line(0,0,0,HEIGHT)  
    line(WIDTH,0,WIDTH,HEIGHT)
    line(0,0,WIDTH,0)
    line(0,HEIGHT,WIDTH,HEIGHT)
end

Thank you for the example. You’ve just answered some if my questions! @dave1707

You can also make a custom physics engine, but the built-in one is much easier to use. It’s a bit slow right now, but it will be fixed in the next Codea update, and will have LiquidFun integrated too, for liquid simulations!

@SkyTheCoder the built in is much more efficient than a custom built, box2D is optimised as well. The integration may not be the best though. Liquid fun will be amazing! And a great addition to my game :stuck_out_tongue: