View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Simple 2D Physics

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

### #1Olaf Van Schlacht  Members

Posted 28 January 2013 - 08:57 AM

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

Ognarion Commander (Java/LIBGDX): https://github.com/OlafVanSchlacht/ognarion-commander

### #2Milcho  Members

Posted 28 January 2013 - 09:02 AM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.