Ship Rotation

Started by AGP, October 08, 2008, 03:31:01 AM

Previous topic - Next topic

AGP

How do I make it so that a ship is rotated around its own axis? As I understand it, objects are rotated by the world axis, so that the moment I rotate a ship in any of its 3 axis, it's no longer aligned with the worls axis. Or am I far off here?

paulscode

You can rotate an object around any axis by using the 'rotateAxis' method:

someObject3D.rotateAxis( someObject3D.getXAxis(), angle );

AGP

Thanks a lot, that was simple!

Another question: I'm getting crashes with a second thread in hardware OpenGL mode. It doesn't happen in software mode. Is there anything about OpenGL or otherwise lwjgl that can't be multi-threaded?

EgonOlsen

Do everything related to jPCT in one thread or synchronize your threads properly. jPCT (like Swing) isn't thread safe. In addition, the OpenGL context is bound to one thread, which makes things even more difficult. Software mode may be more forgiving then hardware mode. What kind of crash do you get?

AGP

The main thread (in static method main(String[]) calls draw() once, after which time all it does is check the keyboard. When you press ENTER a new thread, the game loop, starts. But in OpenGL mode, all I'm getting is a frozen window. I have to have Windows kill the process.

EgonOlsen

AWTGL or GL? You may try to start it in a console and press CTRL+Scroll/Lock to get a thread dump...just to ensure that your frozen window isn't caused by a dead lock. The AWTGLRenderer is prone to such things when used wrong.

AGP

GL only. The latest hardware attempt was single-threaded. It still crashes. It's very weird because I've had a lot of success with the hardware renderer in the past, and I can't see anything wrong with this code. All ENTER is doing in this version is flipping gameOn.


      private void draw() {
// buffer.clearZBufferOnly();
buffer.clear(Color.black);
xWing.rotateAxis(xWing.getXAxis(), rotationStored.x);
xWing.rotateAxis(xWing.getYAxis(), rotationStored.y);
xWing.rotateAxis(xWing.getZAxis(), rotationStored.z);
if (!cameraOnTie)
    follow();
rotationStored.x = 0;
rotationStored.y = 0;
rotationStored.z = 0;
buffer.update();

theWorld.renderScene(buffer);
theWorld.draw(buffer);
drawAxisRep();
if (!openGL)
    buffer.display(this.getGraphics());
else buffer.displayGLOnly();
      }
      public static void main(String[] args) {
boolean hardware = false;
if (args != null && args.length > 0) {
      if (args[0].equalsIgnoreCase("OpenGL"))
hardware = true;
}
RogueSquadron instance = new RogueSquadron(hardware);
if (instance.openGL) {
      instance.buffer = new FrameBuffer(800, 600, FrameBuffer.SAMPLINGMODE_HARDWARE_ONLY);

      try {
Keyboard.create();
      }
      catch (Exception e) {System.out.println("Keyboard failed: "+e.getMessage());}
      glLoop(instance);
      Keyboard.destroy();
      instance.buffer.disableRenderer(IRenderer.RENDERER_OPENGL);
      instance.buffer.dispose();
}
      }
      private static void glLoop(RogueSquadron instance) {
int keyCode;
instance.buffer.enableRenderer(IRenderer.RENDERER_OPENGL);
instance.buffer.disableRenderer(IRenderer.RENDERER_SOFTWARE);
instance.draw();

while (!Display.isCloseRequested()) {
      keyCode = Keyboard.getEventKey();
      instance.keyPressed(keyCode);
      if (instance.gameOn) {
instance.moveTowards(instance.xWing, instance.targetCone.getTransformedCenter(), .01f);
instance.follow();
if (instance.wingsHaveChanged)
      instance.changeWings();
try {
Thread.sleep(50);
}
catch (InterruptedException e) {}
      }
}
instance.gameOn = false;
      }

EgonOlsen

What kind of "crash"? A stack trace, a VM dump, a dead lock? Where in your code is draw being called except that one time after initializing the renderer? It's not advised to query the keyboard in another thread than the one that does the rendering and you have to have regular calls to Display.update() (which buffer.displayGLOnly() does) to receive new key events.
IIRC, you may run into a dead lock condition if you don't.

AGP

The frame stops responding, but I'm not getting any command prompt messages. I always use the command prompt and a simple text editor, by the way. And you may add a instance.draw() inside the while (gameOn) loop (it doesn't really matter since it's never rendering it the second time around).

EgonOlsen

Try to move the keyboard stuff out of the second thread and into the rendering loop, i.e. into the thread that has created the frame buffer and such. Working with multiple threads in LWJGL never was good idea and may have all kinds of side effects.

AGP

FYI, it was the lack of a Keyboard.next() check that was crashing it. How annoying.

EgonOlsen

Quote from: AGP on October 29, 2008, 12:06:37 AM
FYI, it was the lack of a Keyboard.next() check that was crashing it. How annoying.
Keyboard.next() is gone in LWJGL2.0? I haven't noticed that. The keyboard always used to change over the versions. IMO to the worse, but i guess that there were good reasons to change it constantly.

AGP

No, I meant it was missing from my code. And I do think the class changed a little, because I don't remember the getKeyEventState() method, but I like it. I'll have to change every program that uses it (lest I get two events, key down and key up, for every key press).