Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bouncing balls never stay calm on the ground

I try to create a very simple physics engine for my study (processing used for a interactive installation).

The target is to have a ground covered with balls that you can throw around with gestures (based on Kinect information).

Therefor I need to do some basic physic simulation like bouncing and thats what I started with. So there are just balls falling down and bouncing. I simulated the air resistance with a simple 0.995f multiplication on the speed if the ball moves up. Works nice and looks realistic. The main problem is, that the balls never stay calm on the ground. Instead they start to tremble on the ground. That means there is a movement of 1 or 2 pixels up and down.

How can I prevent that without implementing some "borders" on which I set the position directly to the bottom and the speed to zero?

My applet:

public class BubblePhysicApplet extends PApplet {
    public static int width = 640;
    public static int height = 480;
    long lastTime = -1;
    Bubble[] mBubbles = new Bubble[10];
    Random mRandom = new Random();

    public void setup() {
//      size(width, height, OPENGL);
        size(width, height, P2D);
        for (int i = 0; i < mBubbles.length; i++) {
            mBubbles[i] = new Bubble(mRandom.nextInt(width), mRandom.nextInt(height), 50);
        }
        lastTime = System.currentTimeMillis();
    }

    public void draw() {
        background(0);
        long tmp = System.currentTimeMillis();
        long elapsed = tmp - lastTime;
        for (Bubble bubble : mBubbles) {
            bubble.animate(elapsed);
            bubble.draw(this);
        }
        lastTime = System.currentTimeMillis();
    }
}

The ball/bubble:

public class Bubble {
    float mX;
    float mY;
    float mSize;
    float mSpeedX = 0;
    float mSpeedY = 0;

    public Bubble(int x, int y, int size) {
        mX = x;
        mY = y;
        mSize = size;
    }

    public void draw(PApplet applet) {
        applet.stroke(255);
        applet.noFill();
        applet.ellipseMode(PApplet.CENTER);
        applet.ellipse(mX, mY, mSize, mSize);
    }

    public void animate(long elapsed) {
        updateSpeedY(elapsed);
        if (mSpeedX != 0 || mSpeedY != 0) {
            checkBorders();
        }
    }

    private void checkBorders() {
        if (mY > BubblePhysicApplet.height - mSize / 2) {
            mY = 2 * BubblePhysicApplet.height - (mY + mSize);
            mSpeedY = -mSpeedY;
        }
        if (mX > BubblePhysicApplet.width) {
            mX = BubblePhysicApplet.width - (mX - BubblePhysicApplet.width);
            mSpeedX = -mSpeedX;
        }
    }

    private void updateSpeedX() {

    }

    private void updateSpeedY(long elapsed) {
        mSpeedY += (elapsed / 1000f) * 9.81f;
        if (mSpeedY < 0) {
            mSpeedY *= 0.95f;
        }

        mY += mSpeedY;
    }
}
like image 848
WarrenFaith Avatar asked Jan 20 '23 14:01

WarrenFaith


2 Answers

It's not only air resistance that slows the ball down, but the fact that it's not perfectly elastic as this line suggests: mSpeedY = -mSpeedY;

The ball absorbs energy when it squishes against the floor before it bounces back, so it doesn't bounce as high. Try a real super ball. I seem to remember it only bounces 80% as high or so. You might try: mSpeedY = - (0.8 * mSpeedY);

like image 94
Chris Avatar answered Feb 01 '23 08:02

Chris


you have to fix your check borders method, read this answer I just gave a complete formulas needed for realistic physical simulation. and it's also more realistic if you move objects using hist method (p = v*dt + 1/2*adtdt)

like image 20
Ali1S232 Avatar answered Feb 01 '23 07:02

Ali1S232