-5

I'm trying to make a two yellow pacman (one open mouth and one plain) and also a ghost but I'm quite noob with openGL.

Could someone give me code to make them?

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
b.allain732
  • 9
  • 1
  • 4
  • 1
    Please take a "[Tour](http://stackoverflow.com/tour)" in order to understand how SO works. Questions like "Give me the solution to something" are most likely not answered. Please, show us what you have tried so far und point out your problem. – Peter Mar 29 '16 at 09:11
  • Thanks Peter for your answer. I would ask if someone had already made this characters and if he could give it to me. I'm unfortunatelly too bad with openGL to made it myself. – b.allain732 Mar 29 '16 at 09:22

1 Answers1

3

OpenGL is simply a way to draw things. You need to figure out what you want to draw. A pacman is a reasonably simple shape though: it's just a filled arc.

I assume you already know how to draw a triangle. A circle is essentially a set of triangles with a common middle point:

circle with triangles

If you divide it into enough triangles, it will appear completely smooth on the screen. The optimal number will vary depending on your pacman's size.

Now in order to make the "mouth", simply remove some vertices at one place. A pacman's mouth forms a characteristic opening, which is aligned at the direction it's moving and opens at "both ends":

pacman shape

Now, assuming you draw the triangles using a simple loop (pseudocode):

for (i : (0, 360)) {
    rotate(i /* degrees */);  // ---------- see remark 1.
    drawTriangle();
}

To make the opening animation you just need to subtract the current width divided by half from both ends (not that hard, look!):

mouthOpenPercentage = 0.5; // varies from 0 to 1
fullyOpenMouthSize = 0.1; // 10% of the circle, so around 36 deg
numberOfSegments = 360;
mouthOpenSegments = fullyOpenMouthSize * mouthOpenPercentage * numberOfSegments / 2;

for (i : (mouthOpenSegments, numberOfSegments - mouthOpenSegments)) {
    rotate(i /* degrees */);
    drawTriangle();
}

If it comes out e.g. on top instead of right, simply rotate first by the desired angle to "point" it in the direction you want.


1. I assumed that rotate means "absolute rotation". Bear that in mind.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
  • Thanks a lot Bartek for your answer. Unfortunately It's quite hard for me to draw a simple circle with openGL.. – b.allain732 Mar 29 '16 at 14:00
  • @b.allain732 Then perhaps try to find some resources about basic geometry and trigonometry first, as without the foundation knowledge it will be hard for you to progress. – Bartek Banachewicz Mar 30 '16 at 13:53