Thread: 3D ball problem

  1. #1
    Registered User
    Join Date
    Jan 2011
    Location
    Slovakia
    Posts
    21

    3D ball problem

    I'm trying to swing two balls so that when the first ball swings from -20 degrees to zero it stops and sends the second ball swinging out to 20 degrees and returning back to zero again.

    However, that problem is that whilst I can get both balls swinging between the ranges at the same time I can't get the one ball to move whilst the second is still and vice versa. I tried a switch case but that failed totally.

    any ideas?
    then I can get a Newton's Cradle working


    Code:
    #include <iostream>
    #include <GL/glut.h>
    #include <windows.h>
    #include <math.h>
    
    using namespace std;
    HDC hdc;
    
    int slices = 40;
    	int stacks = 40;
    	float radius = 0.1;
    
    float angle[2] = {0.0f, 0.0f};
    
    
    
    GLfloat mat_ambient[] = {0.5, 0.5, 0.5, 0.01};
    GLfloat mat_specular[] = {0.1, 0.1, 0.1, 0.01};
    GLfloat mat_shininess[] = {50.0};
    GLfloat light_position[] = {10.0, 10.0, 20.0, 0.0};
    GLfloat model_ambient[] = {0.5, 0.5, 0.5, 0.0};
    
    void setupMaterials() {
    	glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
    	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    	glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    	glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);
    }
    
    void changeColour(GLfloat r, GLfloat g, GLfloat b, GLfloat A) {
    	model_ambient[0] = r;
    	model_ambient[1] = g;
    	model_ambient[2] = b;
    	model_ambient[3] = A;
    	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);
    }
    
    void init(void) {
    	glClearColor(0.0, 0.0, 0.0, 0.01);
    	setupMaterials();
    	glEnable(GL_LIGHTING);
    	glEnable(GL_LIGHT0);
    	glEnable(GL_DEPTH_TEST);
    	glEnable(GL_CULL_FACE);
    	glFrontFace(GL_CCW);
    	glShadeModel(GL_SMOOTH);
    }
    
    void reshape(int w, int h) {
    	GLfloat fAspect;
    	if(h==0) h=1;
    
    	glViewport(0,0,w,h);
    
    	fAspect = (GLfloat)w / (GLfloat)h;
    
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    
    	gluPerspective(60, fAspect, 0.5, 1000.0);
    	glTranslatef(0,0,-1);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    }
    void DrawSphere(){
    
    glPushMatrix();
    
    
      changeColour(0.3, 0.3, 0.35, 0.0);
            glTranslatef(0.0f, 0.5f,0.0f);
            glBegin(GL_LINES);
    
            glVertex2f(0.0, 0.0);
            glVertex2f(0.0, -0.5);
            glEnd();
    
            glTranslatef(0.0, -0.5, 0.0);
            glutSolidSphere(radius, slices, stacks);
    
    
        glPopMatrix();
    }
    void DrawSphere2(){
    
    glPushMatrix();
      changeColour(0.3, 0.3, 0.35, 0.0);
            glTranslatef(0.0f,0.5f,0.0f);
            glBegin(GL_LINES);
    
            glVertex2f(0.2, 0.0);
            glVertex2f(0.2, -0.5);
            glEnd();
        glTranslatef(0.2, -0.5, 0.0);
    
            glutSolidSphere(radius, slices, stacks);
    
    
        glPopMatrix();
    }
    void display(void) {
    
    bool static sphere= false;
        bool static sphere2=true;
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    
    
    if (sphere){
        angle[0] = angle[0] +1.0f;
    }
    else {
        angle[0] = angle[0] -1.0f;
    }
    if (sphere2){
        angle[1] = angle[1] +1.0f;
    }
    else {
        angle[1] = angle[1] -1.0f;
    }
    
        if(angle[0]<=-20.0f){
        sphere = true;
    
        }
    
    if(angle[0]>=0.0f){
        sphere = false;
    
    }
    glPushMatrix();
        glTranslatef(0.0, 0.5,0.0);
        glRotatef(angle[0],0.0,0.0,1.0);
        glTranslatef(-0.0, -0.5, 0.0);
       DrawSphere();
       glPopMatrix();
    
     if(angle[1]<=0.0f){
        sphere2 = true;
        }
    
    if(angle[1]>=20.0f){
        sphere2 = false;
    }
       glPushMatrix();
       glTranslatef(0.2, 0.5,0.0);
        glRotatef(angle[1],0.0,0.0,1.0);
        glTranslatef(-0.2,-0.5,0.0);
       DrawSphere2();
    
      glPopMatrix();
    
        glutSwapBuffers();
    }
    
    
    void keyboard(unsigned char key, int x, int y) {
    	switch (key) {
    		case 27:
    			exit(0); // Exit the application if 'Esc' key is pressed
    	}
    }
    
    void animate() {
        Sleep(100);
    
        glutPostRedisplay();
    }
    void processMouse(int button, int state, int x, int y){
    if (state==GLUT_DOWN){
    if((button==GLUT_LEFT_BUTTON) && x<350 && x>450){
    glTranslatef(0.0, 0.5,0.0);
    glRotatef(x, 0.0 ,0.0, 1.0);
    glTranslatef(0.0, -0.5,0.0);
    angle[0] = angle[0]+x;
    }
    }
    if (state==GLUT_UP){
    if (button==GLUT_LEFT_BUTTON && angle[0]!=0){
    animate();
    }
    }
    }
    int main(int argc, char * argv[]) {
        glutInit(&argc, argv);
        glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
        glutInitWindowSize (800, 600);
        glutCreateWindow (argv[0]);   init();
        glutKeyboardFunc (keyboard);
        PaintDesktop(hdc);
        glCullFace(GL_BACK);
        glutDisplayFunc (display);
        glutReshapeFunc (reshape);
        glutMouseFunc(processMouse);
        glutIdleFunc(animate);
        glutMainLoop();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Well, at least you make it rather obvious which parts of the code you copy-pasted, and which ones you attempted to write yourself.

    So, welcome to MWAAAHAAA's troubleshooting for beginners. Let's get started with the first step in getting your program on the right track:

    Step #1(a) to solving your problem: properly indent the code you wrote.
    Step #1(b): please explain (in properly indented pseudo-code) the logic you are attempting to implement for achieving your objective.

    We'll get to step #2 once above items have been taken care of.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create a bouncing ball?
    By Swerve in forum C++ Programming
    Replies: 7
    Last Post: 09-12-2008, 03:41 AM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  3. Wireframe 3D engine problem
    By jmgk in forum Game Programming
    Replies: 9
    Last Post: 03-27-2006, 11:14 AM
  4. Display Lists (OpenGL)
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 06-05-2005, 12:11 PM
  5. Mind-Reading chip for the disabled
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-04-2005, 12:44 PM