# Rotating Pac-Man with animated mouth

**URL:** <https://discourse.processing.org/t/rotating-pac-man-with-animated-mouth/45395>\
**Category:** Gallery\
**Created:** [November 27, 2024, 1:52pm UTC](https://discourse.processing.org/t/rotating-pac-man-with-animated-mouth/45395 "2024-11-27T13:52:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [November 27, 2024, 1:52pm UTC](https://discourse.processing.org/t/rotating-pac-man-with-animated-mouth/45395/1 "2024-11-27T13:52:40Z")

</div>

Combining the mouth animation together with rotation of the pac-man is an interesting problem with lots of potential solutions so here one way to do it.

 ![Screenshot 2024-11-27 at 11.49.37](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/b/3b3bc996dae7001d0036d0ff86fde0ec7679dd7b.png)

```processing
/*
 Project: Waka Waka
 Author: quark
 Date: 2024-11-27
 Version: 1.0.0
 */

void setup() {
  size(300, 300);
  noStroke();
  textAlign(CENTER, CENTER);
}

// msa - used to provide cyclic factor to control mouth opening
// dmsa - amount to change msa each frame, controls mouth speed
// msf - a cyclic value in the range +0 to +1 inclusive
// dir the diration the pacman faces
float dir = 0, msf = 0, msa = 0, dmsa = 0.08;

void draw() {
  background(0);
  noStroke();
  fill(255);
  textSize(24);
  text("PacMan rotates towards mouse position", 0, 0, width, 60);
  textSize(16);
  translate(width/2, height/2);
  msa = normAngle(msa + dmsa);
  msf = (1 + cos(msa))/2; // Range 0- 1
  drawPacMan(0, 0, dir, msf);
}

void mouseMoved() {
  dir = atan2(mouseY-height/2, mouseX-width/2);
}

void drawPacMan(float x, float y, float dir, float mouthSize) {
  float maxMouthSize = radians(70);
  float diam = 120, ma = mouthSize * maxMouthSize, hma = 0.5*ma;
  push();
  translate(x, y);
  rotate(dir);
  noStroke();
  fill(255);
  text("WAKA", 10, -15, diam/2, 30);
  fill(255, 255, 0);
  arc(0, 0, diam, diam, hma, TAU-hma, PIE);
  rotate(-hma);
  pop();
}

float normAngle(float a) {
  while (a < 0) a += TAU;
  while (a >= TAU) a-= TAU;
  return a;
}

```

---

<div class="post-metadata">

**Author:** ![ProcessingOrg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/processingorg/32/18341_2.png) [@ProcessingOrg](https://discourse.processing.org/u/ProcessingOrg)\
**Post date:** [December 4, 2024, 8:28pm UTC](https://discourse.processing.org/t/rotating-pac-man-with-animated-mouth/45395/2 "2024-12-04T20:28:11Z")

</div>

A post was split to a new topic: [I made a Pac-Man game](https://discourse.processing.org/t/i-made-a-pac-man-game/45431)
