SOLVED Breaking a bullet's velocity into components

Hi

So I was starting to implement shooting into my game (for now just calculating movement, and not factoring gravity at all), and I was rendering the bullets by drawing a quad at the given location. Each

update()

I would add the two stored components to the position of the bullet. Anyway, when I was trying out different angles, the bullet would only go at the nearest multiple of 45 degrees, which confused me because the input angles aren’t in multiples of that.

Bullet vectory component calculation:


velocityX = (float)(Math.cos(angle) * velocity);
velocityY = (float)(Math.sin(angle) * velocity);

Any thoughts or ideas would be helpful :slight_smile:

CopyableCougar4

Are you sure that angle is in radians?

Yup! I use

Math.atan2(y, x);

to get the angle :slight_smile:

Is angle a double or float? What are x and y?

x and y were just the placeholders I used to show what formula I used. In reality x is the difference between the x of the mouse and the x of the player, and y is the difference between the y of the mouse and the y of the player. All math functions return a double but the number is passed as a float.

I meant what are the types. Just post the section of code containing this, can’t really do anything but presume right now.

This code constructs the bullet. The mouse rectangle just stores the mouse location and size.


Rectangle mouse = DisplayManager.getInstance().getRenderer().getMouse();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
float x = screenSize.width / 2.0f;
float y = screenSize.height / 2.0f;
int dx = (mouse.x - Math.round(x));
int dy = (mouse.y - Math.round(y));
float angle = (float)(Math.atan2(dy, dx));
bullets.add(new Bullet(x, y, width, height, angle, velocity, color));

This code is the constructor for bullet objects


public Bullet(float x, float y, float width, float height, float angle, float velocity, Color color){
		
	// Construct the bounds
	bounds = new Rectangle(Math.round(x), Math.round(y), Math.round(width), Math.round(height));
	// Calculate the velocities
	velocityX = (float)(Math.cos(angle) * velocity);
	velocityY = (float)(Math.sin(angle) * velocity);
	// Set some variables
	this.color = color;
	done = false;
	life = 3000;
	this.angle = angle;
		
}

This code makes the bullet travel


public void travel(){
		
	if(isAlive()){
		life--;
		bounds.x += Math.round(velocityX);
		bounds.y += Math.round(velocityY);
	}
	if(life == 0){
		dispose();
	}
		
}

bounds is a rectangle that stores the collision region for the bullet, for the future.

Let me know if you need more code :slight_smile:

CopyableCougar4

Quite possible it’s that bounds.x, bounds.y are integers, at least try removing the Math.round()s from
[icode]bounds.x += Math.round(velocityX);
bounds.y += Math.round(velocityY);[/icode] or changing them to floor() or ceil().

Also might be that [icode]velocity[/icode] is computed wrong, and rounds to 0 on non-45 degree divisible angles.

Thanks :slight_smile: I found my mistake. With the rounding it only allowed for the 45 degree multiples because the change was rounded.

CopyableCougar4