Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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.

  • You cannot reply to this topic
1 reply to this topic

#1 Olaf Van Schlacht   Members   -  Reputation: 761

Like
0Likes
Like

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


printf("Hello Signature");


Sponsor:

#2 Milcho   Crossbones+   -  Reputation: 1177

Like
1Likes
Like

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.



PARTNERS