"Programming Interactivity" example problem
in
Programming Questions
•
2 years ago
I am working with the example 9-7 ball from the book "Programming Interactivity". When I run the example code I get an unexpected token type error. Now I am very new to Processing and C. This being the case my debuging ability is very limited. Any suggestions?
class Ball {
PVector location;
PVector velocity;
PVector acceleration;
float mass = 20; // how heavy are we
float maximum_velocity = 20; // use this to make sure things dont get to fast
float bounce = 1.0; // how bouncy? higer gains speed, lower loses speed
Ball(PVector initialAcceleration, PVector initialVelocity, PVector initialPosition){
acceleration = initialAcceleration.copy();
velocity.set(initialVelocity);
location.set(initialLocation);
}
Ball(){
acceleration = new PVector (0.0, 0.0, 0.0);
location = new PVector (0.0, 0.0, 0.0);
velocity = new PVector (1.0, 0.0, 0.0);
}
//Modified from original per errata notes on book
void addForce(PVector force) { //Original: void addForce(Vector3D force){
force.div(mass); // make sure the force is modified by the mass
acceleration.add(force); // the acceleration is affected by the force
}
void update(){
velocity.add(acceleration); //add acceleration to velocity
velocity.limit(maximum_velocity);
location.add(velocity);
// the acceleration all comes from the forces on the Ball which are reset
// each frame so we need to reset the acceleration to keep things within
// bounds correctly
acceleration.set(0,0f, 0.0f, 0.0f);
// bounce off walls by reversing the velocity
if(location.y > height){
velocity.y *= -bounce;
location.y = height;
}
if(location.y < 1){
velocity.y *= -bounce;
location.y = 1;
}
if((location.x > width)||(location.x <0)){
velocity.x *= -bounce;
}
// Method to display
void drawFrame(){ //ERROR unexpected token: void
update();
ellipseMode(CENTER);
noStroke();
fill(255);
ellipse(location.x, location.y, 20, 20);
}
}
Ball ball;
PVector wind;
void setup(){
size(200, 200);
smooth();
background(0);
ball = new Ball();
wind = new PVector(0.01, 0.0, 0.0);
}
void draw(){
fill(0, 10);
rect(0, 0, 200, 200);
// add gravity to thing
// this isn't "real" gravity, just a madeup vector force
PVector grav = new PVector(0, 0.5);
ball.addForce(grav);
// give Wind some random variation
float newWind = noise(mouseX, mouseY)-0.5;
wind.x += newWind;
wind.limit(1.0);
ball.addforce(wind);
ball.drawFrame();
}
1