Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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); ? –  Jan Sep 18 '12 at 10:51

3 Answers 3

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);
entity.velocity.setTheta(angle);

//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:

Multiplication:

//This:
vecA.x = vecA.x * vecB.x
vecA.y = vecA.y * vecB.y
//is the same as this:
vecA.scale(vecB);

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:
vecA.scale(scale);
share|improve this answer

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

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
1  
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

Your Answer

 
discard

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.