Adding in acceleration and momentum
in
Programming Questions
•
2 months ago
Hello! I've been working on this program in which a rectangle is directed about the screen using the WASD keys (or arrow keys... I'm not done yet) , and I'm working on having the "front" of the rectangle always be the front, so that moving "forward" causes it to travel forward, even if the rectangle is rotated. Is this possible, and if so, could someone direct me to where I could learn? Thanks!
- boolean left, right, backward, forward;
- boolean SOOPASPEED;
- int fullspeed = 6;
- int regspeed = 1;
- PVector pos = new PVector(500, 300);
- void setup() {
- size(displayWidth, displayHeight, P2D);
- frameRate(300);
- noCursor();
- }
- void draw() {
- if (left) {
- if (SOOPASPEED) { pos.x -= fullspeed; }
- else { pos.x -= regspeed; }
- }
- if (right) {
- if (SOOPASPEED) { pos.x += fullspeed; }
- else { pos.x += regspeed; }
- }
- if (backward) {
- if (SOOPASPEED) { pos.y += fullspeed; }
- else { pos.y += regspeed; }
- }
- if (forward) {
- if (SOOPASPEED) { pos.y -= fullspeed; }
- else { pos.y -= regspeed; }
- }
- if (pos.x>width){pos.x=0; }
- if (pos.x<0){pos.x=width; }
- if (pos.y>height){pos.y=0;}
- if (pos.y<0){pos.y=height;}
- stroke(255);
- fill(255);
- background(0);
- rectMode(CENTER);
- rect(pos.x, pos.y, 50, 90);
- }
- void keyPressed() {
- if (key == 'a' || key == 'A') { left = true; }
- if (key == 'd' || key == 'D') { right = true; }
- if (key == 's' || key == 'S') { backward = true; }
- if (key == 'w' || key == 'W') { forward = true; }
- if(key == CODED) {
- if(keyCode == SHIFT) { SOOPASPEED = true; }
- }
- }
- void keyReleased() {
- if (key == 'a' || key == 'A') { left = false; }
- if (key == 'd' || key == 'D') { right = false; }
- if (key == 's' || key == 'S') { backward = false; }
- if (key == 'w' || key == 'W') { forward = false; }
- if(key == CODED) {
- if(keyCode == SHIFT) { SOOPASPEED = false; }
- }
- }
- boolean sketchFullScreen(){
- return true;
- }
[edit: formatting]
1