r/processing • • Apr 01 '21

swordsman vs gunman

214 Upvotes

28 comments sorted by

4

u/bobsyourson Apr 01 '21

That’s some pretty trick math or some pretty trick editing, either way fun to watch

5

u/createaccount314 Apr 01 '21

Thanks. It's math, I wish I knew how to edit.

3

u/bobsyourson Apr 01 '21

I wish I knew how to math 😂

4

u/blade420 Apr 01 '21

Can you explain more abt this, please ?

Are the red dots firing bullets towards the blue dot and if the blue dot touches the red dot, they die? But, does the blue dot not die due to bullets and can only avoid it? Does the blue dot successfully avoid all bullets? Very interesting.

14

u/createaccount314 Apr 01 '21

The red dots are constantly firing towards the blue dot and if the bullet touches the blue dot it dies while if the blue dot touches the red dot it slashes it and kills it. I gave one chance for the blue dot to survive the first time it is shot which is that big explosion that happens. It avoids the bullets by checking if any bullets are closer than an amount, making a vector pointing from itself to it's kill target, rotating it between -90 an 90 degrees to stay in target and scalling a random amount. Then it checks if this new place is safe and if it is it dashes to it avoiding the bullet. I can post the code here if someone wants it but it is kind of a mess and i need to refactor it a bit. Also i don't know how to post code in reddit so i need to learn that.

2

u/SomePostMan Apr 01 '21

Thanks for the explanation! I was wondering about the rules.

To post code on reddit, add four spaces at the beginning of every line. Those spaces won't be displayed, and everything past it on the line will be preserved

as-      
          is.

2

u/createaccount314 Apr 02 '21

Thanks for explaining.

Just

a

test

I'll put the code here after I smooth out the rough edges

2

u/createaccount314 Apr 02 '21

wait i'll do it again

test

2

u/createaccount314 Apr 02 '21

code

2

u/createaccount314 Apr 02 '21

It just erases the spaces. I'm probably doing somthing wrong here i'll try again tomorrow.

2

u/createaccount314 Apr 02 '21

Just one more attempt

2

u/createaccount314 Apr 02 '21

Jesus. Okay i'll sleep now. I can deal with this later

1

u/[deleted] Apr 02 '21

Cool stuff! Are there any rules for selecting its next kill target or is it currently random? Could produce some interesting side by side variations by working from the outside in, side to side or trying to find the most efficient routes.

2

u/createaccount314 Apr 02 '21

I get them random. I even tried to get the closest but the walking from one target to another one far away makes the blue dot go away from the big mass of bullets for the next target. When it searched for the closest this didn't happen and it died quickly. But I'm sure a more optimal search could be made.

16

u/Volfegan Apr 01 '21

And that's how AI will win the SkyNet wars, it can predict and dodge bullets while meatbags cannot.

2

u/patnoir96 Apr 02 '21

Just pour water on the skynet. Boom

2

u/Volfegan Apr 02 '21

Or just remove a semicolon in the right place and/or replace spacing for tabs.

9

u/[deleted] Apr 01 '21

This an anime fight scene template

1

u/createaccount314 Apr 02 '21

So here is the code. I put coments in all of it to try to explain how it works. You can change the amount of enemies, it reliably wins againt 10, somentimes againt 20 and rarely againt more than 30. The comented piece of code is what I used to record. I recorded the frames, used the movie maker and turned into an .mp4 on the internet.

void setup(){
  size(800, 600);
  sword_man = new SwordMan(new PVector(width / 2, height / 2)); //make sword man
  for(int i = 0; i < 10; i++){ gun_mans.add(new GunMan()); }    //make gun mans
}
void draw(){
  background(220);
  
  noStroke(); fill(255, 0, 0); //gunman display: no stroke, red color
  for(int i = 0; i < gun_mans.size(); i++) gun_mans.get(i).update(); //update gun mans
  
  noStroke(); fill(0, 0, 255); //swordman display: no stroke, blue color
  if(sword_man != null) sword_man.update();                          //update sword man
  
  
  if(bulletPushAnim != null) bulletPushAnim.update(); //play the bullet pushing animation
  
  noStroke(); fill(0); //bullets display: no stroke, black color
  for(int i = bullets.size() - 1; i >=0; i--) bullets.get(i).update_display(); //move and delete unecessary bullets
  
  //recording
  //if(record){ saveFrame("output/frame-######.png"); }else{ exit(); }
}
//boolean record = true;
//void mousePressed(){ record = false; }

