Before you give me a link to another post look first, just in case.
So I'm making a top down game where the person can shoot around, I want the bullets to move towards the mouse, I have tried this:
angle = (float) Math.atan2(pmx - x, pmy - y) * 180 / Math.PI;
x += this.getVelX() + (Math.cos(angle * Math.PI / 180) * 15);
y += this.getVelY() + (Math.sin(angle * Math.PI / 180) * 15);
The pmx
means previous mouse position where I clicked, and the same for pmy
.
But this doesn't work properly look at this screenshot, the gun is pointed towards the mouse (the screenshot doesn't show the mouse) and the yellow squares are the bullets.
How it moves the bullets is the goes towards the mouse about 50 pixels then curls down to the left and inverted for the other side.
After looking at Theraot's post Section B:
int speed = 15;
int velocityX = pmx - x;
int velocityY = pmy - y;
length = Math.sqrt(velocityX * velocityX + velocityY * velocityY);
velocityX *= speed / length;
velocityY *= speed / length;
x += velocityX;
y += velocityY;
This has worked to move it the direction of the mouse, but a new issue has occurred whether this is my issue or not I don't know, as I'm not that good at maths, what it does is moves in the correct direction and when it gets to the mouses old position where it was clicked it stop moving:
If there is a way to stop this from that method can someone tell me if not which Section should I use instead?
pmx
is defined when I click and so it doesn't move around after I move the mouse, this is what I wanted it's just the maths which is the issue for me \$\endgroup\$