#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# NOC 3.2 - My crap cannon

edited March 2015

I've just been doing the cannon from Ex 3.2 and I'm not sure where to go from here, and I'm starting to think I might have approached the whole thing wrong.

I started by making the cannon, and rotating the canvas so that the cannon would be pointing in a roughly 45 degree from the bottom left to the top right.

Then I made it so that a square started at the base of the cannon, and while it's x coordinate was within the domain of the cannon I applied a force to the square that propelled it out of the cannon.

Once the square had left the cannon I applied gravity to the square, that makes it fall towards the floor.

I'm struggling though because I wanted the square to bounce against the far edge of the screen and the bottom of the screen, but I'm not sure how to go about that given the rotations that I created to draw the cannon and that.

So, any guidance appreciated!

Cheers.

here's a link to the code ( I couldn't fit it in)

Tagged:

• edited March 2015 Answer ✓

Nice job on making the rectangle bounce - it works, but you have to change how things are displayed a little bit: Here's a hint: in you draw() method, move translate(), pushMatrix() and popMatrix() so that they only affect the cannon:

``````   void draw() {
background(0);
ground.display();
//------------------------------------------------------
//pushMatrix, translate, was originally here:
//-----------------------------------------------------
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);

//I moved pushMatrix(), translate(), and popMatrix() here:
//--------------------------------
pushMatrix();
translate(90, height-90);
rotate(5.7);
//draw_bits();
c.display();
popMatrix();
//---------------------------------
mov.display();
mov.checkEdges();
noFill();
stroke(255);
strokeWeight(6);
rectMode(CORNER);
rect(0, 0, width, height);
}
``````

Run it, and you should see the problem: your projectile doesn't actually know where it is! The bouncing will now work, but the cannon shot is messed up. It's generally a good idea to only apply translate() to individual elements, rather than the whole sketch so problems like this don't happen. It's what pushMatrix() and popMatrix() are for!

Now lets make the cannon actually fire correctly. Change the initial location of the mover so it starts inside the cannon: `location = new PVector(90, height-20);` and change your blast so that it has both x and y values: `PVector blast = new PVector(0.009, -0.009);` This looks a little funny still, but it will get you started