Advertisement
Guest User

cannon

a guest
Mar 13th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.94 KB | None | 0 0
  1.     Cannon c;                                                                                                              
  2.     Mover mov;                                                                                                            
  3.     Ground ground;                                                                                                        
  4.                                                                                                                            
  5.     void setup() {                                                                                                        
  6.         size(640,360);                                                                                                    
  7.         int num = 1;                                                                                                      
  8.         c = new Cannon();                                                                                                  
  9.         ground = new Ground();                                                                                            
  10.         mov = new Mover();                                                                                                
  11.         mov.location.x = c.cbase;                                                                                          
  12.     }                                                                                                                      
  13.                                                                                                                            
  14.     void draw() {                                                                                                          
  15.         background(0);                                                                                                    
  16.                                                                                                                            
  17.         ground.display();                                                                                                  
  18.         pushMatrix();                                                                                                      
  19.         translate(90,height-90);                                                                                          
  20.         rotate(5.7);                                                                                                      
  21.         if(mov.location.x < c.end){                                                                                        
  22.             PVector blast = new PVector(0.009,0.0);                                                                        
  23.             mov.applyForce(blast);                                                                                        
  24.         }                                                                                                                  
  25.         if(mov.location.x > c.end){                                                                                        
  26.             PVector gravity = new PVector(0,0.03);                                                                        
  27.             gravity.mult(mov.mass);                                                                                        
  28.             mov.applyForce(gravity);                                                                                      
  29.                                                                                                                            
  30.             // now apply friction                                                                                          
  31.             PVector friction = mov.velocity.get();                                                                        
  32.             friction.normalize();                                                                                          
  33.                                                                                                                            
  34.             float c = -0.01;                                                                                              
  35.             friction.mult(c);                                                                                              
  36.             mov.applyForce(friction);                                                                                      
  37.                                                                                                                            
  38.         }                                                                                                                  
  39.         mov.update();                                                                                                      
  40.         fill(255);                                                                                                        
  41.         stroke(0);                                                                                                        
  42.         stroke(255);                                                                                                      
  43.         strokeWeight(5);                                                                                                  
  44.         //draw_bits();                                                                                                    
  45.         c.display();                                                                                                      
  46.         mov.display();                                                                                                    
  47.         popMatrix();                                                                                                      
  48.         mov.checkEdges();                                                                                                  
  49.         noFill();                                                                                                          
  50.         stroke(255);                                                                                                      
  51.         strokeWeight(6);                                                                                                  
  52.         rectMode(CORNER);                                                                                                  
  53.         rect(0,0,width,height);                                                                                            
  54.                                                                                                                            
  55.     }                            
  56.    
  57.     class Ground{                                                                                                          
  58.         Ground(){                                                                                                          
  59.                                                                                                                            
  60.         }                                                                                                                  
  61.         void display(){                                                                                                    
  62.             rectMode(CORNER);                                                                                              
  63.             fill(#A34719);                                                                                                
  64.             noStroke();                                                                                                    
  65.             rect(0,height-25,width,30);                                                                                    
  66.         }                                                                                                                  
  67.                                                                                                                            
  68.     }                                                                                                                      
  69.     class Cannon {                                                                                                        
  70.                                                                                                                            
  71.         float len;                                                                                                        
  72.         float wid;                                                                                                        
  73.         float cbase;                                                                                                      
  74.         float end;                                                                                                        
  75.         Cannon(){                                                                                                          
  76.             len = 200;                                                                                                    
  77.             wid = 80;                                                                                                      
  78.             cbase = -len/2;                                                                                                
  79.             end = len/2;                                                                                                  
  80.         }                                                                                                                  
  81.                                                                                                                            
  82.                                                                                                                            
  83.         void display(){                                                                                                    
  84.             fill(100,255,70,90);                                                                                          
  85.             pushMatrix();                                                                                                  
  86.             noStroke();                                                                                                    
  87.             rectMode(CENTER);                                                                                              
  88.             rect(0,0,len,wid);                                                                                            
  89.             fill(255);                                                                                                    
  90.             //rect(0,0,20,20);                                                                                            
  91.                                                                                                                            
  92.             popMatrix();                                                                                                  
  93.         }                                                                                                                  
  94.     }                                                                                                                      
  95.                                                                                                                            
  96.     void draw_bits(){                                                                                                      
  97.                                                                                                                            
  98.         fill(255,120);                                                                                                    
  99.         stroke(255,120);                                                                                                  
  100.         float dd = c.cbase;                                                                                                
  101.         line(dd,0,100,0);                                                                                                  
  102.         ellipseMode(CENTER);                                                                                              
  103.         ellipse(dd+30/2,0,30,30);                                                                                          
  104.     }                              
  105.    
  106.                                                                                                                            
  107.     class Mover {                                                                                                          
  108.                                                                                                                            
  109.         PVector location;                                                                                                  
  110.         PVector velocity;                                                                                                  
  111.         PVector acceleration;                                                                                              
  112.         float mass;                                                                                                        
  113.                                                                                                                            
  114.         float angle = 0;                                                                                                  
  115.         float aVelocity = 0;                                                                                              
  116.         float aAcceleration = 0;                                                                                          
  117.                                                                                                                            
  118.         Mover() {                                                                                                          
  119.             location = new PVector(0,0);                                                                                  
  120.             velocity = new PVector(0,0);                                                                                  
  121.             acceleration = new PVector(0,0);                                                                              
  122.             mass = 1;                                                                                                      
  123.         }                                                                                                                  
  124.                                                                                                                            
  125.         void applyForce(PVector force) {                                                                                  
  126.             // Newtons 2nd law with mass                                                                                  
  127.             PVector f = PVector.div(force,mass);                                                                          
  128.             acceleration.add(f);                                                                                          
  129.         }                                                                                                                  
  130.                                                                                                                            
  131.         void update() {                                                                                                    
  132.             velocity.add(acceleration);                                                                                    
  133.             location.add(velocity);                                                                                        
  134.                                                                                                                            
  135.             aAcceleration = acceleration.x / 10.0;                                                                        
  136.             aVelocity += aAcceleration;                                                                                    
  137.             aVelocity = constrain(aVelocity, -0.1,0.1);                                                                    
  138.             angle += aVelocity;                                                                                            
  139.                                                                                                                            
  140.             acceleration.mult(0);                                                                                          
  141.         }                                                                                                                  
  142.                                                                                                                            
  143.         void display() {                                                                                                  
  144.             stroke(0);                                                                                                    
  145.             strokeWeight(2);                                                                                              
  146.             noStroke();                                                                                                    
  147.             fill(200);                                                                                                    
  148.             pushMatrix();                                                                                                  
  149.             translate(location.x,location.y);                                                                              
  150.             rotate(angle);                                                                                                
  151.             fill(255,24,90);                                                                                              
  152.             rect(0,0,mass*20,mass*20);                                                                                    
  153.             popMatrix();                                                                                                  
  154.         }                  
  155.    
  156.         void checkEdges() {                                                                                                
  157.                                                                                                                            
  158.             if (location.x > width) {                                                                                      
  159.                 location.x = width;                                                                                        
  160.                 velocity.x *= -1;                                                                                          
  161.             } else if (location.x < 0) {                                                                                  
  162.                 velocity.x *= -1;                                                                                          
  163.                 location.x = 0;                                                                                            
  164.             }                                                                                                              
  165.                                                                                                                            
  166.             if (location.y > height) {                                                                                    
  167.                 velocity.y *= -1;                                                                                          
  168.                 location.y = height;                                                                                      
  169.             }                      
  170. }
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement