I figured out how to make the shots fire at the speed that I want them to (Thanks again PhiLho) but now I ran into the issue of finding an appropriate angle for the shots to fire at.
I am trying to make an enemy that acts as turret (360 degree firing arc) but am having trouble calculating the angle of shots based on the player position(mouseX,mouseY).
I know that I need to use something to the effect of:
line(x,y, 20*sin(angle)+ x, 20*cos(angle) + y);
in order to get the proper rotation on the shot, but where I run into trouble is in calculating value to plug into the angle variable. Because I want the angle to change based on the mouse position, I would assume it has something to do the mouse coordinates, but since I haven't done any trig since high school, I am sort of at a loss as to what formula to set the variable "angle" equal to.
Here are the relevant methods I am using:
Code:
/*In the main game, I am using a dynamic array of these shots to fire
at the player from varying positions(the x,y coords of the aliens which are animated) below are the methods of the IncomingFire class that I wrote which handle the calculation and display of the shots fired */
//sets initial trajectory for the shot
void update(){
Xdist = (mouseX - x);
Ydist = (mouseY - y);
angle = radians(?);
}
//fires a shot towards the player
//(algorithm credit to PhiLho)
void fire(){
PVector dist = new PVector(Xdist, Ydist);
dist.normalize();
xspeed = dist.x * scaleFactor;
yspeed = dist.y * scaleFactor;
x = x + xspeed;
y = y + yspeed;
}
//if timer has not run out, display the shot
//if timer has run out, move the x coord offscreen
void display(){
shotTimer--;
if(shotTimer <= 0){
live = false;
}
if(live == true){
stroke(255,0,0);
strokeWeight(2);
line(x,y, 20*sin(angle)+ x, 20*cos(angle) + y);
stroke(0);
strokeWeight(1);
} else {
x = - 50;
}
}
I took trig in high school, but that was a long time ago and have forgotten most of it. If anyone who remembers this type of math could lend me a hand I would be very grateful.