# Rotate image with PVectors for game

**URL:** <https://discourse.processing.org/t/rotate-image-with-pvectors-for-game/28203>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [March 3, 2021, 1:38am UTC](https://discourse.processing.org/t/rotate-image-with-pvectors-for-game/28203 "2021-03-03T01:38:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![boomstick55](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@boomstick55](https://discourse.processing.org/u/boomstick55)\
**Post date:** [March 3, 2021, 1:38am UTC](https://discourse.processing.org/t/rotate-image-with-pvectors-for-game/28203/1 "2021-03-03T01:38:29Z")

</div>

For a project, I am making a simple top down shooter and I’m trying to incorporate a push pop rotation to the player character. I am having trouble having the character image rotate to the mouse coordinates ala pointing a gun while moving.

calls classes used and loads in PVector and image data for the player class  
Player player = new Player();  
Screens screen = new Screens();

void setup() {  
playerSprite = loadImage(“test.png”);  
playerSprite.resize(100, 100);  
player.playerPos.set(300, 570);

class Player {  
final PVector playerPos = new PVector();  
float angle = 0;  
float targetAngle = 0;  
float easing = 0.05f;

// player image  
void displayPlayer() {

```
angle = atan2(playerPos.y - mouseY, playerPos.x - mouseX);

float dir = (angle - targetAngle) / TWO_PI;
dir -= round( dir );
dir *= TWO_PI;

targetAngle += dir * easing;

pushMatrix();
translate( playerPos.x, playerPos.y );
rotate( targetAngle );
image(playerSprite, playerPos.x, playerPos.y);
popMatrix();

```

}

---

<div class="post-metadata">

**Author:** ![boomstick55](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@boomstick55](https://discourse.processing.org/u/boomstick55)\
**Post date:** [March 3, 2021, 1:39am UTC](https://discourse.processing.org/t/rotate-image-with-pvectors-for-game/28203/2 "2021-03-03T01:39:54Z")

</div>

I can post my full code on request but Im having trouble having ONLY the player character rotate with the mouse and moving naturally in the game

---

<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:** [March 3, 2021, 8:35am UTC](https://discourse.processing.org/t/rotate-image-with-pvectors-for-game/28203/3 "2021-03-03T08:35:25Z")

</div>

your code looks good - usage of pushMatrix and popMatrix

good use of atan2

What is not working exactly?

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [March 3, 2021, 8:36am UTC](https://discourse.processing.org/t/rotate-image-with-pvectors-for-game/28203/4 "2021-03-03T08:36:07Z")

</div>

Hi @boomstick55

Welcome to our community! 🙂

Since you didn’t post the full code version it is hard to test your code. However, my guess is that you should put

```auto
image(playerSprite, 0,0);

```

Because you already translate the image to the player’s position

Hope it helps! 🙂

---

<div class="post-metadata">

**Author:** ![boomstick55](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@boomstick55](https://discourse.processing.org/u/boomstick55)\
**Post date:** [March 3, 2021, 4:25pm UTC](https://discourse.processing.org/t/rotate-image-with-pvectors-for-game/28203/5 "2021-03-03T16:25:34Z")

</div>

My sprite was flying all over the screen previously but this is what I needed!

Thank you so much!
