PVector follow mouse - weird jumping...
in
Programming Questions
•
8 months ago
I'm having trouble with this game program I'm making. you can control the spaceship with your mouse, and it will ease in to your mouse location. But sometimes, there is a random position jump. The default renderer is quite slow, so I'm using P2D. My target framerate is 60fps.
I'm guessing my update() function inside Spaceship class may have a problem, but it's been inconsistent. The default renderer seems fine and it's
worse on my MacBook Air than my Macbook Pro with Retina.(I don't know if this is relevant.)
I attached a video that shows this strangeness.
Any help would be appreciated!
- Spaceship player;
- int life = 10;
- int score = 0;
- ArrayList<Asteroid> asteroids;
- int numAsteroids = 70;
- boolean gameOver = false;
- void setup() {
- size(800, 600, P2D);
- frameRate(60);
- player = new Spaceship(life);
- asteroids = new ArrayList<Asteroid>();
- for (int i=0; i<numAsteroids; i++) {
- asteroids.add(new Asteroid());
- }
- mouseX = width/2;
- mouseY = height/2;
- }
- void draw() {
- background(0);
- for (int i = 0; i < asteroids.size(); i++) {
- Asteroid a = (Asteroid) asteroids.get(i);
- a.update();
- a.display();
- if (player.isHitBy(a)) {
- asteroids.remove(a);
- fill(255, 0, 0);
- rect(0, 0, width, height);
- if (player.life <= 0) {
- gameOver = true;
- }
- }
- }
- if (!gameOver) {
- player.display();
- player.update();
- }
- drawHUD();
- debug();
- }
- void keyPressed() {
- if (key == ' ') {
- //shoot a bullet
- }
- }
- void debug() {
- println("FPS: " + frameRate + " | " + "Life: " + player.life);
- }
- void drawHUD() {
- if (gameOver) {
- noStroke();
- fill(0, 150);
- rect(0, 0, width, height);
- fill(255, 0, 0);
- textSize(80);
- textAlign(CENTER);
- text("GAME OVER", width/2, height/2);
- textSize(30);
- text("Your Score is " + score, width/2, height/2+40);
- }
- else {
- fill(255, 255, 0);
- textSize(24);
- textAlign(LEFT);
- text("life: " + player.life, 30, 30);
- text("Score: " + score, 30, 50);
- }
- }
- class Asteroid {
- PVector loc = new PVector();
- PVector vel = new PVector();
- float mass;
- color c;
- float timeFactor = 1;
-
- Asteroid() {
- c = color(random(100, 150), random(100, 150), 0);
- reset();
- }
-
- void reset() {
- timeFactor += .1;
- mass = random(1, 4);
-
- float r = random(1);
-
- if (r < .25) {
- loc.x = random(-10, 0);
- loc.y = random(height);
- vel.x = timeFactor * (2/mass) * random(0.5, 2);
- }
- else if (r < .5) {
- loc.x = random(width, width+10);
- loc.y = random(height);
- vel.x = timeFactor * (2/mass) * random(-2, -0.5);
- }
- else if (r < .75) {
- loc.x = random(width);
- loc.y = random(-10, 0);
- vel.y = timeFactor * (2/mass) * random(0.5, 2);
- }
- else {
- loc.x = random(width);
- loc.y = random(height, height+10);
- vel.y = timeFactor * (2/mass) * random(-2, -0.5);
- }
- }
-
- boolean isOffscreen() {
- if (loc.x > width+10 || loc.x < -10 || loc.y > height+10 || loc.y < -10) {
- return true;
- }
- else {
- return false;
- }
- }
-
- void update() {
- vel.limit(10);
- loc.add(vel);
-
- if (this.isOffscreen() && !gameOver) {
- score += 10;
- reset();
- }
- }
-
- void display() {
- pushMatrix();
- translate(loc.x, loc.y);
- noStroke();
- fill(c);
- ellipse(0, 0, mass*10, mass*10);
- popMatrix();
- }
- }
- class Spaceship {
- PVector loc;
- PVector vel;
- PVector acc;
-
- int life;
- float topspeed = 6.0;
- float easing = 0.2;
-
- Spaceship(int _life) {
- loc = new PVector(width/2, height/2);
- vel = new PVector();
- acc = new PVector();
- life = _life;
- }
-
- boolean isHitBy(Asteroid a) {
- if (dist(loc.x, loc.y, a.loc.x, a.loc.y) < 10.0) {
- life--;
- return true;
- }
- else {
- return false;
- }
- }
-
- void update() {
- PVector mouse = new PVector(mouseX, mouseY);
- PVector dir = PVector.sub(mouse, loc);
-
- acc.add(dir);
- vel.add(acc);
- vel.limit(topspeed);
- dir.mult(easing);
- loc.add(dir);
-
- acc.mult(0);
- }
-
- void display() {
- float theta = vel.heading2D();
- pushMatrix();
- translate(loc.x, loc.y);
- rotate(theta);
- noStroke();
- fill(0, 200, 100);
- triangle(20, 0, -10, -10, -10, 10);
- popMatrix();
- }
- }
1