added keybindings to allow jumping to planets

This commit is contained in:
Hannes
2018-05-10 13:28:22 +02:00
parent 1ac7cbcefa
commit 33acc16389
3 changed files with 251 additions and 266 deletions

View File

@@ -44,6 +44,10 @@ public abstract class GraphicalObject {
this.stopWatch.start();
}
public Vector3f getPosition() {
return transformation.getTranslation(new Vector3f());
}
public void setPosition(final Vector3f pos) {
this.transformation = transformation.setTranslation(pos);
}

View File

@@ -18,104 +18,64 @@ public class SolarSystemScene extends Scene {
camera.lookAt(new Vector3f(0.0f, 0.0f, 0.0f));
final Universe uv = new Universe();
this.getCanvas().addKeyListener(new UniverseKeyListener(uv));
final UniverseKeyListener universeKeyListener = new UniverseKeyListener(uv, camera);
this.getCanvas().addKeyListener(universeKeyListener);
// Mercury
this.addGraphicalObject(new Planet(
new Orbit(new Vector3f(0.0f, 0.0f, 0.0f),
0.467f,
0.308f),
uv,
0.38f,
"spheres/mercury.jpg", 115.88f, 57.94f));
final Planet mercury = new Planet(new Orbit(new Vector3f(0.0f, 0.0f, 0.0f), 0.467f, 0.308f), uv, 0.38f,
"spheres/mercury.jpg", 115.88f, 57.94f);
this.addGraphicalObject(mercury);
universeKeyListener.setKeySphericalSpaceObject(mercury, 2);
// Venus
this.addGraphicalObject(new Planet(
new Orbit(new Vector3f(0.0f, 0.0f, 0.0f),
0.723f,
0.718f),
uv,
0.38f,
"spheres/venus.jpg", 224.701f, 243.0f));
final Planet venus = new Planet(new Orbit(new Vector3f(0.0f, 0.0f, 0.0f), 0.723f, 0.718f), uv, 0.38f,
"spheres/venus.jpg", 224.701f, 243.0f);
this.addGraphicalObject(venus);
universeKeyListener.setKeySphericalSpaceObject(venus, 3);
// Earth
final Planet earth = new Planet(
new Orbit(new Vector3f(0.0f, 0.0f, 0.0f),
0.98f,
1.02f),
uv,
1.0f,
final Planet earth = new Planet(new Orbit(new Vector3f(0.0f, 0.0f, 0.0f), 0.98f, 1.02f), uv, 1.0f,
"spheres/earth.jpg", 365.256f, 1.0f);
this.addGraphicalObject(earth);
universeKeyListener.setKeySphericalSpaceObject(earth, 4);
// Moon
this.addGraphicalObject(new Moon(
earth,
new Orbit(new Vector3f(0.0f, 0.0f, 0.0f),
2 * Universe.EARTH_RADIUS,
2 * Universe.EARTH_RADIUS),
uv,
0.3f,
"spheres/moon.jpg",
29.530589f,
29.530589f));
this.addGraphicalObject(new Moon(earth,
new Orbit(new Vector3f(0.0f, 0.0f, 0.0f), 2 * Universe.EARTH_RADIUS, 2 * Universe.EARTH_RADIUS), uv,
0.3f, "spheres/moon.jpg", 29.530589f, 29.530589f));
// Mars
this.addGraphicalObject(new Planet(
new Orbit(new Vector3f(0.0f, 0.0f, 0.0f),
1.5f,
1.4f),
uv,
0.2657f * 2,
"spheres/mars.jpg",
686.971f,
1.0275f));
final Planet mars = new Planet(new Orbit(new Vector3f(0.0f, 0.0f, 0.0f), 1.5f, 1.4f), uv, 0.2657f * 2,
"spheres/mars.jpg", 686.971f, 1.0275f);
this.addGraphicalObject(mars);
universeKeyListener.setKeySphericalSpaceObject(mars, 5);
// Jupiter
this.addGraphicalObject(new Planet(
new Orbit(new Vector3f(0.0f, 0.0f, 0.0f),
5.2f - 3.3f,
4.9f - 3.3f),
uv,
12.0f / 12f,
"spheres/jupiter.jpg",
4330.0f,
0.4132f));
final Planet jupiter = new Planet(new Orbit(new Vector3f(0.0f, 0.0f, 0.0f), 5.2f - 3.3f, 4.9f - 3.3f), uv,
12.0f / 12f, "spheres/jupiter.jpg", 4330.0f, 0.4132f);
this.addGraphicalObject(jupiter);
universeKeyListener.setKeySphericalSpaceObject(jupiter, 6);
// Saturn
this.addGraphicalObject(new Planet(
new Orbit(new Vector3f(0.0f, 0.0f, 0.0f),
9.5f - 7f,
9f - 7f),
uv,
10.0f / 10f,
"spheres/saturn.jpg",
10751.805f,
0.45f));
final Planet saturn = new Planet(new Orbit(new Vector3f(0.0f, 0.0f, 0.0f), 9.5f - 7f, 9f - 7f), uv, 10.0f / 10f,
"spheres/saturn.jpg", 10751.805f, 0.45f);
this.addGraphicalObject(saturn);
universeKeyListener.setKeySphericalSpaceObject(saturn, 7);
// Uranus
this.addGraphicalObject(new Planet(
new Orbit(new Vector3f(0.0f, 0.0f, 0.0f),
19f - 16f,
18f - 16f),
uv,
4.0f / 6,
"spheres/uranus.jpg",
30660.0f,
0.72f));
final Planet uranus = new Planet(new Orbit(new Vector3f(0.0f, 0.0f, 0.0f), 19f - 16f, 18f - 16f), uv, 4.0f / 6,
"spheres/uranus.jpg", 30660.0f, 0.72f);
this.addGraphicalObject(uranus);
universeKeyListener.setKeySphericalSpaceObject(uranus, 8);
// Neptun
this.addGraphicalObject(new Planet(
new Orbit(new Vector3f(0.0f, 0.0f, 0.0f),
30f - 26.0f,
29f - 26.0f),
uv,
3.88f / 6,
"spheres/neptune.jpg",
30660.0f,
0.673f));
this.addGraphicalObject(new Sun(new Vector3f(0.0f, 0.0f, 0.0f), 3.0f, "spheres/sun.jpg"));
final Planet neptun = new Planet(new Orbit(new Vector3f(0.0f, 0.0f, 0.0f), 30f - 26.0f, 29f - 26.0f), uv,
3.88f / 6, "spheres/neptune.jpg", 30660.0f, 0.673f);
this.addGraphicalObject(neptun);
universeKeyListener.setKeySphericalSpaceObject(neptun, 9);
this.setSkybox(new String[] {
"skybox/space_left_0.png",
"skybox/space_right_1.png",
"skybox/space_down_2.png",
"skybox/space_up_3.png",
"skybox/space_front_4.png",
"skybox/space_back_5.png"});
final Sun sun = new Sun(new Vector3f(0.0f, 0.0f, 0.0f), 3.0f, "spheres/sun.jpg");
this.addGraphicalObject(sun);
universeKeyListener.setKeySphericalSpaceObject(sun, 1);
this.setSkybox(new String[] { "skybox/space_left_0.png", "skybox/space_right_1.png", "skybox/space_down_2.png",
"skybox/space_up_3.png", "skybox/space_front_4.png", "skybox/space_back_5.png" });
}
}

