#### Howdy, Stranger!

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

# Daniel Shiffman's Nature of Code Exercise 1.5

edited May 2016

Hi there,

I am going through Daniel's book, unable to find the solution of this exercise online, I am very curious to know if my solution for this one is correct or there are better ways to achieve the same result.

Exercise 1.5: create a simulation of a car that accelerates when you press the up key and breakes when you press the down key.

Here's the code of my solution:

``````Mover mover; // declare my car

void setup() {
size (640, 360);
mover = new Mover(); // initialize my car
}

void draw() {
background(255);
mover.update();
//mover.bounce();
mover.checkEdges();
mover.display();
println(mover.velocity.x);

if (keyPressed == true) {
if (keyCode == UP) {
//println("UP!");
} else if (keyCode == DOWN) {
//println("DOWN!");
if (mover.velocity.x > 0 || mover.velocity.y > 0){
mover.accelleration.sub(mover.gas);
println("BREAKING");
} else {
println("STOOP!");
mover.hasstopped();
}
}
}

}

// my Mover Class

class Mover {
//float x, y;
PVector location;
//float xspeed, yspeed;
PVector velocity;

PVector accelleration;
float topspeed; // a varoable to limit the magnitude of velocity
PVector gas;

Mover() {
location = new PVector(width/2, height/2); // location vector describes where the object is
velocity = new PVector(0, 0); // velocity vector describes changes in location over time
accelleration = new PVector(0, 0);
gas = new PVector(0.001, 0.001);
topspeed = 10;
}

void update() {
velocity.limit(topspeed);
}

void bounce() {
if (location.x > width || location.x < 0) {
velocity.x *= -1;
}
if (location.y > height || location.y < 0) {
velocity.y *= -1;
}
}

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;
}
}

void display() {
stroke(0);
fill(150);
ellipse(location.x, location.y, 20, 20);
//rectMode(CENTER);
//rect(location.x, location.y, 20, 20);
}

void hasstopped() {
mover.accelleration.x = 0;
mover.accelleration.y = 0;
mover.velocity.x = 0;
mover.velocity.y = 0;
}
}
``````

Thanks!

Tagged:

when checking a boolean, == true is unnecessary

println should be removed when you're done

line 44 spelling error

mover is the constructor. it maybe wise to try with parameters. so you can have multiple cars starting in different lanes, try an array of cars instead of only one mover

lines 63 / 66 can lead to stuttering - hm, better use abs(velocity.y ---- and -abs(velocity.y

• Great work! One thing you might consider is what happens if the car turns? In this case, you can dynamically make the vector that slows down the car by multiplying by -1. You'll see in chapter 2 how the concept of forces allows you to improve on this as well.

• Thank you for all your comments, much appreciated! Shiffman > I am looking forward for posting more solutions to Nature of Code exercises. It would be great to build a repository for others students to see. Chris > very detailed feedback thank you! GoToLoop > I will study your code, thanks for sharing (I, didn't know about http://studio.processingtogether.com/ Have to dig into it as well ;)

• I have noticed I have problems constraining the object movement inside the sketch space. I thought the following function could do the job. But I was wrong as my object ends up out of the stage even if I can't figure out why…

Any hints?

```void bounce() { if (location.x > width || location.x < 0) { velocity.x *= -1; } if (location.y > height || location.y < 0) { velocity.y *= -1; } }```

• The * -1 can lead to stuttering when we are after the bouncing still in the return zone

The condition is still true, thus *- 1 is applied again (falsely)

It switches between neg & pos, we add it to the car and it stutters

To avoid this use abs() which gives you the amount but always pos

Thus when you are on the left side of the screen say abs(); on the right side - abs() (which is -1 * abs())

velocity.y=abs(velocity.y);

• Not sure If I understood correctly how abs function will do the job… Chris, do you mean something like this?

```void bounce() { if (location.x < 0) { velocity.x = abs(velocity.x); } if (location.x > width){ velocity.x = -abs(velocity.x); } if (location.y < 0) { velocity.y = abs(velocity.y); } if (location.y > height){ velocity.y = -abs(velocity.y); } }```

That does not seem to solve the problem anyway. :(

• That's what I meant.

Although you dont check with width and height of the car

Is it because of checkEdges???

• nope; checkEdges() is an alternative function for having an alternative behavior; I have commented out, so the only call I have is to bounce(). Yet, even with the code amends, the object ends out of the sketch… :(

• Did you add car width / height to location?

• Line 12 is commented out?

• No, sorry for the misunderstanding, the out of sketch problem happens when I test the sketch using the bounce function (and commenting out the checkEdges function)

like this:

```mover.bounce(); //mover.checkEdges();```

infact the amends I have done above with abs(), as you suggested, refers only to the function bounce(). Problem is sometimes the object pass the "boundaries" of the sketch and gets lost.

• use println to find out what happens when it fails