Jump to content
• Advertisement

# Simple 2D Physics

This topic is 2119 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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

#### Share this post

##### Share on other sites
Advertisement

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.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 16
• 18
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633360
• Total Posts
3011524
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!