Adding gravity

I have made two classes that allows me to cut a BufferedImage into pieces, and apply a direction and speed to them. I use that to make sprites explode when the character dies. However I have no idea how to add gravity to them. So far the pieces just float off into any direction instead of falling down to the bottom of the screen.

My very simple code

[quote]layer.setX(layer.getX() + Math.sin(layer.getDirection()) * layer.getSpeed());
layer.setY(layer.getY() + Math.cos(layer.getDirection()) * layer.getSpeed());
[/quote]
The “layer” object have a getPixelCount() method, which returns the amount of none fully transparent pixels in the image, which is what I was going to use to add different weight to the pieces.

you can just constantly increase the downward speed over time, when your object isnt on solid ground, for a simple solution.

I could try, but I was hoping someone would know a good formula to change the direction downwards using the weight of the object.

usually a good approach is to just multiply the falling speed by the “weight”… it works quite well to just mess around with various made-up formulas and values, until you get something which gives your game a good lively feeling.

Actually gravity is independent of mass.

[quote]gravity is independent of mass.
[/quote]
True, but games are often independent of reality ;D Games dont have to be realistic to be fun.

Sure, but why complicate the issue. If that effect is needed, he can add it later. Your first suggestion was just right.

I finally understand, now I’m using

layer.setVerticalSpeed(layer.getVerticalSpeedSpeed() + 0.25);
layer.setX(layer.getPreciseX() + layer.getHorizontalSpeed());
layer.setY(layer.getPreciseY() + layer.getVerticalSpeed());

and when I set the speed

int speed = 3 + r.nextInt(5);
int direction = r.nextInt(360);

layer.setHorizontalSpeed(Math.sin(Math.toRadians(direction)) * speed);
layer.setVerticalSpeed(Math.cos(Math.toRadians(direction)) * speed);

It would have been much easier if one of you had explained that speed and direction was a bad thing to go about it, and tell me that I should use Horizontal and Vertical speed instead.

Ah ok. Just learn vectormath and the difference will go away :stuck_out_tongue:

Why layer.getVerticalSpeedSpeed() haha?
The second speed seems redundant
You should use the term “acceleration” if you actually mean the speed at which the speed increases.

It was a spelling mistake, it suppose to just be getVerticalSpeed