Physics.

Hey! I can’t find any links for physics help, so I need some please. How can I make a box, or object fall or obey physics. I now know how to make classes, how to draw objects from classes and much more! If I can get some help, I will be on my way to making a new game! Please any help would be great.

Physics example and in app reference are a good place to start.

Here’s a good physics body example:


--# Main
function setup()
    displayMode(FULLSCREEN)
    w1 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0)) -- create a solid line from 0,0 to WIDTH,0
    w2 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT*3))
    w3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT*3))
    w4 = physics.body(EDGE,vec2(0,HEIGHT*2),vec2(WIDTH,HEIGHT*3))

    ball_diam=60 --start by setting the diameter
    --now create the physics object that behaves like a
    --real ball, it will tell us where to draw our ball
    --think of it as a GPS navigator that guides us
    -- CREATE BODY
    --physics bodies use radius not diameter
    p_ball = physics.body(CIRCLE,ball_diam/2)
    -- APPLY GRAVITY
    p_ball.gravityScale = 2
    -- next the bounciness - set lower for less
    p_ball.restitution = 0.8 -- 0.3 is a very good number for restitution
    --Now give it some velocity (velocity is in pixels
    --per second, ie per 60 redraws)
    p_ball.linearVelocity=vec2(math.random(600),math.random(600))
    --POSITION it randomly on the screen
    p_ball.x = math.random(60,250)
    p_ball.y = math.random(400,600)
end

function draw()
    background(200,200,200,255)
    fill(0,255,0,40)
    rect(0,0,WIDTH,200)
    fill(0,0,255,40)
    rect(0,HEIGHT-200,WIDTH,200)
    fill(255,0,0,40)
    rect(0,0,200,HEIGHT)
    fill(127.5,127.5,0,40)
    rect(WIDTH-200,0,200,HEIGHT)
    --draw the ball --
    pushStyle() --see previous tutorials for explanation
    fill(255,0,0,255)
    --draw the ball using x,y position of physics object
    ellipse(p_ball.x,p_ball.y,ball_diam)
    
    popStyle()
end

function touched(t)
    if t.state == BEGAN then
        if t.y > HEIGHT-200 then
            p_ball.linearVelocity = vec2(p_ball.linearVelocity.x,p_ball.linearVelocity.y + math.random(500,700))
        end
        if t.y < 200 then
            p_ball.linearVelocity = vec2(p_ball.linearVelocity.x,p_ball.linearVelocity.y + math.random(-700,-500))
        end
        if t.x < 200 then
            p_ball.linearVelocity = vec2(p_ball.linearVelocity.x + math.random(-700,-500),p_ball.linearVelocity.y)
        end
        if t.x > WIDTH-200 then
            p_ball.linearVelocity = vec2(p_ball.linearVelocity.x + math.random(500,700),p_ball.linearVelocity.y)
        end
    end
end

Credit goes to @ignatz and his awesome ebook about physics bodies. (thats where I learned physics bodies).

@LL_Phoenix456 There has to be a lot of physics examples in this forum. I myself have put a lot here. Try doing a forum search on physics.body .

Thanks!

Is a vector a point of drawing? For example, to draw a box, you need 4 vec2’s, and if you apply a physics.body to that, it will make the box have physics?

That’s what the reference is for

https://codea.io/reference/Physics.html#physics.body

@Kolosso @Ignatz YOU ARE THE BEST!!! I KNIW PHYSICS! THANKYOU SO FREAKING MUCH! I LITERALLY LOVE YOU GUYS!