Dice and collsion

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Dice and collsion

Post by Asimov »

Hi all,

I am making a board game. Now in the testing stages I will probably use random numbers for the dice, but later it would be nice to have real dice in the game.

Has anyone had any experience with making dice work.

Would it be best to cheat, generate the random numbers and make the dice fall and show those numbers generated.

Or use simple physics and let the dice fall in real time and somehow read the values off said dice.

If anyone has any experience in this and can help me, I would appreciate it.

I haven't got to testing out any physics in irrlicht yet. I imagine it wouldn't need a full blown physics engine to make dice fall and bounce a few times?

I am no where near that stage yet though. Just a few questions for the future.
CuteAlien
Admin
Posts: 9679
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Dice and collsion

Post by CuteAlien »

I guess you could also make 6 (minimal) animations. Or maybe just the animations for the rotation - and then the movement along a line (maybe with slowing down along the line or so). Or 6 animations including movement - likely the easiest solution. With physics engine you would probably sit for days just tweaking force parameters until the dice move somewhat realistic without falling off your board :-)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: Dice and collsion

Post by Asimov »

HI CuteAlien,

Hmm maybe I could pre-animate in 3ds max, and then import them into irrlicht, as physics in 3ds max are easy.
Can I use obj for this? I know FBX can hold animation data, but not tried it with obj before.

If I just used a texture and normalmap(assuming I will ever get a normalmap to work heh heh) on the dice, perhaps before the animation I could change the texture on the which ends up on top with the random number I generated. This could be difficult LOL, as I would have to change the texture on all 6 sides. Unless I just change the map, but I would have to have 6 maps with the dice load with different, positions. Hmm this is getting complicated.

As dice are two and a die is one, just doing 6 animations wouldn't be enough, but if I could change the texture on top of the dice before the animation that would likely work.

Now all I gotta do is work out how animations work in irrlicht LOL.

This game is becoming a saga....
CuteAlien
Admin
Posts: 9679
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Dice and collsion

Post by CuteAlien »

The obj format doesn't have animations - it's only for static data. You have to use .X or .b3d for that.
You can use the same animation for both of your dice. Just start off with a slight rotation of the node then they will go into somewhat different directions. You can also start with some offset in the start-position additionally. Then draw them without z-buffer and last so they are on top of everything but not intersecting with each other. That might look fine and is probably easy to do.

You can also work by switching uv-coordinates. But why so much work? Far easier to do 6 (or even 21) combination in a 3d tool and then just use those animations.

You could also render the result of the animation into a quick movie and then play that as animated textures on top of your board.

Often easiest to do as much as possible in the modeling tool.

Btw, I got the idea above by taking a look at an old game I worked at long time ago. I didn't implement that part back then and don't know how it was really done - but from watching youtube videos of it I guessed that might be how it was done. If you search for "catan die erste insel" in youtube you will see dices regularly - and I suppose my described solution might give that effect more or less.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: Dice and collsion

Post by Asimov »

Hi CuteAlien,
You could also render the result of the animation into a quick movie and then play that as animated textures on top of your board.
The only problem with this approach is this. There are 36 possible combinations the dice can fall. So for an animated movie I would need 36 different animations.
I could easily render to png so that it looks like it is falling on the board, but I would have to match the shadow to the current lighting.

Just found out that there is now an x exporter for max 2015 phew.
CuteAlien
Admin
Posts: 9679
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Dice and collsion

Post by CuteAlien »

