/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.JH2uE6$ETgK/rev.7
*
* authors:
* GoToLoop
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
/**
* Drivable Car (v2.11)
* by TFGuy44 (2013/Aug)
* mod GoToLoop
*
* http://forum.processing.org/topic/adding-in-acceleration-and-momentum
* http://studio.processingtogether.com/sp/pad/export/ro.9Nl$898UQxW3Q/latest
*/
static int gw, gh;
static float x, y, z, v;
static boolean north, south, west, east;
final static float SPD = .2, ROT = .1, ACCEL = .99;
void setup() {
size(640, 480);
smooth();
gw = width;
gh = height;
x = gw>>1;
y = gh>>1;
}
void draw() {
background(0300);
update();
display();
}
void keyPressed() {
setKeys(keyCode, true);
}
void keyReleased() {
setKeys(keyCode, false);
}
static final void setKeys(int k, boolean decision) {
if (k == UP | k == 'W') north = decision;
else if (k == DOWN | k == 'S') south = decision;
else if (k == LEFT | k == 'A') west = decision;
else if (k == RIGHT | k == 'D') east = decision;
}
static final void update() {
v += (north? SPD : 0) - (south? SPD : 0);
z += (east? ROT : 0) - (west? ROT : 0);
x = (x + gw + cos(z)*v) % gw;
y = (y + gh + sin(z)*v) % gh;
v *= ACCEL;
}
void display() {
translate(x, y);
rotate(z);
noStroke();
fill(#008000);
rect(-10, -10, 20, 20);
stroke(0);
noFill();
triangle(-8, -8, 7, 0, -8, 8);
}