# I need help with bullet direction

**URL:** <https://discourse.processing.org/t/i-need-help-with-bullet-direction/7666>\
**Category:** Coding Questions\
**Created:** [January 20, 2019, 1:48am UTC](https://discourse.processing.org/t/i-need-help-with-bullet-direction/7666 "2019-01-20T01:48:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vGlint](https://avatars.discourse-cdn.com/v4/letter/v/bc8723/32.png) [@vGlint](https://discourse.processing.org/u/vGlint)\
**Post date:** [January 20, 2019, 1:48am UTC](https://discourse.processing.org/t/i-need-help-with-bullet-direction/7666/1 "2019-01-20T01:48:13Z")

</div>

i have made a simple project where a have a player that shoots. i have coded an aimer that gets bigger when you help the mouse down. i need help so the bullets will pick a random direction in the size of my aimer. here is the code.

```auto
ArrayList <Bullet> bullets = new ArrayList <Bullet> ();
PVector player, playerSpeed;
float maxSpeed = 3;
float s = 2.0;
int l = 2;
void setup() {
  size(600, 600);
  player = new PVector(width/2, height/2);
  playerSpeed = new PVector();
  noCursor();
  noStroke();
  smooth();
}
 
void draw() {
  text();
  background(255);
  fill(255);
  player.add(playerSpeed);
  fill(255, 0, 0);
  ellipse(player.x, player.y, 20, 20);
  fill(255);
  ellipse(player.x, player.y, 10, 10);
 
  PVector mouse = new PVector(mouseX, mouseY);
  drawC();
 
  if (frameCount%5==0 && mousePressed) {
    PVector dir = PVector.sub(mouse, player);
    dir.normalize();
    dir.mult(maxSpeed*3);
    Bullet b = new Bullet(player, dir);
    bullets.add(b);
  }
 
  for (Bullet b : bullets) {
    b.update();
    b.display();
  }
}
 
class Bullet extends PVector {
  PVector vel;
 
  Bullet(PVector loc, PVector vel) {
    super(loc.x, loc.y);
    this.vel = vel.get();
  }
 
  void update() {
    add(vel);
  }
 
  void display() {
    noStroke();
    fill(0, 0, 255);
    ellipse(x, y, 3, 3);
  }
}
 
void keyPressed() {
  if (key == 'w') { playerSpeed.y = -maxSpeed; }
  if (key == 's') { playerSpeed.y = maxSpeed; }
  if (key == 'a') { playerSpeed.x = -maxSpeed; }
  if (key == 'd') { playerSpeed.x = maxSpeed; }
}
 
void keyReleased() {
  if (key == 'w' || key == 's') { playerSpeed.y = 0; }
  if (key == 'a' || key == 'd') { playerSpeed.x = 0; }
}

void drawC()
{
stroke(0);
line(mouseX, mouseY + s, mouseX, mouseY + s + l);
line(mouseX, mouseY - s, mouseX, mouseY - s - l);
line(mouseX + s, mouseY, mouseX + s + l, mouseY);
line(mouseX - s, mouseY, mouseX - s - l, mouseY);
if(mousePressed)
 {
 if(s < 30)
 {
  s = s + .1;
 }
 }
 else
 {
  if(s > 2)
  {
   s = s - .5; 
  }
 }
} 

void text()
{
 println(bullets);
 println("FrameRate");
 println(int(frameRate));
}

```

---

<div class="post-metadata">

**Author:** ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)\
**Post date:** [January 20, 2019, 7:30am UTC](https://discourse.processing.org/t/i-need-help-with-bullet-direction/7666/2 "2019-01-20T07:30:16Z")

</div>

Could you elaborate on what you mean on a “random direction in the size of my aimer”?

Do you mean a direction in where the bullets are within the aimer, like: `PVector dir = PVector.sub(PVector.add(mouse,PVector.random2D().mult(s)), player);`?

---

<div class="post-metadata">

**Author:** ![Sky](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sky/32/3013_2.png) [@Sky](https://discourse.processing.org/u/Sky)\
**Post date:** [January 20, 2019, 8:38am UTC](https://discourse.processing.org/t/i-need-help-with-bullet-direction/7666/3 "2019-01-20T08:38:22Z")

</div>

x and y: random( center of the aimer - radius , center of the aimer + radius)
