Cylinders Acting Crazy

Help
zacaj
2010-04-11
2013-05-02
  • zacaj
    zacaj
    2010-04-11

    Ive been experimenting with Tokamak, and whenever I add cylinders, they act insane when the collide sometimes.
    If you run this code, you have to wait until about 8 have spawned.

    /*
     * GLUT Shapes Demo
     *
     * Written by Nigel Stewart November 2003
     *
     * This program is test harness for the sphere, cone
     * and torus shapes in GLUT.
     *
     * Spinning wireframe and smooth shaded shapes are
     * displayed until the ESC or q key is pressed.  The
     * number of geometry stacks and slices can be adjusted
     * using the + and - keys.
     */
    #define GLUT_DISABLE_ATEXIT_HACK
    #include <windows.h>
    #ifdef __APPLE__
    #include <GLUT/glut.h>
    #else
    #include <GL/glut.h>
    #endif
    #include <tokamak.h>
    #include <stdlib.h>
    #include <time.h>
    static int slices = 16;
    static int stacks = 16;
    #include <vector>
    using namespace std;
    /* GLUT callback Handlers */
    neSimulator *simTok = NULL; // Simulation Object
    neAllocatorDefault all; // Memory Allocator
    vector<neRigidBody*> bodies;
    vector<neAnimatedBody*> abodies;
    neV3 nev(float x,float y,float z)
    {
        neV3 ret;
        ret.v[0]=x;
        ret.v[1]=y;
        ret.v[2]=z;
        return ret;
    }
    neV3 nev(float x)
    {
        neV3 ret;
        ret.v[0]=x;
        ret.v[1]=x;
        ret.v[2]=x;
        return ret;
    }
    void printv(neV3 v)
    {
        printf("(%f,%f,%f)\n",v[0],v[1],v[2]);
    }
    void applyTransform(neT3 TransformMatrix)
    {
        //Apply a tokamak transformation matrix to opengl:
        neT3 t = TransformMatrix;
        GLfloat matrix[] = { t.rot[0][0], t.rot[0][1], t.rot[0][2], 0.0f,
                        t.rot[1][0], t.rot[1][1], t.rot[1][2], 0.0f,
                        t.rot[2][0], t.rot[2][1], t.rot[2][2], 0.0f,
                        t.pos[0],t.pos[1], t.pos[2], 1.0f };
        glMultMatrixf((GLfloat*)&matrix);
    }
    /* Program entry point */
    neRigidBody* addSphere(neV3 pos,float r,float m)
    {
        neRigidBody *rgdBall;
        rgdBall = simTok->CreateRigidBody();
        neGeometry *geoBall = rgdBall->AddGeometry();
        geoBall->SetSphereDiameter(r*2);
        rgdBall->SetAngularDamping(.01);
        rgdBall->UpdateBoundingInfo();
        rgdBall->SetMass(m);
        rgdBall->SetInertiaTensor(neSphereInertiaTensor(r*2,m));
        rgdBall->SetPos(pos);
        bodies.push_back(rgdBall);
        return rgdBall;
    }
    neRigidBody* addBox(neV3 pos,neV3 size,float m)
    {
        neRigidBody *rgd;
        rgd = simTok->CreateRigidBody();
        neGeometry *geoBall = rgd->AddGeometry();
        geoBall->SetBoxSize(size);
        rgd->UpdateBoundingInfo();
        rgd->SetMass(m);
        rgd->SetInertiaTensor(neBoxInertiaTensor(size,m));
        rgd->SetPos(pos);
        bodies.push_back(rgd);
        return rgd;
    }
    neRigidBody* addCapsule(neV3 pos,float d,float h,float m)
    {
        neRigidBody *rgd;
        rgd = simTok->CreateRigidBody();
        rgd->SetMass(m);
        neGeometry *geoBall = rgd->AddGeometry();
        geoBall->SetCylinder(d,h);
        geoBall->SetMaterialIndex(0);
        rgd->SetAngularDamping(.01);
        rgd->UpdateBoundingInfo();
        rgd->SetInertiaTensor(neCylinderInertiaTensor(d,h,m));
        rgd->SetPos(pos);
        bodies.push_back(rgd);
        return rgd;
    }
    neAnimatedBody* addABox(neV3 pos,neV3 size)
    {
        neAnimatedBody *aniFloor;
        aniFloor = simTok->CreateAnimatedBody();
        neGeometry *geoFloor = aniFloor->AddGeometry();
        geoFloor->SetBoxSize(size);
        aniFloor->UpdateBoundingInfo();
        aniFloor->SetPos(pos);
        abodies.push_back(aniFloor);
        return aniFloor;
    }
    static void resize(int width, int height)
    {
        const float ar = (float) width / (float) height;
        glViewport(0, 0, width, height);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        //glFrustum(-320,320, -240, 240.0, 2.0, 1000.0);
        gluPerspective(90,ar,1,1000);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity() ;
    }
    int lasttime=0;
    int add=0;
    static void display(void)
    {
        const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
        const double a = t*90.0;
        glClearColor(0,0,0,1);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glPushMatrix();
        glTranslatef(0,-20,-50);
        glColor3f(1,.8,.8);
        for(int i=0;i<bodies.size();i++)
        {
            glPushMatrix();
                applyTransform(bodies[i]->GetTransform());
                bodies[i]->BeginIterateGeometry();
                for(int geomCount=0;geomCount < bodies[i]->GetGeometryCount();geomCount++)
                {
                    glPushMatrix();
                        neGeometry * geom = bodies[i]->GetNextGeometry();
                        applyTransform(geom->GetTransform());
                        neV3 box;
                        float d,h;
                        if(geom->GetBoxSize(box))
                        {
                            glScalef(box.v[0],box.v[1],box.v[2]);
                            glutSolidCube(1.0f);
                        }
                        else if(geom->GetSphereDiameter(d))
                            glutSolidSphere(d/2,30.0f,30.0f);
                        else if(geom->GetCylinder(d,h))
                        {
                                glRotatef(90.0f,1.0f,0.0f,0.0f);
                                glTranslatef(0.0f,0.0f,-h/2.0f);
                                gluCylinder(gluNewQuadric(),d/2.0f,d/2.0f,h,30,30);
                                glutSolidSphere(d/2.0f,30,30);
                                glTranslatef(0.0f,0.0f,+h);
                                glutSolidSphere(d/2.0f,30,30);
                        }
                    glPopMatrix();
                }
            glPopMatrix();
        }
        glColor3f(.8,.8,1);
        for(int i=0;i<abodies.size();i++)
        {
            glPushMatrix();
                applyTransform(abodies[i]->GetTransform());
                abodies[i]->BeginIterateGeometry();
                for(int geomCount=0;geomCount < abodies[i]->GetGeometryCount();geomCount++)
                {
                    glPushMatrix();
                        neGeometry * geom = abodies[i]->GetNextGeometry();
                        applyTransform(geom->GetTransform());
                        neV3 box;
                        float d;
                        if(geom->GetBoxSize(box))
                        {
                            glScalef(box.v[0],box.v[1],box.v[2]);
                            glutSolidCube(1.0f);
                        }
                        else if(geom->GetSphereDiameter(d))
                            glutSolidSphere(d/2,30.0f,30.0f);
                    glPopMatrix();
                }
            glPopMatrix();
        }
        glPopMatrix();
        simTok->Advance(1.f/60.f,10);
        glutSwapBuffers();
        if(clock()-lasttime>CLOCKS_PER_SEC/60)
            printf("push");
        while(clock()-lasttime<CLOCKS_PER_SEC/60);
        lasttime=clock();
        if(add++>175)
        {
            addCapsule(nev(rand()%10*.1-.5,40,0),3,4,100);
            add=0;
        }
    }
    static void key(unsigned char key, int x, int y)
    {
        switch (key)
        {
            case 27 :
            case 'q':
                exit(0);
                break;
            case '+':
                slices++;
                stacks++;
                break;
            case '-':
                if (slices>3 && stacks>3)
                {
                    slices--;
                    stacks--;
                }
                break;
        }
        glutPostRedisplay();
    }
    static void idle(void)
    {
        glutPostRedisplay();
    }
    const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
    const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
    const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
    const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
    const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
    const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
    const GLfloat high_shininess[] = { 100.0f };
    int main(int argc, char *argv[])
    {
        glutInit(&argc, argv);
        glutInitWindowSize(640,480);
        glutInitWindowPosition(10,10);
        glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
        glutCreateWindow("GLUT Shapes");
        glutReshapeFunc(resize);
        glutDisplayFunc(display);
        glutKeyboardFunc(key);
        glutIdleFunc(idle);
        glClearColor(1,1,1,1);
        //glEnable(GL_CULL_FACE);
       // glCullFace(GL_BACK);
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LESS);
        glEnable(GL_LIGHT0);
        glEnable(GL_NORMALIZE);
        glEnable(GL_COLOR_MATERIAL);
        glEnable(GL_LIGHTING);
        glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
        glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
        glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
        glLightfv(GL_LIGHT0, GL_POSITION, light_position);
        glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
        glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
        glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
        glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
        neV3 gravity; gravity.Set(0.0f, -9.8f, 0.0f);
        neSimulatorSizeInfo sizeInfo;
        sizeInfo.rigidBodiesCount = 1000;
        sizeInfo.animatedBodiesCount = 100;
        sizeInfo.geometriesCount = sizeInfo.animatedBodiesCount+sizeInfo.rigidBodiesCount;
        sizeInfo.overlappedPairsCount = ((sizeInfo.geometriesCount)*(sizeInfo.geometriesCount-1))/2;
        simTok = neSimulator::CreateSimulator(sizeInfo,NULL, &gravity);
        simTok->SetMaterial(0,.5,0);
        addBox(nev(0,10,0),nev(4,4,4),5);
        addSphere(nev(0,50,0),2,5);
        addABox(nev(0,-10,0),nev(50,2,50));
        glutMainLoop();
        return EXIT_SUCCESS;
    }