boolean out(PVector v){ return v.x < 0 || v.y < 0 || v.x >= width || v.y >= height; } //outside canvas
float dist(PVector v0, PVector v1){ return dist(v0.x, v0.y, v1.x, v1.y); }            //dist between two PVectors

float bullet_speed = 6;
float bullet_diameter = 2;
ArrayList<Bullet> bullets = new ArrayList<Bullet>();
class Bullet{
  PVector pos, ve; //position and velocity
  Bullet(PVector pos, PVector ve){
    this.pos = pos;    //set it's initial position
    this.ve  =  ve;    //set it's initial velocity
    bullets.add(this); //add itself to the bullets arraylist
  }
  void update_display(){
    this.pos.add(this.ve); //move bullet
    circle(this.pos.x, this.pos.y, bullet_diameter); //ellipse where the bullet is
    if(out(this.pos)) bullets.remove(this);          //if the bullet is outside the canvas delete it
  }
}

float man_diameter = 14;
ArrayList<GunMan> gun_mans = new ArrayList<GunMan>();
class GunMan{
  PVector pos;
  GunMan(){
    this.pos = new PVector(random(width), random(height)); //set position at random vector
  }
  void update(){
    if(frameCount % 10 == 0){ //shoot every 10 frames
      PVector v = PVector.sub(new PVector(mouseX, mouseY), this.pos); //vector pointing from the gunman towards the mouse
      if(sword_man != null) v = PVector.sub(sword_man.pos, this.pos); //vector pointing from the gunman towards the swordman
      v.rotate(radians(random(10) - 5)); //rotate -5° to 5°
      v.setMag(bullet_speed);            //set speed to bullet speed
      
      Bullet b = new Bullet(this.pos.copy(), v); //make bullet with that speed;
    }
    
    //show the gunman
    circle(this.pos.x, this.pos.y, man_diameter);
  }
}

BulletPushAnim bulletPushAnim = null;
class BulletPushAnim{
  float diameter = 0;
  PVector center;
  BulletPushAnim(PVector center){ this.center = center; }
  void update(){
    this.diameter += 16 * bullet_speed; //grow diameter
    
    //draw itself
    noFill();
    stroke(0, 50);
    strokeWeight(3);
    ellipse(this.center.x, this.center.y, this.diameter, this.diameter);
    
    //if invisile delete
    if(this.diameter / 2 > max(width, height)) bulletPushAnim = null;
  }
}

1

u/createaccount314 Apr 02 '21

The rest of the code

