simulating 10 faces dice, what face is up?

sefiroths
Posts: 21
Joined: Thu Nov 11, 2010 1:45 pm

simulating 10 faces dice, what face is up?

Post by sefiroths »

i'd like to simulate 10 faces dice falling instead of a sphere like in hello_world example.
if i have understood well, i must use btConvexHull fof collision shape (instead of btSphereShape)
but how can I know what face of the dice will be up at the end of the simulation...?

Code: Select all

dynamicsWorld->stepSimulation(1/60.f,10);
btTransform trans;
fallRigidBody->getMotionState()->getWorldTransform(trans);
and now...?
trans.getBasis() gives me the maxtrix orientation...do i have to multiply it with a vector (0,1,0) to find angles...?
seem complicated...there is a better way?
thanks
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: simulating 10 faces dice, what face is up?

Post by Flix »

See this post: http://bulletphysics.org/Bullet/phpBB3/ ... hilit=dice
The only difference is that you have 4 further directions to check.

You can do the check once when the object go to sleep if you want, but AFAIK there is no sleep callback at the moment so you can check if the object is sleeping every now and then...

Q) How can I know that the object is sleeping?
A) I don't remember at the moment, but there is a way. The main problem is that a body needs some seconds to go to sleep, so maybe you can check its linear and angular velocities instead...

Hope it helps :) .
sefiroths
Posts: 21
Joined: Thu Nov 11, 2010 1:45 pm

Re: simulating 10 faces dice, what face is up?

Post by sefiroths »

thanks, your code is very interesting!
i put as a reference your code:

Code: Select all

btScalar maxValue = 0;
int maxIndex = -1;
for (int i = 0;i<6;i++) {
const int side = i >= 3 ? i-3 : i;
const bool negative = i>=3;
btScalar value = dice->getWorldTransform().getBasis().getColumn(side).dot(btVector3(0.,negative ? -1. : 1.,0.));//Actually I should negate the first term...
if (value>maxValue) {
maxValue = value;
maxIndex = i;
}
}
if i have understood well, the basis maps 3 faces, and negative vector of the basis give other 3 faces.
dot product with (0,1,0) gives the maximum when the 2 vector are parallel

so i have to write something like:

Code: Select all

