Open chasestarr opened 8 years ago
Take a look at Example 1.9 from The Nature of Code Chapter 1 which talks about randomness within acceleration vectors.
Example 1.9: Motion 101 (velocity and random acceleration)
void update() {
//The random2D() function will give us a PVector of length 1 pointing in a random direction.
acceleration = PVector.random2D();
velocity.add(acceleration);
velocity.limit(topspeed);
location.add(velocity);
}
Because the random vector is a normalized one, we can try scaling it:
acceleration = PVector.random2D();
//Constant
acceleration.mult(0.5);
acceleration = PVector.random2D();
//Random
acceleration.mult(random(2));
While this may seem like an obvious point, it’s crucial to understand that acceleration does not merely refer to the speeding up or slowing down of a moving object, but rather any change in velocity in either magnitude or direction. Acceleration is used to steer an object, and we’ll see this again and again in future chapters as we begin to program objects that make decisions about how to move about the screen.
Take a look at the examples for help.
Tab one
Mover armadillo;
void setup(){
size(500,500);
background(255);
armadillo = new Mover();
}
void draw(){
PVector mouse = new PVector(mouseX, mouseY);
PVector center = new PVector(width/2, height/2);
mouse.sub(center);
float magnitude = mouse.mag();
rect(0, 0, magnitude, 50);
translate(center.x, center.y);
line(0, 0, mouse.x, mouse.y);
background(255);
armadillo.display();
armadillo.update();
armadillo.checkEdges();
}
Tab two
class Mover{
PVector location;
PVector velocity;
PVector acceleration;
Mover(){
location = new PVector(random(width),random(height));
velocity = new PVector(random(-2.5,2.5),random(-2.5,2.5));
acceleration = new PVector(0.015,-0.015);
}
void display(){
stroke(0);
fill(150,150,150);
ellipse(location.x,location.y,25,25);
}
void update(){
velocity.add(acceleration);
location.add(velocity);
}
void checkEdges(){
if(location.x > width){
location.x = 0;
} else if(location.x < 0){
location.x = width;
}
if(location.y > height){
location.y = 0;
} else if(location.y < 0){
location.y = height;
}
}
}
Sorry mister Chase I forgot a lot of the stuff because I did not do the homework until the last day. I am not asking for a total recap, but the information you have given me does not seem too work on processing. It always says something is done improperly( for instance it says the size is the cause of the problems most of the time).
Try going through example 1.9 at this page: http://natureofcode.com/book/chapter-1-vectors/ The code from that section will go in your Mover class and then in the update function. You may need to look at the rest of that web page for more information :smile:
Helico heli;
void setup(){ heli = new Helico(); size(400,400);
} void draw(){ background(255); heli.edges(); heli.display(); heli.update(); }
class Helico{ PVector location; PVector velocity; PVector acceleration;
Helico(){ location = new PVector(random(width),random(height)); velocity = new PVector(random(-2,2),random(-2,2)); acceleration = new PVector(0.01,-0.01); }
void display(){ stroke(0); fill(200,200,0); rect(location.x,location.y,9,65); fill(0); rect(location.x,location.y,9,10); fill(255); ellipse(location.x+4,location.y+27,30,30);
}
void update(){ acceleration = PVector.random2D(); acceleration.mult(random(2));
velocity.add(acceleration); velocity.limit(2); location.add(velocity); }
void edges(){ if(location.x > width){ location.x = 0; } else if(location.x < 0){ location.x = width; }
if(location.y > height){
location.y = 0;
} else if(location.y < 0){
location.y = height;
}
} }
In this homework we will get some more practice with vectors. Look back on your homework from week13 for help.
Please paste your code as a comment on this post