Trouble

Hey,

I'm trying to setup a square in the middle of the screen that rotates at a consistent rate, and a ball that I can throw at it. I tried doing the same process of shape creation for a ball, but it doesn't seem to like certain values, and crashes at world.createBody. 😠 Also, the cube in the middle seems to constantly increase in its rotational velocity, and I want it to rotate at one steady rate. Here's what I have so far:

(I'm using code from the SVN)
Code:
void Game_Loop (void)
{
   Uint8 *keys;
   bool done = false;
   SDL_Event event;
   unsigned long total_update;

   // Box2D stuffs
   worldAABB.minVertex.Set(0, 0);
   worldAABB.maxVertex.Set(640.0f, 480.0f);
   b2Vec2 gravity;
   gravity.x = 0.0f;
   gravity.y = 0.0f;
   bool doSleep = true;
   b2World world(worldAABB, gravity, doSleep);

   // spinning cube in the center
   b2PolyDef cs;
   cs.type = e_polyShape;
   cs.vertexCount = 4;
   cs.vertices[0].Set(-100, -100);
   cs.vertices[1].Set(100, -100);
   cs.vertices[2].Set(100, 100);
   cs.vertices[3].Set(-100, 100);
   cs.density = 1.0f;
   cs.friction = 1.0f;
   b2BodyDef cube;
   cube.AddShape(&cs);
   cube.position.Set(320.0f, 240.0f);
   cube.angularVelocity = 100.0f;
   b2Body *cubeBody = world.CreateBody(&cube);

   cubeBody->SetAngularVelocity(1);
   // ************

   while(!done) {
      // check FPS timer things

      while(SDL_PollEvent(&event))
      {
         switch(event.type)
         {
            case SDL_KEYUP:
               break;
            case SDL_KEYDOWN: // single key events
               ProcessKey(event.key.keysym.sym);
               break;
            case SDL_MOUSEBUTTONDOWN:
            case SDL_MOUSEBUTTONUP:
               ProcessClick(event.button);
               break;
            case SDL_VIDEORESIZE:
               screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 32,
                     SDL_OPENGL|SDL_RESIZABLE);
               if (screen) {
                  ReSizeGLScene(event.resize.w, event.resize.h);
               } else {
                  // boom.
               }
               break;
            case SDL_QUIT:
               done = 1;
               break;
         }
      }

      modifiers = SDL_GetModState();
      keys = SDL_GetKeyState(NULL); // constant key events

      sys_counter = SDL_GetTicks();
      if (!oldticks)
         oldticks = sys_counter - 1000;

      total_update = sys_counter - oldticks;
      timediff = MIN(total_update, UPDATE_SIZE);
      while (total_update > 0)
      {
         if (!paused)
         {
            world.Step(timediff/1000.0f, 10);
            timecounter += timediff;
         }
         total_update -= timediff;
         timediff = MIN(total_update, UPDATE_SIZE);
      }

      // DRAW SCENE HERE!
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      Setup2D();

      glColor4f(1,1,1,1);
      // DRAW CUBE
      glPushMatrix();
      glTranslatef(cube.position.x, cube.position.y, 0);
      glRotatef(cubeBody->GetRotation(), 0, 0, 1);
      glBegin(GL_LINE_LOOP);
      for (int i=0; i < cs.vertexCount; i++)
         glVertex2f(cs.vertices[i].x, cs.vertices[i].y);
      glEnd();
      glPopMatrix();
      // ******
      // DRAW BALL
/*/      glPushMatrix();
      glTranslatef(cube.position.x, cube.position.y, 0);
      glBegin(GL_LINE_LOOP);
      for (int i=0; i < cs.vertexCount; i++)
         glVertex2f(ss.vertices[i].x, ss.vertices[i].y);
      glEnd();
      glPopMatrix();*/

      FinishGL();

      SDL_GL_SwapBuffers();
      done = terminate ? true : done;
   }
}

I've tried a couple of different methods and I have trouble with each. Is this right? How should I continue?

Thanks,
Entar
I ran your code with a fixed time step of 1/60 and it worked correctly. Try using a fixed time step and see if that helps.

What line did you get the crash on? If you still have trouble, give me the code and I'll debug it.
OK, I've debugged it. It seems that the only valid order in setting putting vertices in a b2PolyDef is from the bottom, counter clockwise. Also, the suggestion about the fixed time step helped. My last question is if my glRotate code is correct? Do I rotate those by direct degrees or do I need to convert something?

EDIT: Nevermind, I got the conversions working.
How can I make my cube completely constant, so that it does not move when it is hit by other objects? I don't want to make it completely static, because I want it to continue rotating, but I want to make it stay where it is. It should also be unaffected by gravity. ❓

Also, is there an easy way to box in my world, so that if a body reaches the edge it bounces off, instead of just flying away? Or do I have to create bodies for walls around the edges?
Entar wrote:
How can I make my cube completely constant, so that it does not move when it is hit by other objects? I don't want to make it completely static, because I want it to continue rotating, but I want to make it stay where it is...

Also, is there an easy way to box in my world, so that if a body reaches the edge it bounces off, instead of just flying away? Or do I have to create bodies for walls around the edges?

To make your cube spin at a fixed location, you'll probably have to create a fixed object (density=0) and attach a box to this object through some kind of join (revolute join I guess). I don't know how to correctly use joins, but we have to be patient, documentation is coming ;P
To box the world I think you must create walls with boxes.
To make the block rotate about a fixed point at a fixed speed is easy. Look at the MotorAndLimitTest.

You want to use a motorized revolute joint. Don't use a mass of zero. Use a large density and a large motor torque. Make the density 2x bigger than anything else. Use one of the wall bodies as the attached body in b2RevoluteJointDef.
Thanks for the info. Started to do something a little different, since my old idea was kinda sucky (though that isn't to say that the new one is very good)... here's what I got:
http://experimentalgameplay.com/game.php?g=515

Original thread: https://box2d.org/forum/viewtopic.php?t=12