btScalar maxValue = 0;
int maxIndex = -1;
for (int i = 0;i<10;i++) {
btScalar value = dice->getWorldTransform()*btVector3_normalOfTheFace[i].dot(btVector3(0.,1.,0.);
if (value>maxValue) {
maxValue = value;
maxIndex = i;
}
}
correct me if i'm wrong, i'm a beginner with bullet
thanks! :mrgreen:
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: simulating 10 faces dice, what face is up?

Post by Flix »

sefiroths wrote:if i have understood well, the basis maps 3 faces, and negative vector of the basis give other 3 faces.
dot product with (0,1,0) gives the maximum when the 2 vector are parallel
Exactly, and each of the basis columns (0,1,2) gives the local orientation (x,y,z) of the body in the global space.

Code: Select all

btScalar value = dice->getWorldTransform()*btVector3_normalOfTheFace[i].dot(btVector3(0.,1.,0.);
Well, you want to calculate each local body direction in the global space and then the length of its vertical projection.

First of all I think the operation btTransform * someVector3 is not clear (you don't care about the position of the dice), so it should be better to write: dice->getWorldTransform().getBasis() * someVector3: this should move someVector3 from the local angular space of the body into the global angular space (or maybe viceversa..., anyway you can swap the factors to invert the effect).

Basically I haven't tried your code, but if you want a different form, I would try (not tested):

Code: Select all

btScalar value = ( dice->getWorldTransform().getBasis()*btVector3_normalOfTheFace[i] ).dot(btVector3(0,1,0))
assuming btVector3_normalOfTheFace is one of the 10 directions in local space (which means for an observer inside the dice).
However note that 6 of the 10 directions should be like (1,0,0), and so the first multiplication should end up in dice->getWorldTransform().getBasis().getColumn(0) (if I'm not messing up something...), which saves 2/3 of the calculations...

Should the code be wrong, I expect that a swap of the first two term should make it correct.

Hope it helps.
sefiroths
Posts: 21
Joined: Thu Nov 11, 2010 1:45 pm

Re: simulating 10 faces dice, what face is up?

Post by sefiroths »

thanks for help! however i have some problem, so i have made a simple project.
i have used oolong engine example "falling box" (because i'm not able to compile with xcode exaples from bullet)
so the only change i have is box--->d4 dice (more simple than d10!)
the code for a d4 dice:

Code: Select all

struct texCoord
	{
		GLfloat		u;
		GLfloat		v;
	};
	typedef struct texCoord texCoord;
	typedef texCoord* texCoordPtr;

	typedef struct vec2 vec2;
	typedef vec2* vec2Ptr;

	struct vec3
	{
		GLfloat x;
		GLfloat y;
		GLfloat z;
	};

	typedef struct vec3 vec3;
	typedef vec3* vec3Ptr;

	struct vec4
	{
		GLfloat x;
		GLfloat y;
		GLfloat z;
		GLfloat w;
		};

	typedef struct vec4 vec4;
	typedef vec4* vec4Ptr;

struct vertexData
{
	vec3		vertex;
	vec3		normal;
}
typedef struct vertexData vertexData;
typedef vertexData* vertexDataPtr;


static const vertexData MeshVertexData[] = {
	{/*v:*/{0.000000, 1.000000, -0.000000}, /*n:*/{0.000000, 1.000000, 0.000000} },
	{/*v:*/{0.942809, -0.333333, 0.000000}, /*n:*/{0.942808, -0.333323, 0.000000} },
	{/*v:*/{-0.471405, -0.333333, -0.816497}, /*n:*/{-0.471389, -0.333323, -0.816492} },
	{/*v:*/{0.000000, 1.000000, -0.000000}, /*n:*/{0.000000, 1.000000, 0.000000} },
	{/*v:*/{-0.471405, -0.333333, -0.816497}, /*n:*/{-0.471389, -0.333323, -0.816492} },
	{/*v:*/{-0.471405, -0.333333, 0.816497}, /*n:*/{-0.471389, -0.333323, 0.816492} },
	{/*v:*/{0.000000, 1.000000, -0.000000}, /*n:*/{0.000000, 1.000000, 0.000000} },
	{/*v:*/{-0.471405, -0.333333, 0.816497}, /*n:*/{-0.471389, -0.333323, 0.816492} },
	{/*v:*/{0.942809, -0.333333, 0.000000}, /*n:*/{0.942808, -0.333323, 0.000000} },
	{/*v:*/{0.942809, -0.333333, 0.000000}, /*n:*/{0.942808, -0.333323, 0.000000} },
	{/*v:*/{-0.471405, -0.333333, 0.816497}, /*n:*/{-0.471389, -0.333323, 0.816492} },
	{/*v:*/{-0.471405, -0.333333, -0.816497}, /*n:*/{-0.471389, -0.333323, -0.816492} },
};

// Example OpenGL ES 1.1 Drawing Code:
// glEnableClientState(GL_VERTEX_ARRAY);
// glEnableClientState(GL_NORMAL_ARRAY);
// glVertexPointer(3, GL_FLOAT, sizeof(VertexData3D), &MeshVertexData[0].vertex);
// glNormalPointer(GL_FLOAT, sizeof(VertexData3D), &MeshVertexData[0].normal);
// glDrawArrays(GL_TRIANGLES, 0, kMeshNumberOfVertices);
// glDisableClientState(GL_VERTEX_ARRAY);
// glDisableClientState(GL_NORMAL_ARRAY);


btScalar v[]={
        0.000000, 1.000000, -0.000000,
	0.942809, -0.333333, 0.000000,
	-0.471405, -0.333333, -0.816497,
	-0.471405, -0.333333, 0.816497
};
i have changed the box so:

Code: Select all

btCollisionShape *boxShape=new btConvexHullShape(v,4);
instead of:

Code: Select all

btCollisionShape *boxShape=new btBoxShape(btVector3(1,1,1));
and on renderScene the displayed objects:

Code: Select all

glColor4f(0,1,0,1);
glVertexPointer(3, GL_FLOAT, sizeof(vertexData), &MeshVertexData[0].vertex);
glColor4f(1,0,0,1);
glDrawArrays(GL_TRIANGLES, 0, 3);
glVertexPointer(3, GL_FLOAT, sizeof(vertexData), &MeshVertexData[3].vertex);
glDrawArrays(GL_TRIANGLES, 0, 3);
glColor4f(0,0,1,1);
glVertexPointer(3, GL_FLOAT, sizeof(vertexData), &MeshVertexData[6].vertex);
glDrawArrays(GL_TRIANGLES, 0, 3);
glColor4f(1,1,0,1);
glVertexPointer(3, GL_FLOAT, sizeof(vertexData), &MeshVertexData[9].vertex);
glDrawArrays(GL_TRIANGLES, 0, 3);
but seems that the d4 dice not stops on its face, and some dices not stop at all!
any ideas?
thanks
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: simulating 10 faces dice, what face is up?

Post by Flix »

sefiroths wrote:i have used oolong engine example "falling box"
I'm not familiar with this engine, but it seems that now your problem is completely different from the topic of your first post...
sefiroths wrote:i have changed the box so:
btCollisionShape *boxShape=new btConvexHullShape(v,4);
instead of:
btCollisionShape *boxShape=new btBoxShape(btVector3(1,1,1));
but seems that the d4 dice not stops on its face, and some dices not stop at all!
Well, I don't know why your tetrahedron does not stop. I suggest you build a d8 convex hull to emulate a btBoxShape and see if it works. You may try making it slightly bigger and set some good damping and sleeping threshold values (as well as a good mass and local inertia). Good luck!

P.S.
i have used oolong engine example "falling box" (because i'm not able to compile with xcode exaples from bullet)
I suggest you try to compile the main Bullet examples and use them as a templates for your projects if you can (it might be that the graphical representation of the convex hull is not precise with the engine you used).
sefiroths
Posts: 21
Joined: Thu Nov 11, 2010 1:45 pm

Re: simulating 10 faces dice, what face is up?

Post by sefiroths »

oolong engine uses bullet primitives...
i have used:

Code: Select all

btScalar v8={
0,0,0,
0,0,1,
0,1,0,
0,1,1,
1,0,0,
1,0,1,
1,1,0,
1,1,1
}
and

Code: Select all

btCollisionShape *boxShape=new btConvexHullShape(v8,8);
the cubes does not stop, and the box stopped don't lay on their side...
i'll make other try with bullet examples...(if i'll be able)
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: simulating 10 faces dice, what face is up?

Post by Flix »

The center of mass of your convex hull should be (0,0,0). That would explain why the cube keeps lying on one side, but not why it does not stop (you can try setting up some damping or sleeping threshold).

For the cube you can use vertices between (-a,-a,-a) and (a,a,a), with a = half cube dimension, and see if it works.
sefiroths
Posts: 21
Joined: Thu Nov 11, 2010 1:45 pm

Re: simulating 10 faces dice, what face is up?

Post by sefiroths »

now i have used:

Code: Select all

float a=1;
btScalar v2[]={
-a, -a, -a,
-a, -a,  a,
-a,  a, -a,
-a,  a,  a,
 a, -a, -a,
 a, -a,  a,
 a,  a, -a,
 a,  a,  a,
}
on the left what i have with btBoxShape, on the right with btConvexHull
:(
i did'n studyed how to set up damping or sleeping threshold yet, i'm a beginner
You do not have the required permissions to view the files attached to this post.
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: simulating 10 faces dice, what face is up?

Post by Flix »

This code can be added in the Bullet appBasicDemo at the end of BasicDemo::initPhysics():

Code: Select all

	#define CONVEX_HULL_TEST
	#ifdef CONVEX_HULL_TEST
	{
		// Convex Hull Box half extents
		const btScalar x=1;
		const btScalar y=1;
		const btScalar z=1;
		// Convex Hull Box origin	
		const btScalar ox=0;
		const btScalar oy=0;
		const btScalar oz=0;			
		// -------------------------------
		const btVector3 points[] =	{
			btVector3(ox+x,oy+y,oz+z),
			btVector3(ox-x,oy+y,oz+z),
			btVector3(ox+x,oy-y,oz+z),
			btVector3(ox-x,oy-y,oz+z),
			btVector3(ox+x,oy+y,oz-z),
			btVector3(ox-x,oy+y,oz-z),
			btVector3(ox+x,oy-y,oz-z),
			btVector3(ox-x,oy-y,oz-z)				
		};
		btConvexHullShape* shape = new btConvexHullShape(&points[0].x(),sizeof(points)/sizeof(points[0]));
		//m_collisionShapes.push_back(shape);
		// Convex Hull Box initial position and rotation
		btTransform transform;transform.setIdentity();
		transform.setOrigin(btVector3(-10,5*y,-20));
		transform.setRotation(btQuaternion(btVector3(0,1,0),btRadians(60)));
		// Mass and local inertia
		const btScalar mass(1.);
		const bool isDynamic = (mass != 0.f);
		btVector3 localInertia(0,0,0);
		if (isDynamic)	shape->calculateLocalInertia(mass,localInertia);	
		// Motion state and body
		btDefaultMotionState* myMotionState = new btDefaultMotionState(transform);
		btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,shape,localInertia);
		btRigidBody* body = new btRigidBody(rbInfo);
		// Adding to world
		m_dynamicsWorld->addRigidBody(body);	
	}
	#undef CONVEX_HULL_TEST
	#endif //CONVEX_HULL_TEST
It seems to work for me. From the image you posted it seems that the "Convex Hull Box origin" is not in (0,0,0). (I think I can replicate this with (0,0,1) for example). Note that I did not use any splecial damping or sleeping threshold.
Hope it helps.
sefiroths
Posts: 21
Joined: Thu Nov 11, 2010 1:45 pm

Re: simulating 10 faces dice, what face is up?

Post by sefiroths »

mmm...but the origin is (0,0,0).
the points of the convex hull, except for the order, are the same of your box.
however i'll try to compile bullet from scratch and try that.
thanks