PDA

View Full Version : How to roll a cube??

07-31-2002, 07:08 AM
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();
}

nexusone
07-31-2002, 08:43 AM
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. http://www.opengl.org/discussion_boards/ubb/smile.gif

Originally posted by Claudee:
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();
}

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

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

31337
07-31-2002, 12:21 PM
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.

Jared@ETC
08-01-2002, 05:29 AM
That's not a good habit to get into...

nexusone
08-01-2002, 06:45 AM
Yea, I thought that also when I first read the topic, but wanted to be helpful not a smart a\$\$.

http://www.opengl.org/discussion_boards/ubb/cool.gif

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

31337
08-01-2002, 01:26 PM
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...

Jeeeez
08-01-2002, 01:46 PM
Why not use a sphere,

they seem to roll really nicely!!!!!
http://www.opengl.org/discussion_boards/ubb/smile.gif
J

nexusone
08-01-2002, 04:49 PM
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....

http://www.opengl.org/discussion_boards/ubb/smile.gif

Originally posted by 31337:
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...

08-03-2002, 06:37 AM
Thanks for everybody that already give a comment for me.

Originally posted by Claudee:
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();
}