Rotating Pac-Man with animated mouth

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.

/*
 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;
}
3 Likes

A post was split to a new topic: I made a Pac-Man game