Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm having trouble making my bullets move at a steady pace using delta. I have the formula I used outside of slick2d, but it doesn't take delta into account. My bullets fly fast. How do I make them go slower.

x = x + (int) (length * Math.cos(this.angle));
y = y + (int) (length * Math.sin(this.angle));
share|improve this question
I've answered how to take delta into account in the question Should frame rate affect the speed in a game engine? – doppelgreener Aug 18 '12 at 3:13
I know that you use x += delta * .2f to move right at a certain speed, but how do i do that for an equation like this? – Saif A Chaudhry Aug 18 '12 at 4:08
can't you do x += (int)(length * Math.cos(this.angle) * delta); ? – HumanCatfood Sep 18 '12 at 10:51

If using slick2d, you can make use of the Vector2d class to do most stuff for you. Have a velocity variable (of the type vector2f) in your entity class.

Then you can do something like this:

//During creation of the entity
entity.position = new Vector2f(0, 0);
entity.velocity = new Vector2f(1, 0);

//During update of the entity
float speed = 0.1f;
entity.position.add(entity.velocity.copy().scale(speed * d));

Basically what you do is create a vector, that represents the direction in which the entity is moving. By using new Vector(1, 0), the ray of the vector is 1 "unit" long. During each update, you take this vector (I make a copy, so the initial value is not changed) and multiply it by delta and by speed. Now you can add this multiplied vector to the position vector.

If you want to change the move speed, simple adjust the speed value (smaller values result in smaller speeds).

EDIT: Just for clarification, as I don't know your knowledge with slick: Vector2d is a class, that holds a x and an y value. Vectors support some basic mathematical operations, like addition, subtraction, multiplication (scale). In addition, Vector2f has some nice helper methods, like setTheta and getTheta (keep the length of the vector the same, but change the angle) or normalise (keep the angle, but change the vector length to 1).

If you have two vectors, vecA and vecB, consider this behavior identical:


vecA.x = vecA.x * vecB.x
vecA.y = vecA.y * vecB.y
//is the same as this:

And in the case of the scale with a float value

float scale = 0.1f;
vecA.x = vecA.x * scale;
vecA.y = vecA.y * scale;
//Is the same as:
share|improve this answer

If you're calling the method from the update method why aren't you using delta? Call the moving forward method from the update method of the entity, along with a speed parameter.

The way you can do it is set a speed variable to your bullets, which you use to limit the movement speed which they travel through.

       float hipothenus = movementSpeed * (float) delta;
       x = this.getX() + hipothenus * (float) Math.sin(Math.toRadians(rotation));
   y = this.getY() - hipothenus * (float) Math.cos(Math.toRadians(rotation));

Bear in mind that you will also need to clamp the delta rate with setMinimumLogicUpdateInterval and setMaximumLogicUpdateInterval functions.

share|improve this answer
I didn't quite understand what you mean. This is the equation I use, just like yours. You just need to add delta to your x = x + (int) (hipothenus * length * Math.cos(this.angle)); with hipthenus being the bullet's speed multiplied by delta. – Midori Ryuu Aug 18 '12 at 5:34
The user seems to be using integer x and y, you may wish to elaborate on the need to have floating point storage for the position and coerce it to integer if the library needs it. – Lars Viklund Aug 18 '12 at 9:49
The reason why I use floats is because a lot of the library's functions use float parameters, but using integers works just as good. Just replace the (float) with (int) – Midori Ryuu Aug 18 '12 at 10:27
You misunderstand me, if you constantly coerce to integer, you will lose any fractional movement which makes movement quantize into whole numbers. If your unit is around the size of a pixel and a timestep around 1/30 to 1/60, you will typically end up with deltas being pretty much 0 all the time. – Lars Viklund Aug 18 '12 at 13:40
Hmmm... I don't know.. the cast is being done -after- the delta is multiplied by the movement speed, and in my calculations the added x is often much larger than 1, thus giving a low error rate when cast to an int. But yeah, as you said, it is probably better to use floats or double. – Midori Ryuu Aug 19 '12 at 6:18

Try this:

x += Math.cos(Math.toRadians(rotation)) * ((speed/100)*delta);
y += Math.sin(Math.toRadians(rotation)) * ((speed/100)*delta);

I used it in ym game to work out x and y positions, thought it might be helpful...

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.