|
| 1 | +package com.interplanetaryorbitcalculator.rollercoaster; |
| 2 | + |
| 3 | +import com.badlogic.gdx.ApplicationAdapter; |
| 4 | +import com.badlogic.gdx.ApplicationListener; |
| 5 | +import com.badlogic.gdx.Gdx; |
| 6 | +import com.badlogic.gdx.graphics.*; |
| 7 | +import com.badlogic.gdx.graphics.g2d.SpriteBatch; |
| 8 | +import com.badlogic.gdx.graphics.g3d.*; |
| 9 | +import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; |
| 10 | +import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight; |
| 11 | +import com.badlogic.gdx.graphics.g3d.utils.CameraInputController; |
| 12 | +import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder; |
| 13 | +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; |
| 14 | +import com.badlogic.gdx.math.Matrix4; |
| 15 | +import com.badlogic.gdx.math.Vector3; |
| 16 | + |
| 17 | +public class MapView implements ApplicationListener |
| 18 | +{ |
| 19 | + public ShapeRenderer renderer; |
| 20 | + public PerspectiveCamera camera; |
| 21 | + public Model earthModel; |
| 22 | + public ModelInstance earthInstance; |
| 23 | + public Model rocketModel; |
| 24 | + public ModelInstance rocketInstance; |
| 25 | + public Matrix4 rocketInitTransform; |
| 26 | + public ModelBatch modelBatch; |
| 27 | + public Environment environment; |
| 28 | + public CameraInputController cameraInputController; |
| 29 | + public double ellX; |
| 30 | + public double ellY; |
| 31 | + public double ellZ; |
| 32 | + public Body rocket; |
| 33 | + public float sF = 1000000; |
| 34 | + public double time=-600; |
| 35 | + |
| 36 | + @Override |
| 37 | + public void create () { |
| 38 | + renderer = new ShapeRenderer(); |
| 39 | + modelBatch = new ModelBatch(); |
| 40 | + |
| 41 | + environment = new Environment(); |
| 42 | + environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1)); |
| 43 | + environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f)); |
| 44 | + |
| 45 | + camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); |
| 46 | + camera.position.set(0*6371000f*2/sF,2*6371000f*2/sF,0*6371000f*2/sF); |
| 47 | + camera.lookAt(0,0,0); |
| 48 | + camera.near = 1; |
| 49 | + camera.far = 6371000f*4/sF*100; |
| 50 | + camera.update(); |
| 51 | + |
| 52 | + cameraInputController = new CameraInputController(camera); |
| 53 | + Gdx.input.setInputProcessor(cameraInputController); |
| 54 | + |
| 55 | + ModelBuilder modelBuilder = new ModelBuilder(); |
| 56 | + //model = modelBuilder.createBox(6.371f,6.371f,6.371f, new Material(ColorAttribute.createDiffuse(Color.GREEN)), VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal); |
| 57 | + earthModel = modelBuilder.createSphere(6371000f/sF,6371000f/sF,6371000f/sF,100,100, new Material(ColorAttribute.createDiffuse(Color.GREEN)), VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal); |
| 58 | + rocketModel = modelBuilder.createSphere(1,1,1,50,50, new Material(ColorAttribute.createDiffuse(Color.BLUE)), VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal); |
| 59 | + earthInstance = new ModelInstance(earthModel); |
| 60 | + //earthInstance.transform.translate((float)2.5,(float)2.5,(float)2.5); |
| 61 | + System.out.println(earthInstance.transform); |
| 62 | + rocketInstance = new ModelInstance(rocketModel); |
| 63 | + rocketInitTransform = rocketInstance.transform; |
| 64 | + |
| 65 | + rocket = new Body("rocket", 1000, new Orbit(7371000,1000000+6371000, Math.toRadians(0), 0, 0, 0, Body.G* Body.earthMass)); |
| 66 | + //t=0 defined to be 01/15/2007, 12:30AM |
| 67 | + rocket.parentBody = new Body("earth", Body.earthMass, new Orbit(0.98329* Body.AU, 1.0167* Body.AU, Math.toRadians(0.00005), Math.toRadians(-11.26061), Math.toRadians(102.94717), 0, Body.G* Body.sunMass)); |
| 68 | + rocket.parentBody.addSatellite(rocket); |
| 69 | + rocket.parentBody.parentBody = new Body("sun", Body.sunMass); |
| 70 | + rocket.parentBody.parentBody.addSatellite(rocket.parentBody); |
| 71 | + //Mars reached periapsis at 06/01/2007, 7:20AM |
| 72 | + rocket.parentBody.parentBody.addSatellite(new Body("mars", 6.39*Math.pow(10, 23), new Orbit(1.3814* Body.AU, 1.666* Body.AU, Math.toRadians(1.85061), Math.toRadians(49.57854), Math.toRadians(336.04084), 11861400, Body.G* Body.sunMass))); |
| 73 | + rocket.parentBody.parentBody.getSatelliteByName("mars").parentBody=rocket.parentBody.parentBody; |
| 74 | + System.out.println(rocket.orbit); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void render () { |
| 79 | + Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); |
| 80 | + Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); |
| 81 | + |
| 82 | + Gdx.graphics.setVSync(true); |
| 83 | + |
| 84 | + //Vector3 argAxis = new Vector3(0,0,1).rotateRad((float)rocket.orbit.inclination, 0,1,0).rotateRad((float)rocket.orbit.longitude, 0,0,1); |
| 85 | + Vector3 position = rocket.orbit.getPosition(time);//.rotate(argAxis, 180); |
| 86 | + rocketInstance.transform = rocketInstance.transform.setTranslation(position.scl(1f/sF).rotate(90, 1,0,0)); |
| 87 | + |
| 88 | + if (time==0) |
| 89 | + { |
| 90 | + System.out.println(rocket.orbit.getVelocity(time)); |
| 91 | + System.out.println(rocket.orbit.getPosition(time)); |
| 92 | + rocket.orbit = rocket.orbit.perturbOrbit(time, new Vector3(0,100,0)); |
| 93 | + System.out.println(rocket.orbit.getVelocity(time)); |
| 94 | + System.out.println(rocket.orbit.getPosition(time)); |
| 95 | + System.out.println(rocket.orbit); |
| 96 | + } |
| 97 | + |
| 98 | + if (time%600==0) |
| 99 | + { |
| 100 | + System.out.println(rocket.orbit.getPosition(time)); |
| 101 | + } |
| 102 | + |
| 103 | + time+=10; |
| 104 | + |
| 105 | + cameraInputController.update(); |
| 106 | + renderer.setProjectionMatrix(camera.combined); |
| 107 | + |
| 108 | + //System.out.println(rocketInstance.transform); |
| 109 | + |
| 110 | + renderer.begin(ShapeRenderer.ShapeType.Line); |
| 111 | + renderer.line(-1,0,0,10,0,0); |
| 112 | + renderer.line(0,-1,0,0,10,0); |
| 113 | + renderer.line(0,0,-1,0,0,10); |
| 114 | + float[] par = rocket.orbit.parameterize(); |
| 115 | + Vector3 center = new Vector3(par[2]/sF,par[3]/sF,par[4]/sF).rotate(90, 1,0,0); |
| 116 | + Vector3 semiMajorAxis = new Vector3(par[5],par[6],par[7]).rotate(90, 1,0,0); |
| 117 | + Vector3 semiMinorAxis = new Vector3(par[8],par[9],par[10]).rotate(90, 1,0,0); |
| 118 | + float sMaA = par[0]/sF; |
| 119 | + float sMiA = par[1]/sF; |
| 120 | + for (int t = 0; t <= 100; t++) |
| 121 | + { |
| 122 | + double i = ((double)t/100)*(Math.PI*2); |
| 123 | + double x = center.x + sMaA * Math.cos(i) * semiMajorAxis.x + sMiA * Math.sin(i) * semiMinorAxis.x; |
| 124 | + double y = center.y + sMaA * Math.cos(i) * semiMajorAxis.y + sMiA * Math.sin(i) * semiMinorAxis.y; |
| 125 | + double z = center.z + sMaA * Math.cos(i) * semiMajorAxis.z + sMiA * Math.sin(i) * semiMinorAxis.z; |
| 126 | + if (i==0) |
| 127 | + { |
| 128 | + renderer.line((float)x,(float)y,(float)z,(float)x,(float)y,(float)z); |
| 129 | + } |
| 130 | + else |
| 131 | + { |
| 132 | + renderer.line((float)ellX,(float)ellY,(float)ellZ,(float)x,(float)y,(float)z); |
| 133 | + } |
| 134 | + ellX=x; |
| 135 | + ellY=y; |
| 136 | + ellZ=z; |
| 137 | + |
| 138 | + } |
| 139 | + renderer.end(); |
| 140 | + |
| 141 | + modelBatch.begin(camera); |
| 142 | + modelBatch.render(earthInstance, environment); |
| 143 | + modelBatch.render(rocketInstance, environment); |
| 144 | + modelBatch.end(); |
| 145 | + } |
| 146 | + |
| 147 | + public void dispose() |
| 148 | + { |
| 149 | + earthModel.dispose(); |
| 150 | + rocketModel.dispose(); |
| 151 | + modelBatch.dispose(); |
| 152 | + } |
| 153 | + |
| 154 | + public void resume() |
| 155 | + { |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + public void pause() |
| 160 | + { |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + public void resize(int x, int y) |
| 165 | + { |
| 166 | + |
| 167 | + } |
| 168 | +} |
0 commit comments