Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Cannon c;
- Mover mov;
- Ground ground;
- void setup() {
- size(640,360);
- int num = 1;
- c = new Cannon();
- ground = new Ground();
- mov = new Mover();
- mov.location.x = c.cbase;
- }
- void draw() {
- background(0);
- ground.display();
- pushMatrix();
- translate(90,height-90);
- rotate(5.7);
- if(mov.location.x < c.end){
- PVector blast = new PVector(0.009,0.0);
- mov.applyForce(blast);
- }
- if(mov.location.x > c.end){
- PVector gravity = new PVector(0,0.03);
- gravity.mult(mov.mass);
- mov.applyForce(gravity);
- // now apply friction
- PVector friction = mov.velocity.get();
- friction.normalize();
- float c = -0.01;
- friction.mult(c);
- mov.applyForce(friction);
- }
- mov.update();
- fill(255);
- stroke(0);
- stroke(255);
- strokeWeight(5);
- //draw_bits();
- c.display();
- mov.display();
- popMatrix();
- mov.checkEdges();
- noFill();
- stroke(255);
- strokeWeight(6);
- rectMode(CORNER);
- rect(0,0,width,height);
- }
- class Ground{
- Ground(){
- }
- void display(){
- rectMode(CORNER);
- fill(#A34719);
- noStroke();
- rect(0,height-25,width,30);
- }
- }
- class Cannon {
- float len;
- float wid;
- float cbase;
- float end;
- Cannon(){
- len = 200;
- wid = 80;
- cbase = -len/2;
- end = len/2;
- }
- void display(){
- fill(100,255,70,90);
- pushMatrix();
- noStroke();
- rectMode(CENTER);
- rect(0,0,len,wid);
- fill(255);
- //rect(0,0,20,20);
- popMatrix();
- }
- }
- void draw_bits(){
- fill(255,120);
- stroke(255,120);
- float dd = c.cbase;
- line(dd,0,100,0);
- ellipseMode(CENTER);
- ellipse(dd+30/2,0,30,30);
- }
- class Mover {
- PVector location;
- PVector velocity;
- PVector acceleration;
- float mass;
- float angle = 0;
- float aVelocity = 0;
- float aAcceleration = 0;
- Mover() {
- location = new PVector(0,0);
- velocity = new PVector(0,0);
- acceleration = new PVector(0,0);
- mass = 1;
- }
- void applyForce(PVector force) {
- // Newtons 2nd law with mass
- PVector f = PVector.div(force,mass);
- acceleration.add(f);
- }
- void update() {
- velocity.add(acceleration);
- location.add(velocity);
- aAcceleration = acceleration.x / 10.0;
- aVelocity += aAcceleration;
- aVelocity = constrain(aVelocity, -0.1,0.1);
- angle += aVelocity;
- acceleration.mult(0);
- }
- void display() {
- stroke(0);
- strokeWeight(2);
- noStroke();
- fill(200);
- pushMatrix();
- translate(location.x,location.y);
- rotate(angle);
- fill(255,24,90);
- rect(0,0,mass*20,mass*20);
- popMatrix();
- }
- void checkEdges() {
- if (location.x > width) {
- location.x = width;
- velocity.x *= -1;
- } else if (location.x < 0) {
- velocity.x *= -1;
- location.x = 0;
- }
- if (location.y > height) {
- velocity.y *= -1;
- location.y = height;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement