Java OpenGL nézési irány egérrel

Java OpenGL nézési irány egérrel
2011-11-15T18:48:01+01:00
2011-11-16T12:39:14+01:00
2022-11-23T17:50:38+01:00
adam21.
Üdv!

A programomba azt szeretném megoldani, hogy egér hatására lehessen a nézési irányt megváltoztatni. Ugyanolyan módon mint pl.: vannak a lövöldözős játékok, abban is úgy lehet egérrel mozogni, hogy a camera a saját tengelye körül forgatható, de minden irányba és ha előre megyek akkor amerre nézek az egérrel arra megyek.
Van egy metódusom amibe a mozgásokat valósítottam meg. Ezen belül van egy ilyen sor:
gl.glTranslatef((float)balrajobbra, 0.0f, (float)-elorehatra);
A W, S, -el tudok előre menni, az A, S-el pedig oldalra.
Az előre, hátra, jobbra, balra nyilakkal pedig tudok forogni. Ezeket a
gl.glRotated(rotx, 0.0, 1.0, 0.0); gl.glRotated(roty, 1.0, 0.0, 0.0);
függvényekkel oldottam meg. Ezzel viszont az a gondom, hogy nem a legszebb megoldást eredményezi. Ezeket szeretném kiváltani az egérrel. Hogyan tehetném meg ezt egyszerűbben? Köszönöm előre is!
Mutasd a teljes hozzászólást!
A gamedev.net fórumán találkoztam ezzel:

public class FirstPersonCam implements Camera, KeyListener, MouseMotionListener, GLEventListener { private static final int forward = KeyEvent.VK_W; private static final int backward = KeyEvent.VK_S; private static final int leftward = KeyEvent.VK_A; private static final int rightward = KeyEvent.VK_D; private static final int space = KeyEvent.VK_SPACE; private static final int shift = KeyEvent.VK_SHIFT; private static final int mouseSpeed = 1; private static final int keySpeed = 3; private static final int rotVMax = 1; private static final int rotVMin = -1; private final Map<Integer, Boolean> keys = new ConcurrentHashMap<Integer, Boolean>(); private final float[] modelview = new float[16]; private final GLCanvas canvas; private final Robot robot; private float rotX, rotY, rotZ, rotV; private float posX, posY, posZ; private int centerX, centerY; private long lastTime = -1; // a very simple first person shooter style camera public FirstPersonCam(final GLCanvas canvas) { // we need the robot to get full control over the mouse Robot r = null; try { r = new Robot(); } catch (final AWTException e) { e.printStackTrace(); } // setup the modelview matrix for (int i = 0; i < 4; i++) modelview[i * 5] = 1.0f; this.canvas = canvas; this.robot = r; canvas.addKeyListener(this); canvas.addMouseMotionListener(this); canvas.addGLEventListener(this); } /** * Berrechnet die aktuelle View Matrix aus der aktuellen Kamera Position */ public ViewMatrix getViewMatrix() { calculatePosition(); calculateModelview(); return new ViewMatrix(modelview); } private void calculatePosition() { if (lastTime == -1) lastTime = System.nanoTime(); double mul = 1.0; Boolean value = null; if ((value = keys.get(shift)) != null && value == true) mul = 0.1; final double speed = keySpeed * -((lastTime - (lastTime = System.nanoTime())) / 10E7) * mul; if ((value = keys.get(forward)) != null && value == true) { posX -= Math.sin(rotY) * speed; posZ += Math.cos(rotY) * speed; posY += rotV * speed; } if ((value = keys.get(backward)) != null && value == true) { posX += Math.sin(rotY) * speed; posZ -= Math.cos(rotY) * speed; posY -= rotV * speed; } if ((value = keys.get(leftward)) != null && value == true) { posX += Math.cos(rotY) * speed; posZ += Math.sin(rotY) * speed; } if ((value = keys.get(rightward)) != null && value == true) { posX -= Math.cos(rotY) * speed; posZ -= Math.sin(rotY) * speed; } } private void calculateModelview() { final float sinX = (float) Math.sin(rotX); final float sinY = (float) Math.sin(rotY); final float sinZ = (float) Math.sin(rotZ); final float cosX = (float) Math.cos(rotX); final float cosY = (float) Math.cos(rotY); final float cosZ = (float) Math.cos(rotZ); modelview[0] = cosY * cosZ + sinY * sinX * sinZ; modelview[1] = cosX * sinZ; modelview[2] = -sinY * cosZ + cosY * sinX * sinZ; modelview[4] = -cosY * sinZ + sinY * sinX * cosZ; modelview[5] = cosX * cosZ; modelview[6] = sinY * sinZ + cosY * sinX * cosZ; modelview[8] = sinY * cosX; modelview[9] = -sinX; modelview[10] = cosY * cosX; modelview[12] = modelview[0] * posX + modelview[4] * posY + modelview[8] * posZ; modelview[13] = modelview[1] * posX + modelview[5] * posY + modelview[9] * posZ; modelview[14] = modelview[2] * posX + modelview[6] * posY + modelview[10] * posZ; } public void setPosition(float x, float y, float z){ posX = x; posY = y; posZ = z; } @Override public void init(final GLAutoDrawable drawable) {} @Override public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { rotV = 0; final Rectangle r = canvas.getParent().getBounds(); final Point p = canvas.getParent().getLocationOnScreen(); centerX = r.x + p.x + width / 2; centerY = r.y + p.y + height / 2; Boolean value = null; if ((value = keys.get(space)) != null && value == true) if (robot != null) robot.mouseMove(centerX, centerY); } @Override public void mouseMoved(final MouseEvent e) { Boolean value = null; if ((value = keys.get(space)) != null && value == true) { rotY -= (centerX - e.getXOnScreen()) / 1000.0 * mouseSpeed; rotV -= (centerY - e.getYOnScreen()) / 1000.0 * mouseSpeed; if (rotV > rotVMax) rotV = rotVMax; if (rotV < rotVMin) rotV = rotVMin; rotX = (float) Math.cos(rotY) * rotV; rotZ = (float) Math.sin(rotY) * rotV; if (robot != null) robot.mouseMove(centerX, centerY); } } @Override public void mouseDragged(final MouseEvent e) { mouseMoved(e); } @Override public void keyPressed(final KeyEvent e) { keys.put(e.getKeyCode(), true); } @Override public void keyReleased(final KeyEvent e) { keys.put(e.getKeyCode(), false); } @Override public void keyTyped(final KeyEvent e) { } @Override public void display(final GLAutoDrawable drawable) { } public void dispose(GLAutoDrawable drawable) { } }

Remélem ez jó lesz.
Mutasd a teljes hozzászólást!