float swordman_speed = 2;
float dash_size = 1.5 * man_diameter;
SwordMan sword_man;
class SwordMan{
  PVector pos, v;
  boolean should_die = false;
  SwordMan(PVector pos){ this.pos = pos; }
  void update(){
    for(int i = 0; i < bullets.size(); i++){
      float d = dist(this.pos, bullets.get(i).pos); //distance from swordman to the bullet
      if(d >= man_diameter / 2) continue; //the bullet isn't close so there is nothing to be done
      
      //bullet is close
      if(this.should_die){ //kill it
        println("sword man died");
        sword_man = null;
        return;
      }
      
      //swordman survives this time
      this.do_bulletPush();
      this.should_die = true; //won't survive the next
      return;
    }
    
    if(gun_mans.size() == 0){ //there is no gunman left
      noStroke();
      fill(0, 0, 255);
      circle(this.pos.x, this.pos.y, man_diameter); //show and return
      return;
    }
    
    GunMan target2kill = gun_mans.get(0); //target to kill
    
    //move towards target
    this.v = PVector.sub(target2kill.pos, this.pos); //vector from swordman pointing towards target
    this.v.setMag(swordman_speed);                   //set magnitude to the swordman speed
    this.pos.add(this.v);                            //move
    
    if(this.close2(this.pos, dash_size)){ //bullet close
      this.dash(target2kill);   //dash
    }
    
    if(dist(this.pos, target2kill.pos) < man_diameter){ //reached the target
      this.slash_gunMan(target2kill); //kill it
    }
    
    //display itself
    noStroke();
    fill(0, 0, 255);
    circle(this.pos.x, this.pos.y, man_diameter);
  }
  void do_bulletPush(){
    for(int j = 0; j < bullets.size(); j++){
      PVector new_v = PVector.sub(bullets.get(j).pos, this.pos); //vector pointing from the swordman towards the bullet
      new_v.setMag(bullet_speed);                                //set it's magnitude as bullet_speed
      bullets.get(j).ve = new_v;                                 //set the bullet speed
    }
    bulletPushAnim = new BulletPushAnim(this.pos.copy());        //create the bullet push animation
  }
  void dash(GunMan target2kill){
    PVector
      dash_v = null,      //the dash vector
      test_pos = null;    //the position after dashing to test if it is a good one
    int max_tries = 1000; //maximun amount of tries to not stay in an unending loop
    
    for(int i = 0; i < max_tries; i++){ //for instead of while to not stay stucked in a loop
      dash_v = PVector.sub(target2kill.pos, this.pos);  //vector pointing from swordman to target
      dash_v.rotate(radians(random(-90, 90)));          //rotate between -90° an 90° to stay into target direction
      dash_v.setMag(random(man_diameter, 2 * man_diameter)); //set a random size
      test_pos = PVector.add(this.pos, dash_v);              //add to the position
      
      if(!this.close2(test_pos, dash_size)) break; //if the position will be good get out of loop
      if(i == max_tries - 1) dash_v = PVector.random2D().setMag(dash_size); //if we excede attempt limit get a random dash
    }
    PVector last_pos = this.pos.copy(); //store current position
    this.pos.add(dash_v); //move dash amount
    
    //update v
    this.v = PVector.sub(target2kill.pos, this.pos); //vector from swordman pointing towards target
    this.v.setMag(swordman_speed);                   //set magnitude to the swordman speed
    
    //draw dash
    stroke(0, 0, 255, 100); 
    strokeWeight(man_diameter / 2);
    line(last_pos.x, last_pos.y, this.pos.x, this.pos.y); //use stored position
    strokeWeight(1);
    noStroke();
  }
  void slash_gunMan(GunMan target2kill){
    println("sword man slashed");
    
    //draw slash
    stroke(0, 0, 255);
    PVector slash_v0 = PVector.random2D().mult(man_diameter / 2 + 10); //random vector
    PVector slash_v1 = slash_v0.copy().mult(-1);                       //oposite of slash_v0
    slash_v0.add(target2kill.pos); slash_v1.add(target2kill.pos);      //put the vectors around the target
    line(slash_v0.x, slash_v0.y, slash_v1.x, slash_v1.y);              //draw a line that goes through the center of the target
    
    //kill gunman
    gun_mans.remove(target2kill);
  }
  boolean close2(PVector v, float radius){ //if any bullet is at distance radius from v
    for(int i = 0; i < bullets.size(); i++) if(dist(v, bullets.get(i).pos) < radius) return true;
    return false;
  }
}

1

u/DinoRex6 Apr 01 '21

Is this inspired by the berd video?

2

u/createaccount314 Apr 01 '21

A bit. The colors were for sure.

1

u/[deleted] Apr 01 '21

Mesmerising!

1

u/Wotsmenameagain Apr 14 '21

Add code to stop the swords man from leaving the frame and it’s perfect.