How to roll a cube??

I trying to roll a cube from left screen to right screen.Here are the coding.I hope somebody can help me as I am unable to call the rollCube function.
Thanks.

void MyMouse(int button,int state,int x,int y){
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN){

glTranslated(100,0,0);
glRotatef(-90.0,0.0,1.0,0.0);
glTranslated(-100,0,0);
}

else if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
	glutIdleFunc(NULL);

glFlush();
}

I personaly like to keep all the gltranslate/rotate stuff in my display() routine.

example my mouse routine, only problem is that you only get movement once per click.

void mouse(int button, int state, int x, int y)
{

if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN)) Worldz++;

if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN)) Worldz–;

}

To have it move until button is released do this:
Note: edit and added rolling function.

void mouse(int button, int state, int x, int y)
{

if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN)) cube_x = 1; 1 = roll right.

if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_UP)) cube_x = 0; 0 = no motion
// repeat above for left button excecpt cube_x = -1; where -1 = roll left.

}

display()
{

now add rotate and translate and drawing commands
// note I have update a little

glTranslate(cube_position_x, y, z); move right or left.
glRotatef(cube_rotation, 1, 0, 0); Roll cube

Draw cube

}

my_idle_func()
{

cube_position = cube_position + cube_x; // move cube right or left based on cube_x value.
if (cube_position > 10) cube_position = 10; // max movement to the right. Adjust number to match your screen units
if (cube_position < -10) cube_position = -10; // max movement to the left.

// make cube roll
cube_rotation = cube_rotation + cube_x; // rotate roll cube clockwise or counter based on direction of travel.
if (cube_rotation > 360) cube_rotation = 0; // 360 is max of angle.
if (cube_rotation < 0) cube_rotation = 360;

glutRepostDisplay();// last call in idle func.
}

Let me know if there is anything you don’t understand about this code

Note if you reg. you will get an e-mail when someone post a reply to your message.

Originally posted by Claudee:
[b]I trying to roll a cube from left screen to right screen.Here are the coding.I hope somebody can help me as I am unable to call the rollCube function.
Thanks.

void MyMouse(int button,int state,int x,int y){
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN){

glTranslated(100,0,0);
glRotatef(-90.0,0.0,1.0,0.0);
glTranslated(-100,0,0);
}

else if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
glutIdleFunc(NULL);
glFlush();
}[/b]

[This message has been edited by nexusone (edited 07-31-2002).]

[This message has been edited by nexusone (edited 07-31-2002).]

just an idea from a newbie, why not try having the cube stay in the same spot and just spin the camera around it like crazy? You could probably figure out some easy formula with sine that would do the job nicely.

That’s not a good habit to get into…

Yea, I thought that also when I first read the topic, but wanted to be helpful not a smart a$$.

Originally posted by Jared@ETC:
That’s not a good habit to get into…

I didn’t mean to give off the idea that my nose is up in the air, thats why I said, AN IDEA FROM A NEWBIE

I don’t know much about gl it was just an idea. If you can’t take it that way then don’t bother…

Why not use a sphere,

they seem to roll really nicely!!!

J

The comments where not aimed at you, but about the subject. it was a little joke about the subject… think about it and maybe you will get it…

Originally posted by 31337:
[b]I didn’t mean to give off the idea that my nose is up in the air, thats why I said, AN IDEA FROM A NEWBIE

I don’t know much about gl it was just an idea. If you can’t take it that way then don’t bother…[/b]

Thanks for everybody that already give a comment for me.

Originally posted by Claudee:
[b]I trying to roll a cube from left screen to right screen.Here are the coding.I hope somebody can help me as I am unable to call the rollCube function.
Thanks.

void MyMouse(int button,int state,int x,int y){
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN){

glTranslated(100,0,0);
glRotatef(-90.0,0.0,1.0,0.0);
glTranslated(-100,0,0);
}

else if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
glutIdleFunc(NULL);
glFlush();
}[/b]