  • Ahogy én utánanéztem, az egyik lehetőséged a JOGL használata.
    Ehhez van fent pár tutorial (Google a barátod:)).
    Másik az LWJGL, szintén van fent tutrial hozzá.

    Egyébként az egyik legjobb oktatóanyag az OpenGL-hez: NeHe Productions - Everything OpenGL
    Mutasd a teljes hozzászólást!
  • JOGL-ot használok. Elég sok tutorialt megnéztem már, de egyik sem volt az igazi, ezért várom egy-két OpenGL mágus ötletét:)
    Mutasd a teljes hozzászólást!
  • A gamedev.net fórumán találkoztam ezzel:

    public class FirstPersonCam implements Camera, KeyListener, MouseMotionListener, GLEventListener { private static final int forward = KeyEvent.VK_W; private static final int backward = KeyEvent.VK_S; private static final int leftward = KeyEvent.VK_A; private static final int rightward = KeyEvent.VK_D; private static final int space = KeyEvent.VK_SPACE; private static final int shift = KeyEvent.VK_SHIFT; private static final int mouseSpeed = 1; private static final int keySpeed = 3; private static final int rotVMax = 1; private static final int rotVMin = -1; private final Map<Integer, Boolean> keys = new ConcurrentHashMap<Integer, Boolean>(); private final float[] modelview = new float[16]; private final GLCanvas canvas; private final Robot robot; private float rotX, rotY, rotZ, rotV; private float posX, posY, posZ; private int centerX, centerY; private long lastTime = -1; // a very simple first person shooter style camera public FirstPersonCam(final GLCanvas canvas) { // we need the robot to get full control over the mouse Robot r = null; try { r = new Robot(); } catch (final AWTException e) { e.printStackTrace(); } // setup the modelview matrix for (int i = 0; i < 4; i++) modelview[i * 5] = 1.0f; this.canvas = canvas; this.robot = r; canvas.addKeyListener(this); canvas.addMouseMotionListener(this); canvas.addGLEventListener(this); } /** * Berrechnet die aktuelle View Matrix aus der aktuellen Kamera Position */ public ViewMatrix getViewMatrix() { calculatePosition(); calculateModelview(); return new ViewMatrix(modelview); } private void calculatePosition() { if (lastTime == -1) lastTime = System.nanoTime(); double mul = 1.0; Boolean value = null; if ((value = keys.get(shift)) != null && value == true) mul = 0.1; final double speed = keySpeed * -((lastTime - (lastTime = System.nanoTime())) / 10E7) * mul; if ((value = keys.get(forward)) != null && value == true) { posX -= Math.sin(rotY) * speed; posZ += Math.cos(rotY) * speed; posY += rotV * speed; } if ((value = keys.get(backward)) != null && value == true) { posX += Math.sin(rotY) * speed; posZ -= Math.cos(rotY) * speed; posY -= rotV * speed; } if ((value = keys.get(leftward)) != null && value == true) { posX += Math.cos(rotY) * speed; posZ += Math.sin(rotY) * speed; } if ((value = keys.get(rightward)) != null && value == true) { posX -= Math.cos(rotY) * speed; posZ -= Math.sin(rotY) * speed; } } private void calculateModelview() { final float sinX = (float) Math.sin(rotX); final float sinY = (float) Math.sin(rotY); final float sinZ = (float) Math.sin(rotZ); final float cosX = (float) Math.cos(rotX); final float cosY = (float) Math.cos(rotY); final float cosZ = (float) Math.cos(rotZ); modelview[0] = cosY * cosZ + sinY * sinX * sinZ; modelview[1] = cosX * sinZ; modelview[2] = -sinY * cosZ + cosY * sinX * sinZ; modelview[4] = -cosY * sinZ + sinY * sinX * cosZ; modelview[5] = cosX * cosZ; modelview[6] = sinY * sinZ + cosY * sinX * cosZ; modelview[8] = sinY * cosX; modelview[9] = -sinX; modelview[10] = cosY * cosX; modelview[12] = modelview[0] * posX + modelview[4] * posY + modelview[8] * posZ; modelview[13] = modelview[1] * posX + modelview[5] * posY + modelview[9] * posZ; modelview[14] = modelview[2] * posX + modelview[6] * posY + modelview[10] * posZ; } public void setPosition(float x, float y, float z){ posX = x; posY = y; posZ = z; } @Override public void init(final GLAutoDrawable drawable) {} @Override public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { rotV = 0; final Rectangle r = canvas.getParent().getBounds(); final Point p = canvas.getParent().getLocationOnScreen(); centerX = r.x + p.x + width / 2; centerY = r.y + p.y + height / 2; Boolean value = null; if ((value = keys.get(space)) != null && value == true) if (robot != null) robot.mouseMove(centerX, centerY); } @Override public void mouseMoved(final MouseEvent e) { Boolean value = null; if ((value = keys.get(space)) != null && value == true) { rotY -= (centerX - e.getXOnScreen()) / 1000.0 * mouseSpeed; rotV -= (centerY - e.getYOnScreen()) / 1000.0 * mouseSpeed; if (rotV > rotVMax) rotV = rotVMax; if (rotV < rotVMin) rotV = rotVMin; rotX = (float) Math.cos(rotY) * rotV; rotZ = (float) Math.sin(rotY) * rotV; if (robot != null) robot.mouseMove(centerX, centerY); } } @Override public void mouseDragged(final MouseEvent e) { mouseMoved(e); } @Override public void keyPressed(final KeyEvent e) { keys.put(e.getKeyCode(), true); } @Override public void keyReleased(final KeyEvent e) { keys.put(e.getKeyCode(), false); } @Override public void keyTyped(final KeyEvent e) { } @Override public void display(final GLAutoDrawable drawable) { } public void dispose(GLAutoDrawable drawable) { } }

    Remélem ez jó lesz.
    Mutasd a teljes hozzászólást!
Tetszett amit olvastál? Szeretnél a jövőben is értesülni a hasonló érdekességekről?
abcd