We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Firing speeds in retro style space shooter
Page Index Toggle Pages: 1
Firing speeds in retro style space shooter (Read 1574 times)
Firing speeds in retro style space shooter
Apr 28th, 2010, 11:20pm
 
Hello,

I am currently working on a space shooter in which aliens fire at the player ship. (the player ship is mouseX, mouseY) I am trying to get the shots to fire at the player, which I can do (nearly). However, I am having trouble setting the shots to a uniform speed. If the alien is far away from the player, the shots fire a decent speed. If the player is close to the alien, the shots fire very slowly. I am trying to find a way to make the shots fire at a uniform speed and still end up at mouseX, mouesY(assuming the mouse is not being moved. )

Here are the methods I am currently using:

void update(){
   Xdist = (mouseX - x);
   Ydist = (mouseY - y);    
 }

 void fire(){
   x = x + (Xdist/50);
   y = y + (Ydist/50);
}


I am fairly certain my formula is wrong, but I am not sure how it should look. Any insight would be greatly appreciated!

Thanks!

Re: Firing speeds in retro style space shooter
Reply #1 - Apr 29th, 2010, 4:48am
 
Well, if you make your steps proportional to distance, no wonder speed depends on it...
Now, that's easier said than done... Smiley
One way is to make speed constant on one axis:
xStep = 5;
yStep = Xdist == 0 ? xStep : xStep * Ydist / Xdist;

void fire() {
 x += xStep;
 y += yStep;
}

Not entirely satisfying...
Actually, what you want is a vector of unit length in the given direction.
Being lazy, instead of computing that myself, I let PVector do that.
I made a little sketch to verify I am right...
Code:
boolean bFiring;
float x, y;
float xSpeed, ySpeed;

float targetX, targetY;

void setup()
{
 size(500, 300);
 targetX = width / 2;
 targetY = height / 2;
}

void draw()
{
 background(255);
 fill(#00FF00);
 ellipse(targetX, targetY, 8, 8);
 if (bFiring)
 {
   x += xSpeed;
   y += ySpeed;
   fill(#FF0000);
   ellipse(x, y, 4, 4);
   if (abs(x - targetX) < abs(xSpeed) && abs(y - targetY) < abs(ySpeed))
   {
bFiring = false;
   }
 }
}

void mousePressed()
{
 x = mouseX;
 y = mouseY;
 float xDist = targetX - x;
 float yDist = targetY - y;
 PVector dist = new PVector(xDist, yDist);
 dist.normalize();
 xSpeed = dist.x * 3;
 ySpeed = dist.y * 3;
 println("Speed: " + xSpeed + " " + ySpeed);
 bFiring = true;
}
Re: Firing speeds in retro style space shooter
Reply #2 - Apr 29th, 2010, 8:17am
 
Thank you PhiLho I think I have it now!
Re: Firing speeds in retro style space shooter
Reply #3 - Apr 29th, 2010, 11:20am
 
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.
Re: Firing speeds in retro style space shooter
Reply #4 - Apr 29th, 2010, 12:09pm
 
Code:
line(x,y, 20*sin(angle)+ x, 20*cos(angle) + y);

If I understand you correctly, then you want:
Code:

angle = atan2(mouseX - x, mouseY - y);
Re: Firing speeds in retro style space shooter
Reply #5 - Apr 29th, 2010, 12:22pm
 
Perfect! Thanks for the help Smitty!
Re: Firing speeds in retro style space shooter
Reply #6 - May 1st, 2010, 12:09pm
 
hmm, I use the exact opposite for the same result:
angle = atan2(mouseY - y, mouseX - x);
line(x,y, 20*cos(angle)+ x, 20*sin(angle) + y);
(not that it matters?)
Re: Firing speeds in retro style space shooter
Reply #7 - May 2nd, 2010, 2:22am
 
BenHem is right, atan2 takes y, x as parameters (reverse of other functions, historical reasons I suppose).
Re: Firing speeds in retro style space shooter
Reply #8 - May 2nd, 2010, 10:29pm
 
I noticed that as well when I checked the API but for some reason (I probably reversed something somewhere) the x first then y version that smitty posted works while the other way around does not.
Page Index Toggle Pages: 1