There are only 21 different combinations ;-) But you only need 6 - as long as you play one above the other each dice can use the same 6 animations (that's what I think the game I just mentioned did).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: Dice and collsion

Post by Asimov »

Hi CuteAlien,

Just got a box and did an animation of it spinning. Exported it as a panda x file, and I got it to work in irrlicht.
Although I got it to crash a few times first though. Apparently irrlicht does not do compressed x files.

I know I can animate the dice as I have done lots of animation before.

So now I have to decide on my method.
Will spend some time playing with different ideas.

Thanks for the input.

One more question. I know nodeAnim->setAnimationSpeed(25); plays the animation in a loop, but how do I only play an animation once?

I have expanded my LoadManager class. It can now load static meshes and animated meshes.

Code: Select all

#ifndef LOADMANAGER_H
#define LOADMANAGER_H
 
#include "irrlicht.h"
 
class LoadManager
{
    public:
        LoadManager();
        void loadMesh(irr::scene::ISceneManager* smgr,irr::video::IVideoDriver* driver,const wchar_t* modelname);
        void RotateMesh(float *rotation,const irr::f32 frameDeltaTime);
        void nodePosition(float x,float y,float z,float rot);
        void nodeAnimPosition(float x,float y,float z,float rot);
        void loadPiece(irr::scene::ISceneManager* smgr,irr::video::IVideoDriver* driver,const wchar_t* modelname);
        irr::scene::ICameraSceneNode* camera;
        void loadAnim(irr::scene::ISceneManager* smgr,irr::video::IVideoDriver* driver,const wchar_t* modelname);
 
    private:
        irr::scene::IMesh *mesh;
        irr::scene::IMeshSceneNode*node;
        irr::video::IVideoDriver* driver;
 
        irr::scene::IAnimatedMesh *meshAnim;
        irr::scene::IAnimatedMeshSceneNode *nodeAnim;
};
 
#endif // LOADMANAGER_H
 
and here is my cpp

Code: Select all

#include "LoadManager.h"
 
using namespace irr;
using namespace core;
using namespace video;
using namespace scene;
using namespace io;
using namespace gui;
 
LoadManager::LoadManager()
{
    //constructor
}
 
void LoadManager::loadMesh(ISceneManager* smgr,IVideoDriver* driver,const wchar_t* modelname)
{
    mesh = smgr->getMesh(modelname);
    node = smgr->addMeshSceneNode(mesh);
    if (node)
    {
        node->setMaterialFlag(EMF_LIGHTING, true);
        node->addShadowVolumeSceneNode();
        node->getMaterial(2).TextureLayer[0].LODBias = -4;
    }
}
 
void LoadManager::loadAnim(ISceneManager* smgr,IVideoDriver* driver,const wchar_t* modelname)
{
    meshAnim = smgr->getMesh(modelname);
    nodeAnim = smgr->addAnimatedMeshSceneNode(meshAnim);
 
    if (nodeAnim)
    {
        nodeAnim->setMaterialFlag(EMF_LIGHTING, true);
        nodeAnim->addShadowVolumeSceneNode();
        nodeAnim->setAnimationSpeed(25);
    }
}
  
void LoadManager::RotateMesh(float *rotation,const irr::f32 frameDeltaTime)
{
     node->setRotation(core::vector3df(0,*rotation,0));
}
 
void LoadManager::nodePosition(float x,float y,float z,float rot)
{
    node->setPosition(vector3df(x,y,z));
    node->setRotation(vector3df(0,rot,0));
}
void LoadManager::nodeAnimPosition(float x,float y,float z,float rot)
{
    nodeAnim->setPosition(vector3df(x,y,z));
    nodeAnim->setRotation(vector3df(0,rot,0));
}
 
 
 
 
 
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: Dice and collsion

Post by Asimov »

Hi CuteAlien,

I am a little unhappy with the x format. Oh I can save out a model and the animation works, but again normalmaps don't seem to work, and I get strange squares on the texture, and I have no idea why.
Image
CuteAlien
Admin
Posts: 9679
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Dice and collsion

Post by CuteAlien »

Here's the doc for IAnimatedMeshSceneNode:http://irrlicht.sourceforge.net/docu/cl ... _node.html
Looping control with setLoopMode.

I don't know formats well enough and as previously mentioned have no experience with normal maps. If you can put an example of the model+texture online maybe someone can help (not sure if me, I have little spare-time this weeks ...).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
rustyk
Posts: 25
Joined: Sat Jan 10, 2015 4:03 pm

Re: Dice and collsion

Post by rustyk »

@Asimov,

I think you should recalculate your normal maps or try a different 3d format. Posting it here will help too so someone could test it.
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: Dice and collsion

Post by Asimov »

Hi rustyk,

I have used 3 different programs to create normalmaps, and all fail.

This grenade was baked in 3ds max. The room was actually baked in mudbox, and I have also tried another method.
My normalmaps work in Mudbox,Unity, Cryengine and marmoset.

I have now found a different exporter and I believe the strange squares on my model are shadow, but for some reason the realtime shadow is showing as squares on my model.

The last x exporter is the one on this page.
http://www.cgdev.net/download.php

They state on the page that irrlicht does not load in x files properly and a change to the code might help. Haven't tried this yet though.
Load X files with 3d engines

Irrlicht is an open source cross-platform graphics engine which enable X files to be used in both DirectX and OpenGL applications. This engine provides a class CXMeshFileLoader to load X files without D3DX. It supports text and binary X file format from version 1.1, normals and texcoords in DeclData from version 1.5 but up to version 1.8.1 the loader has a bug on parsing DeclData which may cause engine failed to load a binary X file.
My solution is open CXMeshFileLoader.cpp and insert readHeadOfDataObject() at the beginning of the if block in CXMeshFileLoader::parseDataObjectMesh

if (objectName == "DeclData")
{
if (!readHeadOfDataObject()) //Add this
return false;
...
}
As for 3d formats so far I have tried 3ds, obj and now x. Can't use b3d as I am using 3ds max 2015 and there is no exporter for it, and it is really old anyway.
Couldn't paste my normalmap here as photobucket don't accept tga files. Will have to post a link tomorrow.
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: Dice and collsion

Post by thanhle »

If you post the mesh file with texture in a zip, maybe other guys can test it for you.
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: Dice and collsion

Post by Asimov »

Hi thanhle,

Will do this when I get home from work.
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: Dice and collsion

Post by Asimov »

Hi all,

I have still unsuccesfully got a normalmap to work in irrlicht so I have zipped up my model, as a max file, an obj, and an x file.
I have tried the built in exporter for obj in max 2015, then I tried x format with the Axe exporter. I also tried for x, the KWE exporter, but none of them seem to work properly in irrlicht.

I know you can export as a text file or binary, so I exported as text, and had a look inside one of the x files and this is strange.
I can find the following in the x file

Code: Select all

TextureFilename {
  "Diffuse.tga";
 }
}
 
