Game Development Reference
In-Depth Information
A for loop is used to enumerate over every child node in order to create the vertex posi-
tions for the current frame. The first node is the center node, and its position is simply
taken as is. For all remaining child nodes, the position is extended outward from the cen-
ter by _enlargeDrawRadius . The principle and algorithm are the same as in Listing
10-11 .
The resulting vertexPos is converted to a GLKVector4 type by using the GLKVect-
or4Make function and assigned as the position of the vertex at the index that corresponds
to the child node being enumerated. The third coordinate is the z coordinate, with which
you can influence the draw order of the vertex. In this case, it is zero, so it will use the
same draw order as designed by SpriteBuilder. The last coordinate ( w ) has to be 1.0 for all
vertices.
Updating the render buffer with the new vertex is done by CCRenderBufferSetVer-
tex , which essentially copies _vertices[i] to the render buffer at index i . Since the
vertex position is expressed in coordinates relative to the sprite's parent, CCVertexAp-
plyTransform is used to apply the transform passed into the draw method. This has
the effect of moving the vertex position to world (scene) coordinates.
After the for loop, the last vertex is set to become a copy of the second vertex. This
closes the last triangle 1-9-10, which is the same as 1-9-2 in Figure 10-6 .
Finally, the CCRenderBufferSetTriangle creates all the triangles from the vertices
already set before with CCRenderBufferSetVertex , in the order 0-1-2, 0-2-3, 0-3-4
and so on. Keep in mind that index i is zero-based, so index 0 is the center vertex, unlike
in Figure 10-6 where the center node is labeled as vertex #1 .
You can have some fun now by running the game in its current state with the new render-
er. Since the player's bodies aren't held together by joints, they are free to go, while the
new renderer draws the texture between the individual bodies. This leads to some heavy
distortion and some hilarious results, one can be seen in Figure 10-8 . But it does prove
that drawing the texture works in general, and it also gives you an example of what things
may look like if all the bodies together don't form a coherent body. The texture can some-
times stretch far and thin, or it can become flipped as bodies travel in opposite directions.
Search WWH ::




Custom Search