View File

@@ -3,13 +3,19 @@ package org.kuchelmeister.solarsystem.input;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import org.kuchelmeister.engine.camera.Camera;
import org.kuchelmeister.solarsystem.geometry.SphericalSpaceObject;
import org.kuchelmeister.solarsystem.geometry.Universe;
public class UniverseKeyListener implements KeyListener {
Universe universe;
Camera camera;
SphericalSpaceObject[] spaceObjects;
public UniverseKeyListener(final Universe universe) {
public UniverseKeyListener(final Universe universe, final Camera camera) {
this.universe = universe;
this.spaceObjects = new SphericalSpaceObject[10];
this.camera = camera;
}
@Override
@@ -31,6 +37,21 @@ public class UniverseKeyListener implements KeyListener {
System.out.println("Time scale halfed to: " + universe.getTimeScale());
}
}
// Jumping to the position
if (KeyEvent.VK_0 <= e.getKeyCode() && e.getKeyCode() <= KeyEvent.VK_9) {
final int position = e.getKeyCode() - KeyEvent.VK_0;
final SphericalSpaceObject sObject = spaceObjects[position];
if (sObject != null) {
// TODO: get position and move camera to that position
// lookingDirection * radius + sObject.getPosition()
this.camera.setPosition(sObject.getPosition());
}
}
}
public void setKeySphericalSpaceObject(final SphericalSpaceObject object, final int numberKey) {
assert 0 <= numberKey && numberKey <= 9;
spaceObjects[numberKey] = object;
}
@Override