Author Topic: Ship Rotation  (Read 8105 times)

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Ship Rotation
« on: October 08, 2008, 03:31:01 am »
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?

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Ship Rotation
« Reply #1 on: October 08, 2008, 01:26:50 pm »
You can rotate an object around any axis by using the 'rotateAxis' method:

Code: [Select]
someObject3D.rotateAxis( someObject3D.getXAxis(), angle );

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Ship Rotation
« Reply #2 on: October 08, 2008, 09:43:32 pm »
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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Ship Rotation
« Reply #3 on: October 08, 2008, 10:00:41 pm »
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?

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Ship Rotation
« Reply #4 on: October 09, 2008, 08:12:31 pm »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Ship Rotation
« Reply #5 on: October 09, 2008, 09:23:06 pm »
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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Ship Rotation
« Reply #6 on: October 11, 2008, 08:38:15 am »
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.

Code: [Select]
      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;
      }

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Ship Rotation
« Reply #7 on: October 11, 2008, 09:11:02 am »
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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Ship Rotation
« Reply #8 on: October 11, 2008, 10:00:35 am »
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).

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Ship Rotation
« Reply #9 on: October 11, 2008, 11:14:12 am »
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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Ship Rotation
« Reply #10 on: October 29, 2008, 12:06:37 am »
FYI, it was the lack of a Keyboard.next() check that was crashing it. How annoying.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Ship Rotation
« Reply #11 on: October 29, 2008, 11:29:21 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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Ship Rotation
« Reply #12 on: October 29, 2008, 03:53:38 pm »
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).