Custom physics engine doubt

Is there a way to make a floor without using Box2D, try this program I can set the vel of the ball before it hits the floor but after it touches down, I can’t. I know why, but I’m not able to think of an alternative to avoid this problem.

function setup()
    x = WIDTH/2
    y = HEIGHT/2
    r = 50
    vel = 0
    parameter.action("vel", function() vel = 5 end)
end

function draw()
    background(255, 255, 255, 255)
    ellipse(x, y, r*2)
    
    vel = vel - .1
    if y <= r then
        vel = 0
    end
    y = y + vel
end

Try:

if y <=r and vel < 0 then
    vel = 0
end

This sets the velocity to 0 only if the ball is moving downwards.

Thanks Andrew Stacey, that worked out.

@Saurabh i changed your code a bit so that it doesn’t suddenly sit 1/3 in the floor or so

function setup()
    x = WIDTH/2
    y = HEIGHT/2
    r = 50
    vel = 0
    parameter.action("vel", function() vel = 5 end)
    ground = 0
end

function draw()
    background(255, 255, 255, 255)
    ellipse(x, y, r*2)

    vel = vel - .1
    if y <= ground + r and vel < 0 then
        vel = 0
    end
    if y + vel - r >= ground then
        y = y + vel
    else
        y = ground + r
    end
end

ground = the floor, if a square would be on the floor… and it would be 100 in height, then the ground var should be 100 for example

tho it’s your code so do with it what you want :wink:

notice this isn’t the best way to do it, since it just stops, and physics (if you want to keep them as real as possible) would bounce

Thank Stevon8er though I figured out one more way some time after posting the discussion.

What you could do is add one more force (the Normal force) besides gravity when it collides.

Though your code makes it more clean of a contact when it sinks into the body.

ofcourse, the normal force, shame on me that i didn’t think of that, i have my physics notes next to me… since i have a test about a part of it in 20min, and i didn’t think of that :o
xD

@Saurabh - I did some posts on physics including collisions and normals recently…

Yeah @Ignatz I glanced over them but I couldn’t find standing on bodies, I found reflection, wind and those thing but couldn’t find what I was looking for. Or did I miss something??

Thanks!!

It covers falling and hitting a rigid surface, eg floor, if that what you want

Yes that’s what I was looking for, maybe I skipped it. I’ll go through them once again.