Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

i have a cube with texture but when i add 3 balls so that they bounce on the walls of the cubes everything seemed to be wrong,the balls are not bouncing in the right directions, i want them to bounce on the walls without collision ,how can i make this work nicely?? this is my code below i just pasted everything

#include <iostream>
#include <stdlib.h>

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include "imageloader.h"
#include <Eigen/Core>
using namespace Eigen;

using namespace std;

void handleKeypress(unsigned char key, int x, int y) {
    switch (key) {
        case 27: //Escape key
            exit(0);
    }
}

//Makes the image into a texture, and returns the id of the texture
GLuint loadTexture(Image* image) {
    GLuint textureId;
    glGenTextures(1, &textureId); //Make room for our texture
    glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
    //Map the image to the texture
    glTexImage2D(GL_TEXTURE_2D,                //Always GL_TEXTURE_2D
                 0,                            //0 for now
                 GL_RGB,                       //Format OpenGL uses for image
                 image->width, image->height,  //Width and height
                 0,                            //The border of the image
                 GL_RGB, //GL_RGB, because pixels are stored in RGB format
                 GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
                                   //as unsigned numbers
                 image->pixels);               //The actual pixel data
    return textureId; //Returns the id of the texture
}
struct PointMass
{
    PointMass() : pos(0,0,0), vel(0,0,0) {}
    PointMass( const Vector3f& pos )
        : pos( pos )
        , vel( 0, 0, 0 )
    {}

    void Integrate( float dt )
    {
        // "gravity" force vector
        Vector3f g( 0, 0, -2 );

        // semi-implicit euler
        vel = vel + g * dt;
        pos = pos + vel * dt;

        // collision detection/response
        if( pos.z() < 0 )
        {
            pos.z() = -pos.z();
            vel.z() = -vel.z();
        }
    }

    Vector3f pos;
    Vector3f vel;
};
GLfloat WHITE[] = {1, 1, 1, 1};
GLfloat RED[] = {1, 0, 0, 1};
GLfloat GREEN[] = {0, 1, 0, 1};
GLfloat MAGENTA[] = {0, 0, 1, 1};

class Ball 
{
    double radius;
    GLfloat* color;
    PointMass pm;
public:
    Ball(double r, GLfloat* c, const Vector3f& pos )
        : radius(r)
        , color(c)
        , pm( pos )
    { }

    void Integrate( float dt )
    {
        pm.Integrate( dt );
    }

    void Draw()
    {
        glPushMatrix();
        glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
        glColor4fv( color );
        glTranslatef( pm.pos.x(), pm.pos.y(), pm.pos.z() );
        glutSolidSphere(radius, 30, 30);
        glPopMatrix();
    }
};

Ball balls[] = 
{
    Ball( 0.1,   GREEN,      Vector3f( 1, 1, 2 )    ),
    Ball( 0.1,   MAGENTA,    Vector3f( -1, 1, 1 )   ),
    Ball( 0.1,   WHITE,      Vector3f( 0, -1, 1.5 ) ),
};
double rotate_x = 55; 
double rotate_z = 25;   
GLuint _textureId; //The id of the texture

void initRendering() {
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);

    Image* image = loadBMP("vtr.bmp");
    _textureId = loadTexture(image);
    delete image;
}

void handleResize(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (float)w / (float)h, 1.0, 200.0);
}

void drawScene() {
    static int last = glutGet(GLUT_ELAPSED_TIME);
    int cur = glutGet(GLUT_ELAPSED_TIME);
    float dt = ( cur - last ) / 1000.0f;
    last = cur;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();


    glTranslatef(0.0f, 1.0f, -6.0f);

    GLfloat ambientLight[] = {0.2f, 0.2f, 0.2f, 1.0f};
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);

    GLfloat directedLight[] = {0.7f, 0.7f, 0.7f, 1.0f};
    GLfloat directedLightPos[] = {-10.0f, 15.0f, 20.0f, 0.0f};
    glLightfv(GL_LIGHT0, GL_DIFFUSE, directedLight);
    glLightfv(GL_LIGHT0, GL_POSITION, directedLightPos);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, _textureId);

    //Bottom
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glColor3f(1.0f, 0.2f, 0.2f);
    glBegin(GL_QUADS);

    glNormal3f(0.0, 1.0f, 0.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-2.5f, -2.5f, 2.5f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(2.5f, -2.5f, 2.5f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(2.5f, -2.5f, -2.5f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-2.5f, -2.5f, -2.5f);

    glEnd();

    //Back
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glColor3f(1.0f, 1.0f, 1.0f);
    glBegin(GL_TRIANGLES);

    glNormal3f(0.0f, 0.0f, 1.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-2.5f, -2.5f, -2.5f);
    glTexCoord2f(5.0f, 5.0f);
    glVertex3f(0.0f, 2.5f, -2.5f);
    glTexCoord2f(10.0f, 0.0f);
    glVertex3f(2.5f, -2.5f, -2.5f);

    glEnd();

    //Left
    glDisable(GL_TEXTURE_2D);
    glColor3f(1.0f, 0.7f, 0.3f);
    glBegin(GL_QUADS);

    glNormal3f(1.0f, 0.0f, 0.0f);
    glVertex3f(-2.5f, -2.5f, 2.5f);
    glVertex3f(-2.5f, -2.5f, -2.5f);
    glVertex3f(-2.5f, 2.5f, -2.5f);
    glVertex3f(-2.5f, 2.5f, 2.5f);

    glEnd();

    for (int i = 0; i < sizeof balls / sizeof(Ball); i++) 
    {
        balls[i].Integrate( dt );
        balls[i].Draw();
    }
    glFlush();
    glutSwapBuffers();
}

void timer( int extra )
{
    // run display() every 16ms or so
    glutTimerFunc( 16, timer, 0 );
    glutPostRedisplay();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(400, 400);

    glutCreateWindow("Textures");
    initRendering();

    glutDisplayFunc(drawScene);
    glutTimerFunc( 0, timer, 0 );
    glutKeyboardFunc(handleKeypress);
    glutReshapeFunc(handleResize);

    glutMainLoop();
    return
share|improve this question

closed as too localized by msell, Byte56, bummzack, Josh Petrie, Anko Apr 21 '13 at 23:43

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

Browse other questions tagged or ask your own question.