Well, I recently started making a little top-down shooting game and I need a bit of help with the bullets!
I basically want them to fire in the direction of the mouse. Any help? Sorry for horribleness, I didn’t know how to ask question
So you want to move the bullet from the player to the mouse?
You basically want to use some trig to figure this out:
// These get the delta positions between the mouse and the player
int distX = Mouse.getX() - player.getX();
int distY = Mouse.getY() - player.getY();
// This determines the angle of the bullet using an inverse tangent function.
double angle = Math.atan2(distX, distY); // I should also note that this returns the angle in radians.
// These set the bullet's speed in the x and y axis, along with its rotation.
bullet.setDx(Math.sin(angle));
bullet.setDy(Math.cos(angle));
bullet.rotate(angle);
I hope this is what you were asking for. If you need any other help, just ask.
Ha ha thanks, But I figured it out by myself ;D
If anyone wants to know how, I basically did this:
(r is the player which is a circle)
double angle = Math.atan2((double)r.getCenterY() - (double)container.getInput().getMouseY(), (double)r.getCenterX() - (double)container.getInput().getMouseX()) - Math.PI;
bullets.add(new Bullet(new Circle(r.getCenterX(), r.getCenterY(), 5), angle));
r.setX((float)(r.getX()+Math.cos(b.getAngle())*b.getSpeed()));
r.setY((float)(r.getY()+Math.sin(b.getAngle())*b.getSpeed()));
I hate to quote myself*, but:
If you used the Search bar at the top left of the page, you perhaps would’ve found this yourself. :point:
*Disclaimer: I don’t hate to quote myself. ;D