Sign in to follow this  
Followers 0
munito

Simple 2D Physics

1 post in this topic

Hey Everyone,

 

Im just playing around with Java and Slick2D and try to create a small Physics simulation.

At the moment I just want have a simple ball falling from the top of the screen to the bottom.

I want the ball to have some kind of rebound from the ground but I have no idea how I could accomplish that.

 

for the moment my Shape update and fall physics look like this (Which is a Cube at the moment).

 

private Rectangle shape;


private float x;
private float y;
private float friction;
private float velocity;
private float acceleration;


public Cube(int length) {
        this.length = length;
        this.x = 0;
        this.y = 0;
        this.friction = 0.8f;
        this.velocity = 0.0f;
        this.acceleration = 0.5f;
        this.shape = new Rectangle(this.x, this.y, this.length, this.length);
    }


public void setY(float y) {
        this.y = y;
        this.shape.setY(this.y);
    }


public void update(GameContainer gc, int delta) {
        if (this.shape.getMaxY() < gc.getHeight()) {
            this.velocity += this.acceleration * this.friction;
            this.setY(this.y + this.velocity);
        } else {
            this.velocity = 0.0f;
            this.setY(gc.getHeight() - this.length);
        }
    }

 

I could not find any good tutorials about physics on google so im trying to find some advices or hints here

 

Best Regards

0

Share this post


Link to post
Share on other sites

Simple bounce along the y-axis is done simply by inverting your downwards y-velocity and multiplying it by a 'bounce' factor, which should be between 0 and 1, where 0 is no bounce and 1 is perfect bounce.

From your code, I'd try changing the 'else' like this:

else {
  this.velocity = -this.velocity * this.bounceFactor; // where you declare this.bounceFactor to be 0 to 1 as mentioned above
  this.setY(gc.getHeight() - this.length);
}

 

Edit: Forgot to add: in general, bounce from any 2d surface, oriented in any direction is nearly the same, only you need to take into account the surface's normal:

1) Find speed (scalar value) of the object along the surface normal (usually by doing dot product between your velocity vector and the surface normal vector)

2) Separate your velocity vector into two parts: normal velocity, which is basically surfaceNormal*speedAlongNormal, and tangential velocity which is just myVelocityVector - normalVelocityVector

3) invert the direction of the normalVeclocityVector by multiplying by -1, and then just as above multiply by some 'bounce' factor that's 0..1 so that it scales down. 

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0