Spaceship issues
in
Programming Questions
•
5 months ago
Hello,
I have created a little rocket flying around a la metroid (from Shiffmans examples).
My problem is when rotating and thrusting the ship, it seems to continue do so until maxspeed is reached, or another rotation direction is added. I want the ship to stop turning and thrusting when I release the arrow keys.
In the update(), there is an acceleration.mult(0), that should stop any more acceleration. What am I missing?
Best,
David
Ship ship;
void setup() {
size(299, 299);
background(255);
smooth();
ship = new Ship(50,50);
}
void draw() {
background(255);
noStroke();
fill(0);
ship.keyPressed();
ship.update();
ship.display();
}
/////////////////////////
class Ship {
PVector location;
PVector velocity;
PVector acceleration;
float resistance;
float angle;
float heading;
// float r = 15;
// float theta = 0;
// float x = r * cos(theta);
// float y = r * sin(theta);
Ship(float _x, float _y) {
location= new PVector(_x, _y);
velocity= new PVector(0, 0);
acceleration= new PVector(0, 0);
heading = 0;
}
void update() {
//Deployment
velocity.add(acceleration);
velocity.limit(3);
location.add(velocity);
checkEdges();
//Incrementing/resetting before next frame
acceleration.mult(0);
}
void turn(String direction) {
if (direction=="right") {
heading+=0.05;
}
else if (direction=="left") {
heading-=0.05;
}
else if (direction=="straight") {
heading-=0;
}
}
void thrust() {
float angle = heading-PI/2;
PVector force = new PVector(cos(angle), sin(angle));
force.mult(0.1);
applyForce(force);
}
void applyForce(PVector force) {
PVector f=force.get();
acceleration.add(f);
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
thrust();
}
else if (keyCode == RIGHT) {
turn("right");
}
else if (keyCode == LEFT) {
turn("left");
}
else {
turn("straight");
}
}
}
void checkEdges() {
if (location.x > width-8) {
location.x -= width;
}
else if (location.x < 8) {
location.x += width;
}
if (location.y > height-8) {
location.y -= height;
}
else if (location.y < 8) {
location.y += height;
}
}
void display() {
stroke(0);
fill(174);
pushMatrix();
translate(location.x, location.y);
rotate(heading);
triangle(-10, 10, 10, 10, 0, -30 );
rectMode(CENTER);
popMatrix();
}
}
1