# How do I make a sprite rotate to face another sprite?

**URL:** https://discourse.processing.org/t/how-do-i-make-a-sprite-rotate-to-face-another-sprite/22266
**Category:** Coding Questions
**Created:** [June 30, 2020, 12:08am UTC](https://discourse.processing.org/t/how-do-i-make-a-sprite-rotate-to-face-another-sprite/22266 "2020-06-30T00:08:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![The99thTroll](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@The99thTroll](https://discourse.processing.org/u/The99thTroll)
#### Post date: [June 30, 2020, 12:08am UTC](https://discourse.processing.org/t/how-do-i-make-a-sprite-rotate-to-face-another-sprite/22266/1 "2020-06-30T00:08:04Z")

</div>

I’m working on a tower defense game but I am unable to find a way for my towers to rotate to look at the current enemy they’re targeting. Any solutions?

---

<div class="post-metadata">

### Author: ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)
#### Post date: [June 30, 2020, 12:12am UTC](https://discourse.processing.org/t/how-do-i-make-a-sprite-rotate-to-face-another-sprite/22266/2 "2020-06-30T00:12:12Z")

</div>

Have you tried using the [`atan2()`](https://p5js.org/reference/#/p5/atan2) function?

---

<div class="post-metadata">

### Author: ![The99thTroll](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@The99thTroll](https://discourse.processing.org/u/The99thTroll)
#### Post date: [June 30, 2020, 12:42am UTC](https://discourse.processing.org/t/how-do-i-make-a-sprite-rotate-to-face-another-sprite/22266/3 "2020-06-30T00:42:02Z")

</div>

I looked at it but uh, I don’t understand it.

---

<div class="post-metadata">

### Author: ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)
#### Post date: [June 30, 2020, 1:20am UTC](https://discourse.processing.org/t/how-do-i-make-a-sprite-rotate-to-face-another-sprite/22266/4 "2020-06-30T01:20:16Z")

</div>

You use the `atan2()` function to find the angle of rotation for the tower. In the sample code below, in the `atan2()` line, the arguments `ex-height/2` and `ey-width/2` represent the position of the enemy relative to the tower:

```auto
function setup() {
  createCanvas(400, 400);
}

// position variables for enemy
ex = 150; ey = 0;

function draw() {
  // fill & stroke properties for enemy and tower
  background(255);
  noStroke();
  fill(0);
  // enemy
  circle(ex, ey, 20);
  ex ++; ey ++;
  // tower
  translate(width/2, height/2);
  a = atan2(ex-width/2, ey-height/2);
  rotate(a*-1);
  triangle(-10,0, 0,20, 10,0);
  // draw green beam
  fill('#00FF00');
  rect(0, 0, 1, 500);
}

```

You’ll need to adapt this code to work with your program.