Which obviously loads the diffuse, but I can't find anywhere in that x file that loads the normalmap, and even when I manually load it in using irrlicht it does not show up.
Anyway here is the link to my model.
By the way it isn't just this model. I have tried many different models, and none of the normalmaps work.
http://www.asimoventerprises.co.uk/test/grenade.zip
CuteAlien
Admin
Posts: 9679
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Dice and collsion

Post by CuteAlien »

The .X file should have "NormalmapFilename" like the "TextureFilename" somehow. Thought Irrlicht would only load it - not set the right material-type as far as I can see. So that would have to be fixed afterwards. Also I notice the obj loader calls videoDriver->makeNormalMapTexture(texture, bumpiness); after loading that textures while the .X loader doesn't do that. This is because parallax maps need some conversion to put the hightmap into the alpha-channel of the texture. No idea if that is needed as well or not in this case.

What might work (untested): Load the .X mesh. Convert it with IMeshManipulator::createMeshWithTangents() to a mesh that can have normal maps.
Load that texture yourself (maybe convert it with makeNormalMapTexture or maybe not...). Set it as texture with index 1 to the material of your mesh. Change the MaterialType to video::EMT_PARALLAX_MAP_SOLID or maybe one of the other normal-map types (depends on the format used by the texture - see descriptions here: http://irrlicht.sourceforge.net/docu/na ... 0cbc5430f1). Also you might have to set MaterialTypeParam - obj loader does set it to 0.035 ... not sure right now what it's used for. But something about bumpiness as well I guess.

Btw, the obj loader has a problem with filenames with spaces. I think you found that out already recently? Remove those and it loads some normal map. It looks not exactly correct I think - but that's hard to tell for me as I've no experience whatsoever with parallax maps. So could simply be the wrong texture-format - MaterialType combination in this case. Try using the other material types.

I never got your problem with the squares - try loading it in the meshloader if you also get it there or if this is a problem only in your application (I tried with svn trunk and with svn 1.8 of Irrlicht).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply