# Obstacles in game not working properly

**URL:** <https://discourse.processing.org/t/obstacles-in-game-not-working-properly/25329>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [November 10, 2020, 8:08pm UTC](https://discourse.processing.org/t/obstacles-in-game-not-working-properly/25329 "2020-11-10T20:08:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ninjarick](https://avatars.discourse-cdn.com/v4/letter/n/5fc32e/32.png) [@ninjarick](https://discourse.processing.org/u/ninjarick)\
**Post date:** [November 10, 2020, 8:08pm UTC](https://discourse.processing.org/t/obstacles-in-game-not-working-properly/25329/1 "2020-11-10T20:08:14Z")

</div>

Hi there!  
I’ve hit a snag in my code. I want to be able to make a game where the player (a rect) jumps over obstacles. Similar to the no wifi Dino game. I’ve added code to make the player character flip when they jump.

```auto
 show() {
    angleMode(DEGREES);
    rectMode(CENTER);

    noStroke();
    fill(150);

    // Rotate player when jumping
    if (this.y < height - 25) {
      translate(this.x, this.y);
      rotate(this.angle);
      this.angle = this.angle + 7;
      rect(0, 0, this.s, this.s);
    } else {
      rect(this.x, this.y, this.s, this.s);
    }
  }

```

However, as the player jumps, the obstacles rotate along with the player while moving. I want the obstacles to simply move across the screen. My question is, how can I make the obstacles move toward the player without rotating when the player jumps?  
Full code here:

```auto
let player;
let obstacle = [];

function setup() {
  createCanvas(900, 500);

  player = new Player();
}

// Press Space to Jump
function keyPressed() {
  if (key == ' ') {
    player.jumpSp();
  }
}

function draw() {
  
   if (random(1) < 0.01){
    obstacle.push(new Obstacle());
  }
  
  background(220);
  player.show();
  player.jump();
  
  for(let obs of obstacle){
    obs.move(); 
    obs.show();
  }
  
 
}

// Player properties

class Player {
  constructor() {
    this.s = 50;
    this.x = 50;
    this.y = height - 25;
    this.j = 0;
    this.gravity = 1;
    this.angle = 0;

  }
  
  // Jump Speed
  jumpSp() {
    this.j = -15;
  }
  
  // Jump movement
  // Gives player gravity
  // Keeps player from leaving canvas
  jump() {
    this.y += this.j;
    this.j += this.gravity
    this.y = constrain(this.y, 25, height - 25)

  }

  show() {
    angleMode(DEGREES);
    rectMode(CENTER);

    noStroke();
    fill(150);

    // Rotate player when jumping
    if (this.y < height - 25) {
      translate(this.x, this.y);
      rotate(this.angle);
      this.angle = this.angle + 7;
      rect(0, 0, this.s, this.s);
    } else {
      rect(this.x, this.y, this.s, this.s);
    }
  }
}

// Obstacle properties
class Obstacle {
  
  constructor(){
    this.w = 50;
    this.h = 70;
    this.x = width;
    this.y = height - this.h;
  }
  
  move(){
    this.x -= 5;
  }
  
  show(){
    angleMode(RADIUS);
    rectMode(CORNER);
    noStroke();
    fill(0);
    rect(this.x,this.y,this.w,this.h);
  }
  
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 10, 2020, 9:38pm UTC](https://discourse.processing.org/t/obstacles-in-game-not-working-properly/25329/2 "2020-11-10T21:38:55Z")

</div>

> [@ninjarick](#):
>
> ```auto
> translate(this.x, this.y);
> rotate(this.angle);
> 
> ```

This stuff is the rotating bit.

This stuff makes changes onto the Matrix (the underlying coordinate system).

They add up (or just stay) which you don’t want.

**What to do**

To _isolate_ the move of one shape (player) against the others (obstacles) use `push()` and `pop()`:

Read the comments here:

```auto
      push(); // save Matrix as it is behind the scenes !!!!!!!!!!!!!!!!!!!!!!
      translate(this.x, this.y);
      rotate(this.angle);
      this.angle = this.angle + 7;
      rect(0, 0, this.s, this.s); // this is the Player
      pop(); // restore the old Matrix (without the translate and rotate) !!!!!!!!!!!!!!!!!!!!!!!!!!!!
      // here the translate and rotate is not having effect anymore because of pop()

```

Hey, and welcome to the processing forum!

Great to have you here with us!!!

Warm regards,

Chrisir

---

<div class="post-metadata">

**Author:** ![ninjarick](https://avatars.discourse-cdn.com/v4/letter/n/5fc32e/32.png) [@ninjarick](https://discourse.processing.org/u/ninjarick)\
**Post date:** [November 10, 2020, 9:56pm UTC](https://discourse.processing.org/t/obstacles-in-game-not-working-properly/25329/3 "2020-11-10T21:56:15Z")

</div>

Thank you so much for your help!!! Got it all fixed up now!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 10, 2020, 9:56pm UTC](https://discourse.processing.org/t/obstacles-in-game-not-working-properly/25329/4 "2020-11-10T21:56:57Z")

</div>

You are a Ninja now!

Congrats!
