aiming and directional movement trouble in 2d game

Hi!

Im writing my second game now, and am having the same problems as i had on my first game.

When firing a bullet from a weapon i cant make it go in the exact direction i want it to go. On my last game (http://home.online.no/~stefjako) i made the bullet movements manually sort of. Very dirty. But now i use Math.cos and Math.sin to move it:

publicvoid draw(Graphics2D g2)

{

g2.setColor(Color.WHITE);

g2.drawOval(position.x, position.y, 1, 1);

}

/**

* Moves bullet!

*/

publicvoid move()

{

double newX = (double)position.x +Math.cos(angrad)*factor;

double newY = (double)position.y +Math.sin(angrad)*factor;

position.setLocation(newX, newY);

factor += speed;

}

Where angrad is the given angle in radians, factor = 1.0 , and speed = 0.1 by default.

This works kindof... But in certain angles the bullet will move straight in one direction for a while, and then suddenly start going in an angle. (sorry for the awful explaining)!

How do u guys usually make movements work nice and feel smooth?

Thanks!

[1537 byte] By [Tenacioa] at [2007-10-1 22:26:13]
# 1
position should be stored as a floating point number.
Abusea at 2007-7-13 8:40:28 > top of Java-index,Other Topics,Java Game Development...
# 2
thanks for the reply, but i don't see what you mean.i'm using the g2.drawOval(position.x, position.y, 1, 1); to draw the bullet. Graphics2D.drawOval method doesn't take any float values. only integers.Could you please spesify?
Tenacioa at 2007-7-13 8:40:29 > top of Java-index,Other Topics,Java Game Development...
# 3

Something like this:

double oldX;

double oldY;

public void draw(Graphics2D g2) {

g2.setColor(Color.WHITE);

g2.drawOval((int)oldX, (int)oldY, 1, 1);

}

public void move() {

oldX += Math.cos(angrad)*factor;

oldY += Math.sin(angrad)*factor;

factor += speed;

}

You need to store the position as a double value and feed it to the drawOval as a int value.

T10a at 2007-7-13 8:40:29 > top of Java-index,Other Topics,Java Game Development...
# 4
> You need to store the position as a double value and> feed it to the drawOval as a int value.This is called casting (for the OP's benefit.)
Viruma at 2007-7-13 8:40:29 > top of Java-index,Other Topics,Java Game Development...
# 5

I know what casting is. But what difference would casting it as an integer make?

If the drawOval-method only draws at the rounded integer position anyways? There are no compiler errors in the game.

What i actually wanted to know was how you guys make aiming work in your games. How do you calculate angles and so?

Tenacioa at 2007-7-13 8:40:29 > top of Java-index,Other Topics,Java Game Development...
# 6

More or less like you do.

If I should make a example of why you have to use some sort of float value to hold the position, try making a game where the "game objects" move less than a pixel/frame.

If you use a int value for storing the position this will mean that it won't move at all because it will never move a whole pixel.

Look at this

If your object have a int x and int y holding it's current position.

x + 0.8 = xbecause 0.8 will be rounded to 0

y + 0.7 = yand the same here

That's what happens when you use only int values, float values is almost nessecary to store the position in games.

T10a at 2007-7-13 8:40:29 > top of Java-index,Other Topics,Java Game Development...