Advertisement
vakho

Processing N1

Nov 9th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. // Main
  2. Animal a1;
  3.  
  4. void setup() {
  5.   size(500, 400);
  6.   background(0, 0, 0);
  7.   a1 = new Animal(new PVector(3, 6));
  8.   a1.applyGravity(new PVector(0, 9));
  9. }
  10.  
  11. void draw() {
  12.   clear();
  13.   a1.update();
  14. }
  15.  
  16. void mouseClicked() {
  17.   a1.applyVecodity(new PVector(mouseX, mouseY));
  18. }
  19.  
  20. // Animal (class)
  21. class Animal {
  22.  
  23.   PVector position;
  24.   PVector speed;
  25.   PVector acceleration;  
  26.  
  27.   // TODO add mass  
  28.  
  29.   void applyVecodity(PVector mouseClickPosition) {
  30.      PVector newVelocity = mouseClickPosition.sub(position);
  31.      speed.x += newVelocity.x;
  32.      speed.y += newVelocity.y;
  33.   }
  34.  
  35.   void checkBounds() {
  36.     if (position.x > width) {
  37.       speed.x *= -1f;
  38.       position.x = width-1;
  39.     }
  40.     if (position.x < 0) {
  41.       speed.x *= -1f;
  42.       position.x = 0;
  43.     }
  44.     if (position.y > height) {
  45.       speed.y *= -1f;
  46.       position.y = height-1;
  47.     }
  48.     if (position.y < 0) {
  49.       speed.y *= -1f;
  50.       position.y = 0;
  51.     }
  52.   }
  53.  
  54.   void applyGravity(PVector g) {
  55.       acceleration.x = g.x;
  56.       acceleration.y = g.y;
  57.   }
  58.  
  59.   void update() {
  60.     checkBounds();
  61.     speed = speed.add(acceleration);
  62.     position = position.add(speed);
  63.     pushMatrix();
  64.     translate(position.x, position.y);
  65.     stroke(255, 0, 0);
  66.     ellipse(0, 0, 30, 30);
  67.     popMatrix();
  68.   }
  69.  
  70.   Animal(PVector initialSpeed) {
  71.     speed = new PVector();
  72.     acceleration = new PVector();
  73.     position = new PVector();
  74.     speed.x = initialSpeed.x;
  75.     speed.y = initialSpeed.y;
  76.   }
  77.  
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement