Figuring out the geometry

The CCDrawNode class provides just three functions. These functions are drawDot, drawSegment, and drawPolygon, which enable you to draw a color-filled circle, a color-filled segment, and a color-filled polygon with a separate border, respectively. We will write a simple yet extremely resourceful function to help us with the generation of vertices for our game elements. However, before we write this utilitarian function, let's take a closer look at each element and the vertices they contain:

Game element

Visual

Visual with vertices

Player

Figuring out the geometry
Figuring out the geometry

Enemy

Figuring out the geometry
Figuring out the geometry

Shield

Figuring out the geometry
Figuring out the geometry

Bomb

Figuring out the geometry
Figuring out the geometry

Missile launcher

Figuring out the geometry
Figuring out the geometry

I have highlighted the vertices on each of these shapes to underline a basic geometrical object that we will use to our advantage in this game—the regular polygon. As most of you may know, a regular polygon is a polygon that is equiangular and equilateral. The interesting thing about a regular polygon is that we can use the parametric equation of a circle to get its vertices. This is exactly what the GetRegularPolygonVertices function from our helper class GameGlobals does. Let's take a look:

void GameGlobals::GetRegularPolygonVertices(vector<CCPoint> &vertices, 
  int num_vertices, float circum_radius, float start_angle)
{
  vertices.clear();
  float delta_theta = 2 * M_PI / num_vertices;
  float theta = start_angle;
  for(int i = 0; i < num_vertices; ++i, theta += delta_theta)
  {
    vertices.push_back(CCPoint(circum_radius * cosf(theta), 
      circum_radius * sinf(theta)));
  }
}

We passed in a vector of CCPoint that will actually hold the vertices along with the number of vertices, the radius of the encompassing circle, and the angle the first vertex will make with the x axis. The function then calculates the difference in angles between successive vertices of the polygon based on the number of vertices the polygon is supposed to have. Then, initializing theta with start_angle, we run a loop to generate each vertex using the parametric equation of the circle. To summarize, let's take a look at the output of this function:

GameGlobals::GetRegularPolygonVertices(vertices, 3, 50, 0)

This generates the vertices of the following triangle:

Figuring out the geometry

While GameGlobals::GetRegularPolygonVertices(vertices, 3, 50, M_PI/2) generates the vertices of the following triangle:

Figuring out the geometry

So, we have everything we need to draw most of the elements in our game. You can already fill in the blanks and see that the Bomb, which happens to be a power-up, can be summed up as a brown circle containing a green triangle. Similarly, the Shield power-up is a brown circle containing a cyan hexagon, but MissileLauncher and Enemy might not be that straightforward. We shall cross that bridge when we get there, but now we need to focus on the main elements of the game world.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.35.185