Making a spiral shooting enemy

Hi I was wondering on how to make an enemy shoot in a spiral way around the screen.

BasicEnemy(float tempX, float tempY, int tempXdir, int tempYdir, int tempSize, float tempSpeed, float tempDamage)

Thats the constructor for a square enemy that just moves in a direction until it leaves the screen so how would I be able to make an enemy shoot out a bunch of these types of enemies in a spiral way out. I was thinking on using map but I’m not sure how do this. Please help me out if you can or point me in the right direction.

1 Like

If i didn‘t get you wrong, you probably just want to set tempX and tempY to the position of your main enemy. And set tempDirX and tempDirY to sin(angle) and cos(angle) respectively.

Then just use 0 as the first angle and increase it every time a new enemy is shot. The spiral pattern is created automatically because of the delay between shots.

I’m a little confused on how to get the cos and sin angles to constantly change after each shot yes you have the right idea as the Xdir and Ydir are the directions they go at a constant speed. I have no idea how to implement the cos and sin angles for each shot but I do know how I could set a delay after each shot. If you could give me an idea for the cos and sin angles variables.

I don’t understand.

When your enemy (cowboy/alien) shoots something, these would be bullets.

Do you want the bullets spawn from the enemy in a spiral way? See the tutorial about trigonometry about cos and sin: https://www.processing.org/tutorials/trig/

3 Likes

The angle variable is just one. At the first shot it‘s 0, then it increments by some value (let‘s say 1), so the next enemy is shot at angle 1, then increment again, and so on and so forth.

Something like this :

Enemy e;

void setup() {
   size(300,600);
   
   e = new Enemy(150,150);
}

void draw() {
   println(frameCount);
   background(255);
   if (frameCount%20==0)
   e.shoot();
   println("shot");
   e.moveBullets();
   e.display();
   
   //e.posX += 0.1;
}

class Bullet { 
   //this is the shot enemy, but can‘t call both enemy...
   float posX, posY;
   float dirX;
   float dirY;
   
   Bullet (float x, float y, float dx, float dy) {
      posX = x;
      posY = y;
      dirX = dx;
      dirY = dy;
   }
   
   void display() {
      ellipse(posX, posY, 3, 3);
   }
   
   void move() {
      posX += dirX;
      posY += dirY;
   }
}

class Enemy {
   
   final float BULLET_SPEED = 0.8;
   
   final float ANGLE_INCREMENT = PI/2; 
   //2PI would only shoot down, PI is up down, 
   //halfPi is in 4 dirs and so on
   //you can also just use some random number like 0.3256
   //but try keeping it within 0-2PI
   
   float posX, posY;
   
   Bullets[] bullets; //might want to use a list
   
   Enemy (float x, float y) {
      posX = x;
      posY = y;
      
      bullets = new Bullets[0];
   }
   
   void display() {
      ellipse(posX, posY, 10, 10);
      for (int i = 0; i < bullets.length; i++) bullets[i].display();
   }
   
   void shoot() {
      bullets = append(bullets, new Bullet(posX, posY, sin(bullets.length*ANGLE_INCREMENT)*BULLET_SPEED, cos(bullets.length*ANGLE_INCREMENT)*BULLET_SPEED));
   }
   
   void moveBullets() {
      for (int i = 0; i < bullets.length; i++) {
         bullets[i].move();
      }
   }
}
2 Likes

made some improvements

e.g. (this seems to be necessary in older version of processing 3.4)

    // append it
    bullets = (Bullet[]) append(bullets, newBullet);

AND:

and cos is for x, sin for y, so first cos, then sin

full sketch:

Enemy e;

void setup() {
  size(1300, 600);

  e = new Enemy(width/2, height/2);
}

void draw() {
  background(255);

  // every 20th frame, we shoot 
  if (frameCount%20 == 0) {
    e.shoot();
    print(frameCount);
    println(" shot");
  }
  e.moveBullets();
  e.display();

  // enemy moving:
  //e.posX += 1.1; //  0.1
}

// ================================================================================

class Enemy {

  final float BULLET_SPEED = 0.8;

  final float ANGLE_INCREMENT = PI/10; 
  //2PI would only shoot down, PI is up down, 
  //halfPi is in 4 dirs and so on
  //you can also just use some random number like 0.3256
  //but try keeping it within 0-2PI

  float posX, posY;

  Bullet[] bullets; //might want to use a list (ArrayList)

  Enemy (float x, float y) {
    posX = x;
    posY = y;

    bullets = new Bullet[0];
  }

  void display() {
    // enemy
    ellipse(posX, posY, 
      10, 10);

    // his bullets
    for (int i = 0; i < bullets.length; i++) {
      bullets[i].display();
    }
  }

  void shoot() {
    // make a new bullet
    Bullet newBullet = new Bullet(
      posX, posY, 
      cos(bullets.length*ANGLE_INCREMENT)*BULLET_SPEED, sin(bullets.length*ANGLE_INCREMENT)*BULLET_SPEED);
    // append it
    bullets = (Bullet[]) append(bullets, newBullet);
  }

  void moveBullets() {
    for (int i = 0; i < bullets.length; i++) {
      bullets[i].move();
    }
  }
  //
}//class

// ================================================================================

class Bullet { 
  // Bullet
  // (this is the shot enemy, but can‘t call both enemy...)
  float posX, posY;
  float dirX;
  float dirY;

  Bullet (float x, float y, 
    float dx, float dy) {
    posX = x;
    posY = y;
    dirX = dx;
    dirY = dy;
  }

  void display() {
    ellipse(posX, posY, 
      3, 3);
  }

  void move() {
    posX += dirX;
    posY += dirY;
  }
}//class
//
3 Likes

Well, i tried the Code before posting it, and it worked, so idk… although i used the iCompiler, so maybe that handels the cast automatically.

As for the Order of cos sin, is there some Special reason for that, or just to have the angle 0 start at the top?

Angle 0 is on the right side (east)

I don’t know, just in maths, cos is used for x

Oh… right, i though there was something about angle 0 starting at 3 o‘clock recently :sweat_smile: well, as long as it‘s not going to cause any problems i‘ll just use it how it works :sweat_smile:

1 Like

Thank you all for the help it’s great to know that people are very kind and helpful here, I have a bit of an understanding to how I could use trigonometry for this because I only remember SOH CAH TOA as a memory saying in school and it’s only for right triangles.

2 Likes