Advertisement
Guest User

AgentWithRotations

a guest
Jul 25th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. //DEMO
  2. Agent orange;
  3.  
  4. void setup() {
  5.   size(600, 600);
  6.   orange = new Agent();
  7. }
  8.  
  9. void draw() {
  10.   background(255);
  11.   orange.update();
  12.   orange.display();
  13.  
  14. }
  15.  
  16. void mousePressed() {
  17.   //orange.velocity.mult(1.1);
  18.   orange.turn(PI/6);
  19. }
  20.  
  21. //
  22. //Agent with rotations
  23. //
  24. class Agent {
  25.   PImage sprite;
  26.   PVector position, velocity, acceleration;
  27.   float size;
  28.   float topSpeed;  
  29.  
  30.   Agent(PVector pos, PVector vel, PVector acc, float size, float top, PImage img) {
  31.     position = pos;
  32.     velocity = vel;
  33.     acceleration = acc;
  34.     this.size = size;
  35.     topSpeed = top;
  36.     sprite = img;
  37.   }
  38.  
  39.   Agent(float x, float y) {
  40.     this(new PVector(x, y), new PVector(0.5, 0.0), new PVector(0, 0), 100, 10, null);
  41.   }
  42.  
  43.   Agent() {
  44.     this(width/2, height/2);
  45.   }
  46.  
  47.  
  48.   float getSpeed() {
  49.     return velocity.mag();
  50.   }
  51.  
  52.   /**change the speed  keep the direction*/
  53.   void setSpeed(float speed) {
  54.     velocity.normalize();
  55.     velocity.mult(speed);
  56.   }
  57.  
  58.  
  59.   void arrow() {
  60.     stroke(0);
  61.     line(0, 0, 10*velocity.mag(),0);
  62.     ellipse(10*velocity.mag(), 0, 4, 4);
  63.   }
  64.  
  65.   void display() {
  66.     //Draw things by following these steps
  67.     //1. push matrix
  68.     pushMatrix();
  69.    
  70.     //2. translate by the x,y position
  71.     translate(position.x, position.y);
  72.  
  73.     //debug text before rotate
  74.     fill(0);
  75.     text("Angle: "+degrees(getAngle())+"\nSpeed: "+getSpeed(), 80, -20);
  76.     text("Pos: "+position+" ", 80, 40);
  77.     text("Vel: "+velocity+" ", 80, 60);
  78.     text("Acc: "+acceleration+" ", 80, 80);
  79.  
  80.     //3. rotate by the -angle
  81.     rotate(getAngle());
  82.  
  83.     //4. draw the shape at the origin.
  84.     //this can be an image, or primitive shapes.
  85.     fill(255);
  86.     rectMode(CENTER);
  87.     rect(0, 0, size, size);
  88.     arrow();
  89.  
  90.     //5. popmatrix will move the screen back to
  91.     //the original coordinate system taking the
  92.     //drawn object with it!
  93.     popMatrix();
  94.   }
  95.  
  96.   void setAngle(float a) {
  97.     float mag = velocity.mag();
  98.     float x = cos(a);
  99.     float y = sin(a);
  100.     velocity = new PVector(x, y);
  101.     velocity.mult(mag);
  102.   }
  103.  
  104.   void turn(float a) {
  105.     setAngle(getAngle()+a);
  106.   }
  107.  
  108.  
  109.   float getAngle() {
  110.     return atan2(velocity.y, velocity.x);
  111.   }
  112.  
  113.   void keepInWindow() {
  114.     position.x = constrain(position.x, 0, width);
  115.     position.y = constrain(position.y, 0, height);
  116.   }
  117.  
  118.   void update() {
  119.     position.add(velocity);
  120.     velocity.add(acceleration);
  121.     velocity.limit(topSpeed);
  122.     acceleration.mult(0);
  123.     keepInWindow();
  124.   }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement