The examples in this post are built using Aquanet.

The examples below are the most basic of the applications built in Aquanet. This is the client-side code for a simple multiplayer game we built at work. It is very simple and uses a few simple objects. It is a single page application, meaning it is a single HTML page which contains the JavaScript.

Once you have Aquanet installed, you need to download Aquanet client code. There are client versions available for Node, Ruby, PHP, C#, Java, C++, C# and Python.

Simple Game

The application is quite simple. There are two players and a ball (a sphere).

 

var game = new aquanet.Game("playground","#playground");
var play = new aquanet.Player("#playername", game, new aquanet.Sphere(0.5, "#playershape"))

play.attachEvent("keypressed", function(e) {
    if(e.key == "f") {
        game.moveForward(play);
    }
    else if(e.key == "b") {
        game.moveBackward(play);
    }
})

The attachEvent() method is used to attach an event handler, this makes the game move forward when the player types F and backwards when the player types B. There is also a keypressed(), which is used to check the key that the player has pressed and to move the player accordingly (forward if 'F', backwards if 'B').

In the end, the game is saved inside a global variable called game.

Player Control

 

A player is a container for a player control object (such as a ball) and a state object (which is basically a dictionary).

Every player is a member of a game.

A player control object is a function that receives a player as argument and returns a state object.

var game = new aquanet.Game("playercontrol", "#playercontrol")
var play = new aquanet.Player("#playername", game, new aquanet.Rect(100, 100, 100, 100))
var ball = new aquanet.Ball(play, "ball")
var state = new aquanet.State(play, {
    ball: ball,
    left: 100,
    right: 200
})
function PlayerControl(id, playerContainer, stateObj) {
    this.stateObj = stateObj
}

 

The state object is used to calculate the ball's current position, and to check whether it should move left or right.

The ball's current position is calculated in the update function.

The update function receives the player as argument and returns the updated state.

 

The draw function is called every frame (which is the game's time unit), and it is responsible for drawing the screen and calculating the ball's position on the screen.

function draw(player, camera) {
    camera.update()
    var rect = player.getBoundingRect()
    var w = rect.width
    var h = rect.height
    var sx = rect.x + w * player
    var sy = rect.y + h * player
    camera.draw(w * player, sx, sy, h)
}

 

The player variable represents the player, and the camera is the camera object.

 

The update function does the following things:

 

Finally, the draw function calculates the ball's position on the screen, given the player's position, and the camera's position.

function update(delta) {
    if (!player.isAlive) return
    if (game.state == GameState.PLAYER_LEFT || game.state == GameState.PLAYER_RIGHT) {
        var dx = player.x - player.width / 2
        var dy = player.y - player.height / 2
        // Update camera's position based on player's position
        camera.update(dx, dy)
    } else {
        // Calculate the ball's position based on the current game state
        var sx = ball.x - ball.radius - ball.width / 2
        var sy = ball.y - ball.radius - ball.height / 2
        // Update ball's position based on the camera's position and the game state
        ball.x = camera.x + (camera.x - ball.x) * (2 + game.state) - ball.width / 2
        ball.y = camera.y + (camera.y - ball.y) * (2 + game.state) - ball.height / 2
    }
    // Draw the scene
    renderer.clear()
    renderer.draw(program, camera)
    renderer.draw(skybox)
    renderer.draw(scene)
    renderer.draw(grid)
    renderer.draw(player)
    renderer.draw(ball)
}

You will notice that we update the camera's position based on the player's position during the PLAYER_LEFT & PLAYER_RIGHT transitions. This allows the player to be visible even after the camera moves.

Blender is a powerful 3D modeling software, and we will use it to design the background of the scene.

Start with some basic geometry, and then apply Blender's UV Layout to the mesh. Now, you will need to assign a texture to each mesh. The first thing you will want to do is assign a textured plane to the background of the scene.

Next, we will create a solid texture for the character. There are two ways to do this with Blender. You can either:

Either way works, but in this tutorial, we will use the solid option.

Select the top part of the cube and hit the Make Mesh Object button.

Afterwards, select the cube and hit the Texture button. A new box will open and you have to select the texture you want to use.

Image title

Now you have to assign the texture to the part of the mesh that must be visible on the character. Do this by selecting the texture and choosing Texture > Set Texture, selecting the part of the mesh and hitting OK.

Now you have to do the same for the other mesh, but this part is visible at the bottom of the character. Select the Material to use for the character and select the material you want to use for the bottom part. Again select the mesh using the box and assign the material to the mesh using Texture > Set Material, selecting the material and hitting OK.

Image title

Now you can close the box, hit Unlink and Flatten and save.

Then select your model and rotate it so that its bottom is at the correct position. You can see the model bottom in image http://www.cocos2d-iphone.org/wiki/doku.php/proj:blender:blender_select_tutorial_bottom_image_2.

If you want to render it, select the Material you want to use for the character and select the Render tab. You can see the default settings in the image http://www.cocos2d-iphone.org/wiki/doku.php/proj:blender:render_material_default_settings.

If you want to render the model only you can select the Material you want to use for the character and select the Render tab. This will allow you to render only the model of the character.

node-render-material.png

To make the animation we need to render the model in order to export the motion.

We can see a simple setup in the next images. The rendering time depends in the number of vertices of the model but it is more efficient to use the Blender to render the model than to use Cocos2d-X.

In the next step we need to use the Blender to animate the model and export the result to a file from Cocos2d-X.

The Blender is very powerful and easy to use. For example, it is much easier to make the animation if you create a blend file. This file contains all the keyframes from one scene to another. If you are new to Blender, please read the tutorial Cocos2d-X.

The next step is to import the file and create a Cocos2d-X project and use the cocos2d-iphone-extensions to make a Cocos2d-X project in iOS.

Finally, Cocos2d-X is now in the Blender project. Open the project in Blender.

The next step is to add the camera and the Cocos2d-X project to the Blender project.

For adding the camera, you need to add a camera component to the Blender project and then add a link to the camera project to Blender.

To add a link to the camera project to Blender, you need to open the camera project in the Blender project.

Select the folder containing the camera project and then click the Project menu.

The Camera Project dialog appears.

Camera Project dialog

Figure 1 - Camera Project dialog

Select the Projects tab to open the projects list.

Click the Add button to add the camera project to the list.

Once the camera project is added, click the Close button.

Now, select the Blender project from the Objects tab.

Open the User Defined tab.

In the Add New dropdown, select Camera.

Scroll down and select the camera linked project.

Click the OK button.

Open the object in the 3D viewport and you should see the camera preview.

This is a very simple example for adding a camera to your project. You can use this as a template and expand upon it. You can add more views to the camera or even add another camera. You can add a 3D model to the camera.

You can also add a microphone to the Blender project. See the Cocos2D Blender project setup document for further information.

To add a camera to an existing 3D scene, you are going to have to open the 3D scene in some other application. For the examples given in this tutorial, I will use the 3D modeler from Blender, Blender’s own 3D modeler.

Hint

In the 3D viewport, open the Edit menu and select Add >.

Open the File menu and select Load from File....

Select the file Camera.blend.

Click the Open button.

Now select the Camera project from the Objects tab.

../_images/teaser.png ../_images/camera.png ../_images/camera-small.png

The camera object should now be selected. Press the G key to go to object mode. Notice that Camera is the only object selected.

Add a camera to an existing 3D scene

Go to the Camera object properties, and set the object's Radius to 5.0. Press the E key to go to the Edit mode.

Set the camera radius to five

Adjust the radius of the camera so that the X axis is pointing to the top of the scene. You should have the following view.

Set view parameters

Now, you can add a material to the camera.

Tip

If you need to quickly select an object, you can press the T key to enter object mode.

The image below shows the object with a diffuse material.

Add a material

Next, you need to add a MeshGeometry to the camera. This object will be the scene background.

Go to the Materials object properties and add a Mesh material. Drag the Scene Background object from the Object library to the scene hierarchy. Add a Mesh object to the camera, which should have the following properties.

Mesh
Mesh object name: Mesh Backgroun
Mesh
Mesh object name: Mesh Backgroun
Mesh type: Triangle
Mesh
Mesh object name: Mesh Backgroun
Mesh type: Triangle
Mesh
Mesh object name: Mesh Backgroun
Mesh type: Triangle
Mesh
Mesh object name: Mesh Backgroun
Mesh
Mesh object name: Mesh Backgroun
Mesh type: Triangle
Mesh
Mesh object name: Mesh Backgroun
Mesh type: Triangle

Properties

Property Description Type Required
Backround Mesh Backgroun Mesh background of the mesh Mesh False
Center Mesh Center Mesh center of the mesh Vector3D False
Center Vector3D Vector3D center of the mesh Vector3D False
Color Color Mesh (background) color of the mesh Color False
Material Material Mesh (background) material of the mesh Material False
Material MaterialName Mesh (background) material name of the mesh Material False
Material Texture Texture used for the background of the mesh Texture False
Texture TextureName Name of the texture used for the background of the mesh Texture False
Texture TextureCoord Texture coordinates of the texture used for the background of the mesh Texture False
Texture TextureCoord2 Texture coordinates of the texture used for the background of the mesh Texture False
Texture TextureCoord3 Texture coordinates of the texture used for the background of the mesh Texture False
Texture TextureCoord4 Texture coordinates of the texture used for the background of the mesh Texture False