0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

Prosessingで近づいてくる物体から離れる物体のシミュレーションをする

Posted at

ソースコード

sim_by_processing
void setup(){
  size(1000, 600);
  background(255);
}

Object1 h = new Human(700, 500);
Object2 r = new Robot(500, 400);

void draw(){
  background(255, 255, 255, 100);
  h.move();
  r.move(h);
  r.display();
  h.display();
}
Object1
class Object1{
  float x, y, angle;
  float v, angular_v;
  float size;
  
  Object1(float x, float y){
    this.x = x;
    this.y = y;
    this.angle = 180.0;

    this.v = 1.0;
    this.angular_v = 1.0;
    
    this.size = 50;
  }
  
  void display(){
    pushMatrix();
    noStroke();
    translate(this.x, this.y);
    rotate(radians(this.angle));
    fill(0, 0, 255);
    ellipse(0, 0, this.size, this.size);
    stroke(0);
    line(0, 0, 0, 100);
    popMatrix();
  }
  
  void move(){
    if (keyPressed == true) {
      if (key == CODED) { 
        if (keyCode == UP) {
          this.x += v * -sin(radians(this.angle));
          this.y += v * cos(radians(this.angle));
        }
        if (keyCode == RIGHT) { 
          this.angle += this.angular_v;
        } 
        if (keyCode == DOWN) {
          this.x += v * sin(radians(this.angle));
          this.y += v * -cos(radians(this.angle));
        }
        if (keyCode == LEFT) {
          this.angle -= this.angular_v;
        }
      }
    }
  }
  
}
Object2
class Object2{
  float x, y, angle;
  float v, angular_v;
  float d0;
  float size;
  float t_angle;
  
  Object2(float x, float y){
    this.x = x;
    this.y = y;
    this.angle = 180.0;

    this.v = 0.0;
    this.angular_v = 1.0;
    
    this.d0 = 100;
    this.size = 50;
    this.t_angle = this.angle;
  }
  
  void display(){
    pushMatrix();
    noStroke();
    translate(this.x, this.y);
    rotate(radians(this.angle));
    fill(0, 255, 0);
    ellipse(0, 0, this.size, this.size);
    stroke(0);
    line(0, 0, 0, 100);
    popMatrix();
  }
  
  void decide1(Object1 h){
    
    float d = sqrt(sq(this.x - h.x) + sq(this.y - h.y));
    
    if(this.d0 > d){
      this.t_angle = h.angle;
      this.v = this.d0 - d ;
    }else{
      this.v = 0;
    }
    
    this.x += this.v * -sin(radians(this.angle));
    this.y += this.v * cos(radians(this.angle));
  }
  
  void decide2(Object1 h){
    float dx = this.x - h.x;
    float dy = this.y - h.y;
    float d = sqrt(sq(dx)+sq(dy));
    
    if(this.d0 > d){
      float ang = degrees(atan2(dx, dy))+180;
      this.t_angle = fixDegrees(ang);
      
      this.v = this.d0 - d;
    }else{
      this.v = 0;
    }
  }
  
  void move(Object1 h){
    decide2(h);
    
    float da = this.t_angle-this.angle;
    
    if(da<-5){
      this.angle -= this.angular_v;
    }
    else if(da>5){
      this.angle += this.angular_v;
    }
    
    this.angle = fixDegrees(this.angle);
        
    this.x += this.v * -sin(radians(this.angle));
    this.y += this.v * cos(radians(this.angle));
  }
  
  float fixDegrees(float ang){
    if(ang < -180.0){
      ang = ang+360;
    }
    if(ang > 180.0){
      ang = ang-360;
    }
    return ang;
  }
  
}

動作内容

  • 青の円をキーボードで操作できる
  • 緑の円は青の縁が近づくと、離れるように移動する

スクリーンショット 2024-07-02 20.49.41.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?