# Shooting moving circles

**URL:** <https://discourse.processing.org/t/shooting-moving-circles/13791>\
**Category:** Coding Questions\
**Created:** [September 4, 2019, 8:51am UTC](https://discourse.processing.org/t/shooting-moving-circles/13791 "2019-09-04T08:51:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbiju72](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@sbiju72](https://discourse.processing.org/u/sbiju72)\
**Post date:** [September 4, 2019, 8:51am UTC](https://discourse.processing.org/t/shooting-moving-circles/13791/1 "2019-09-04T08:51:58Z")

</div>

# I’m trying to shoot a moving ball. Most of it works (ball moves, hit is detected). Except the the movement of bullet. I can’t get it moving.

```auto
void setup(){
  size(1000,800);
  
}
Projectile bullet = new Projectile();
Enemy rock = new Enemy();
boolean flag = true;

    
void draw()
{
 background(100,100,100);
 
 stroke(0,100,0);
 circle(rock.pos.x, rock.pos.y, rock.radius*2);
 stroke(100,0,0);
 circle(bullet.pos.x, bullet.pos.y, bullet.radius*2);
 if (mousePressed && (mouseButton == LEFT))
    {
      //bullet.set_vel(mouseX, mouseY);
      
      while (flag){
        stroke(100,0,0);
        circle(bullet.pos.x, bullet.pos.y, bullet.radius*2);
       
        bullet.update();
        println("Bullet position " + str(bullet.pos.x) + " " + str(bullet.pos.y));
        hit(rock,bullet);
        flag = false;
      }
    }
 rock.update();
 hit(rock,bullet);
 
}

void hit(Enemy rock, Projectile bullet){
  float dx, dy, distance;
  dx = rock.pos.x - bullet.pos.x;
  dy = rock.pos.y - bullet.pos.y;
  distance = sqrt((sq(dx) + sq(dy)));
  if (distance < (rock.radius + bullet.radius)){
    rock.respawn();
    bullet.respawn();
    println("Rock hit bullet");
  }
}
void keyPressed()
{
  if (key == ESC)
  {
    flag = false;
    exit();
  }
}
=======================
class Enemy{
  PVector pos;
  PVector vel;
  float radius;
  float speed;
  
  Enemy(){
    //pos = new PVector(width * 0.75, height/4);
    pos = new PVector(300, 600);
    vel = new PVector(round(random(-2,2)), round(random(-2,2)));
    radius = 60;
    speed = 5;
  }
void update(){
   pos.x = pos.x + vel.x;
   pos.y = pos.x + vel.y;
   //print("Inside bullet update");
   out_of_bounds();
 }
 boolean out_of_bounds(){
   if ((pos.x+radius) > width || (pos.y + radius) > height || (pos.x - radius )< 0 || (pos.y - radius ) <0)
   {
     respawn();
   }
   return false;
   }
 
 void respawn(){
   pos.x = random(0,width - radius - 1);
   pos.y = random(0, height - radius -1);
   vel.x = random(-2,2);
   vel.y = random(-2,2);
   print("Rock respawn");
   }
}
=========================== 
class Projectile{
  PVector pos;
  PVector vel;
  float speed;
  float radius;
  PVector target; //for debug
  
  Projectile()
  {
    pos = new PVector(500,400);
    vel = new PVector(round(random(-5,5)), round(random(-5,5))); //(round(random(-5,5))), round(random(-5,5))
    speed = 10;
    radius = 8;
    target = new PVector();
    disp_comment("Creating bullet "+ str(vel.x)+ " " +str(vel.y));
  }
  void update()
 {
   pos.x = pos.x + vel.x;
   pos.y = pos.x + vel.y;
   out_of_bounds();
   disp_comment("Bullet updated " + str(pos.x) + " " + str(pos.y) + " ");
 }
 void set_vel(int tx, int ty){
// testing purpose
   //stores target position
   target.x = tx;
   target.y = ty;
   
   //set position back to the centre
   pos.x = width/2;
   pos.y = height/2;
   disp_comment("Set Vel " + str(pos.x) + " " + str(pos.y) + " ");
   
 }
 void out_of_bounds(){
   if ((pos.x+radius) > width || (pos.y + radius) > height || (pos.x - radius )< 0 || (pos.y - radius ) <0)
   {
     respawn();
     disp_comment("Bullet Out of bounds ");
   }
   }
void respawn(){
  pos.x = width/2;
  pos.y = height/2;
  vel.x = 0;
  vel.y = 0;
  disp_comment("Respawning bullet ");
}
void disp_comment(String s )
{
  println(s);
}
}

```

========================  
I fail to understand why the bullet is not redrawn. Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [September 4, 2019, 9:58am UTC](https://discourse.processing.org/t/shooting-moving-circles/13791/2 "2019-09-04T09:58:49Z")

</div>

Hi,

Please format your code by pressing `Ctrl+T` in the Processing IDE and then use the `</>` button in the message editor on the forum.

---

<div class="post-metadata">

**Author:** ![sbiju72](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@sbiju72](https://discourse.processing.org/u/sbiju72)\
**Post date:** [September 4, 2019, 12:02pm UTC](https://discourse.processing.org/t/shooting-moving-circles/13791/3 "2019-09-04T12:02:17Z")

</div>

```auto

void setup() {
  size(1000, 800);
}
Projectile bullet = new Projectile();
Enemy rock = new Enemy();
boolean flag = true;

void draw()
{
  background(100, 100, 100);

  stroke(0, 100, 0);
  circle(rock.pos.x, rock.pos.y, rock.radius*2);
  stroke(100, 0, 0);
  circle(bullet.pos.x, bullet.pos.y, bullet.radius*2);
  if (mousePressed && (mouseButton == LEFT))
  {
    //bullet.set_vel(mouseX, mouseY);

    while (flag) {
      stroke(100, 0, 0);
      circle(bullet.pos.x, bullet.pos.y, bullet.radius*2);

      bullet.update();
      println("Bullet position " + str(bullet.pos.x) + " " + str(bullet.pos.y));
      hit(rock, bullet);
      flag = false;
    }
  }
  rock.update();
  hit(rock, bullet);
}

void hit(Enemy rock, Projectile bullet) {
  float dx, dy, distance;
  dx = rock.pos.x - bullet.pos.x;
  dy = rock.pos.y - bullet.pos.y;
  distance = sqrt((sq(dx) + sq(dy)));
  if (distance < (rock.radius + bullet.radius)) {
    rock.respawn();
    bullet.respawn();
    println("Rock hit bullet");
  }
}
void keyPressed()
{
  if (key == ESC)
  {
    flag = false;
    exit();
  }
}

```

```auto
class Enemy {
  PVector pos;
  PVector vel;
  float radius;
  float speed;

  Enemy() {
    //pos = new PVector(width * 0.75, height/4);
    pos = new PVector(300, 600);
    vel = new PVector(round(random(-2, 2)), round(random(-2, 2)));
    radius = 60;
    speed = 5;
  }
  void update() {
    pos.x = pos.x + vel.x;
    pos.y = pos.x + vel.y;
    //print("Inside bullet update");
    out_of_bounds();
  }
  boolean out_of_bounds() {
    if ((pos.x+radius) > width || (pos.y + radius) > height || (pos.x - radius )< 0 || (pos.y - radius ) <0)
    {
      respawn();
    }
    return false;
  }

  void respawn() {
    pos.x = random(0, width - radius - 1);
    pos.y = random(0, height - radius -1);
    vel.x = random(-2, 2);
    vel.y = random(-2, 2);
    print("Rock respawn");
  }
}

```

```auto
class Projectile {
  PVector pos;
  PVector vel;
  float speed;
  float radius;
  PVector target; //for debug

  Projectile()
  {
    pos = new PVector(500, 400);
    vel = new PVector(round(random(-5, 5)), round(random(-5, 5))); //(round(random(-5,5))), round(random(-5,5))
    speed = 10;
    radius = 8;
    target = new PVector();
    disp_comment("Creating bullet "+ str(vel.x)+ " " +str(vel.y));
  }
  void update()
  {
    pos.x = pos.x + vel.x;
    pos.y = pos.x + vel.y;
    out_of_bounds();
    disp_comment("Bullet updated " + str(pos.x) + " " + str(pos.y) + " ");
  }
  void set_vel(int tx, int ty) {
    //stores target position
    target.x = tx;
    target.y = ty;

    //set position back to the centre
    pos.x = width/2;
    pos.y = height/2;
    disp_comment("Set Vel " + str(pos.x) + " " + str(pos.y) + " ");
  }
  void out_of_bounds() {
    if ((pos.x+radius) > width || (pos.y + radius) > height || (pos.x - radius )< 0 || (pos.y - radius ) <0)
    {
      respawn();
      disp_comment("Bullet Out of bounds ");
    }
  }
  void respawn() {
    pos.x = width/2;
    pos.y = height/2;
    vel.x = 0;
    vel.y = 0;
    disp_comment("Respawning bullet ");
  }
  void disp_comment(String s )
  {
    println(s);
  }
}

```
