Rolling a 3d die in cocos2d

| | August 6, 2015

How can I have a 6 sided die in my cocos2d game and when the device is shaked the die rolls and shows a number? I would also use UIKit if it was necessary.

One Response to “Rolling a 3d die in cocos2d”

  1. There are several ways to go about this. Here are some ideas:

    Pre-rendered animations

    Use a 3D rendering and animation software to animate 6 different die rolls. The advantage of this is that you don’t need any 3D library, as you can show the rendered animations in a 2D context. Also pre-rendered animations give you lots of artistic freedom.

    In your game you would wait for a gesture (eg. shaking the device), run a random-number generator to get the result of the roll and then play the matching animation.

    Using realtime 3D rendering

    Animate a die roll in a 3D modelling software and then use something like Cocos3D to show the animation in your game. This is almost the same as the suggestion with the pre-rendered animations, but instead of having 6 different animations, you could have just one and get the different results by applying one of 6 different textures to the cube. Also this will be much smaller in file-size, as you’re using realtime rendering.

    Triggering and playing the animation would work pretty much the same as with the pre-renderer animations.

    Using realtime 3D in combination with a 3D physics-engine

    Use realtime 3D rendering (such as Cocos3D) together with a 3D physics engine (such as bullet) to simulate die movement. The advantage of this is that you could feed real device motion data into the simulation and have a different animation everytime.

    In your game you would probably want to give the die/cube a random start-rotation (or randomly pick one of the 6 sides that should point up initially). Then wait for a user gesture and apply the device motion to the die and start the physics simulation. Wait for the die to stop movement and figure out which side is pointing upwards to get the result.

    While the realtime approach with physics is the most realistic one with the most variation, it is also quite demanding in terms of programming and setting up the libraries. If you have never used a 3D physics engine, it might be quite a challenge to get the simulation look “right”.

    Using six pre-rendered animations is probably the easiest approach. You could ask an artist to create these for you and most game-engines can play animations without the need for additional libraries. And since you’ll have six slightly different animations for each result, the user will still get some variation.

Leave a Reply