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.

  • \$\begingroup\$ This is a pretty broad question. Do you have a specific problem you've come up against when implementing this? \$\endgroup\$ – MichaelHouse Jul 7 '12 at 18:12
  • \$\begingroup\$ @Byte56 I havent started because I don't know how. \$\endgroup\$ – Coder404 Jul 7 '12 at 18:20
  • \$\begingroup\$ It's one of those questions that totally depends on what you want. Do you want the die to react to motion like a real die would? Do you want to know the value ahead of time and just have the die rolling around for effect? There's a lot of detail this question is lacking, and we can't really answer "where to get started" type questions because there's a thousand places to start and a million paths to take once you choose a starting point. Look into opensource games or apps that have rolling dice, see how they did it. \$\endgroup\$ – MichaelHouse Jul 7 '12 at 18:27

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.