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 › shooting while rotating
Page Index Toggle Pages: 1
shooting while rotating (Read 669 times)
shooting while rotating
Oct 18th, 2009, 2:13pm
 
I am trying to implement a simple game but I am having difficulties with it.  I have a tank (triangle) that is supposed to shoot shots when the spacebar is pressed. That works fine, but I can't figure out how to make it shoot the shots when the tank turns left and right. If I turn the tank left or right the shots still fire from the same location at the beginning.


//front tip of tank
 int xTip = 300;
 int yTip = 200;
 
 int bulletAmount = 20;

 ......

 void setup()
 {
     .......

    shot= new Shot[bulletAmount];
   for(int i = 0; i< bulletAmount; i++)
{
  shot[i] = new Shot();
}
 }
 
 void draw()
 {

   ......

     
      for(int i = 0; i < bulletAmount; i++){
  if(shot[i].alive){
    shot[i].showShot();
    shot[i].posY--;
  }
}
      for(int i = 1; i< bulletAmount ;i++){
  if(!shot[i].alive){
    if(keyPressed) {
      if(key == ' '){

     translate (300,200);
     rotate(angle);//angle of rotation of the tank
     translate (-300,-200);
     shot[i].posX = xTip;
     shot[i].posY = yTip;
        shot[i].alive = true;
        i++;
        return;
      }
    }
  }
}
     
class Shot{

boolean alive;
int posX;
int posY;

Shot(){
  alive = false;
}

void showShot(){
  ellipse(posX,posY,12,12);
}  
}

Re: shooting while rotating
Reply #1 - Oct 18th, 2009, 3:14pm
 
I think you may have a more fundamental problem, but I'll return to that in a moment.  Presumably what happens is that when the tank is at an angle the shots fire from a location other than the tank.  What you should perhaps consider is that the translate(-300,-200) takes place after the rotation, and is therefore affected by the rotation: i.e. the whole coordinate system has been rotated so translate(-300,-200) does not return to the same point, but a point in relation to the angle of rotation...  Perhaps you wanted to use pushMatrix

Of course the more fundamental problem I alluded to is that you'd need to store the angle each shot was fired at and rotate appropriately before applying motion to it; otherwise I suspect the shots will continue to be affected by the rotation of the tank after they have been fired.  That would involve a whole lot of push/popMatrixes which doesn't seem ideal...

A better option would be to calculate the direction vector for the angle when fired and avoid using rotate() altogether.  i.e. if fired straight up vx (velocity on the x axis) = 0 and vy (velocity on y axis) = -1.  If fired directly to the side vx = plus or minus 1 and vy = 0.  Any angle in between would need an appropriate vector (trigonometry is your friend).

I know that would be my preferred approach as relying on rotation of the whole coordinate space is likely to make collision detection rather tricky...
Page Index Toggle Pages: 1