Bullet Directional Shooting Images [Need Help]

Hello Everyone,

I need some help with something. Right now i have a play and i use the mouse to shoot the bullet. The bullet shoots towards the mouse fine and dandy. Basically I would like the change the image of the bullet when ever the bullet is at a certain Degree or Radian. Could someone help me. I supplied arrows and an image as to what i want to do.

Thank you.

So, a bullet shooting left would look different than a bullet shooting right (besides the flip)? Or are you asking how to flip/rotate bullet image per direction?

I have an image with 8 bullets inside of it. for all 8 directions, “North”, “North-East”, “East”, “South-East”, “South”, “South-West”, “West”, and “North-Wast” like the black arrows in the images. I want to change the image when the mouse is at the “North-East” of the player or if the mouse is at the “South-East” of the player etc…

I am using this to shoot the bullet towards the mouse

rad = Math.atan2(mouseY - bullet.getY(), mouseX - bullet.getX())
dx = speed * Math.cos(rad);
dy = speed * Math.sin(rad);

This is how i change the direction of the bullet

Why change the image? You can also rotate it by the angle you just calculated. If you use pure Java2D you could do something like that:


AffineTransform at = new AffineTransform();
at.rotate(angle);
g2d.transform(at);

It seems to be rotating everything on the screen for example the hud and stuff.

Ok then you need to create a separat AffineTransform like this:


// You create an identity transform (this should never change):
AffineTransform identity = new AffineTransform();
// You create another transform (used for rotating):
AffineTransform at= new AffineTransform();
// set the transform for rotating to the identity (do this with every bullet):
at.setTransform(identity)
// rotate your image:
at.rotate(angle);
// Draw your image:
g2d.drawImage(image, at, this);

The AffineTransform class is quite complicated and i don’t even understand it fully, but that would maybe do the trick :).

Edit: changed the code because it won’t do anything else than my code posted before xD

I think what you’re looking for is:

//rotate the Graphics2D object around the center of your bullet object
g.rotate(angle,x+(bulletWidth/2), y+(bulletHeight/2));
//draw the bullet as you would normally
g.drawImage(bulletImage, x,y, null);
//rotate it back in the using negative angle
g.rotate(-angle,x+(bulletWidth/2), y+(bulletHeight/2));

It’s just a matter of rotating it back. :slight_smile:
However, depending on your usage of rotation (especially with larger images), sometimes it is logical to cache an pre-rendered image array of,say every 5 or 10 degrees, so that you don’t have to do the rotation each time.

Also, just food for thought, you could also render the bullet as just a line if you don’t want to deal with all that.

A free bullet image by me.

And to rotate use @epicpunnum’s method.