This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
/
Universo.java
337 lines (248 loc) · 13.9 KB
/
Universo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
Gallardo Morales, Juan Carlos - jcgallardomorales@gmail.com
Izquierdo Vera, Javier - javieriizquierdovera@gmail.com
*/
package sistemaSolar;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.Viewer;
import com.sun.j3d.utils.universe.ViewingPlatform;
import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Material;
import javax.media.j3d.PointLight;
import javax.media.j3d.Texture;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
public class Universo {
private Nave nave;
private HashMap<String,Astro> astros;
private Texture textura;
private Canvas3D aCanvas;
final int TRASLADO_SOL = 200000;
final int TRASLADO_ORB = 200000;
final float ESCALE = 0.3f;
public Universo (String archivo_text,Canvas3D aCanvas){
// this.textura = new TextureLoader(archivo_text, null).getTexture();
this.nave = null;
this.astros = new HashMap<String,Astro>();
this.aCanvas = aCanvas;
}
public int getNumComponentes(){
int total = astros.size();
if (nave != null)
total++;
return total;
}
public Nave getNave() {
return nave;
}
public void setNave(Nave nave) {
this.nave = nave;
}
public HashMap<String,Astro> getAstros() {
return astros;
}
public ArrayList<Astro> getAstrosArray(){
ArrayList<Astro> as = new ArrayList();
for (Astro value : astros.values()) {
as.add(value);
}
return as;
}
public void setAstros(HashMap<String,Astro> astros) {
this.astros = astros;
}
public void addAstro(Astro ast){
astros.put(ast.getNombre(),ast);
}
public int numAstros(){
return astros.size();
}
public void crearSistemaSolar(){
String dir_text_sol = "src/texturas_estrellas/";
String dir_text_sat = "src/texturas_satelites/";
String dir_text_pla = "src/texturas_planetas/";
Material m_planetas = new Material();
m_planetas.setAmbientColor(0.5f, 0.5f, 0.5f);
m_planetas.setDiffuseColor(1.0f, 1.0f, 1.0f);
m_planetas.setSpecularColor(0.3f, 0.3f, 0.3f);
Material m_sol = new Material();
m_sol.setAmbientColor(1.0f,0.5f,0.5f);
m_sol.setDiffuseColor(1.0f, 1.0f, 1.0f);
m_sol.setEmissiveColor(1.0f, 1.0f, 1.0f);
/*
nombre
radio
distancia
archivo_textura
material
color
rotacion
traslacion
radio_false
distancia_false
rotacion_false
traslacion_false
*/
// ESTRELLAS --> SOL
Astro sol = new Estrella("sol", 696342, dir_text_sol + "sol.jpg", m_sol,
26, 10,/*radio_false*/25*ESCALE,/*distancia_false*/5*ESCALE,
/*rotacion_false*/TRASLADO_SOL,/*traslacion_false*/0);
astros.put(sol.getNombre(),sol);
// SATÉLITES
Astro luna = new Satelite("luna", 1737, 370300, dir_text_sat + "luna.jpg", m_planetas,
27.32, 27.32,2*ESCALE/*radio_false*/,/*distancia_false*/4*ESCALE,
/*rotacion_false*/TRASLADO_SOL+TRASLADO_SOL/10000,/*traslacion_false*/TRASLADO_ORB/10);
astros.put(luna.getNombre(), luna);
Astro fobos = new Satelite("fobos", 11, 9380, dir_text_sat + "fobos.jpg", m_planetas,
0.3 , 0.3,2*ESCALE/*radio_false*/,/*distancia_false*/5*ESCALE,
/*rotacion_false*/TRASLADO_SOL/30,/*traslacion_false*/TRASLADO_ORB/17);
astros.put(fobos.getNombre(), fobos);
Astro deimos = new Satelite("deimos",7,23460, dir_text_sat + "deimos.jpg", m_planetas,
1.25,1.25,2*ESCALE/*radio_false*/,/*distancia_false*/5*ESCALE,
/*rotacion_false*/TRASLADO_SOL/18,/*traslacion_false*/TRASLADO_ORB/14);
astros.put(deimos.getNombre(), deimos);
Astro io = new Satelite("io",1820,422000, dir_text_sat + "io.jpg", m_planetas,
1.75,1.75,3*ESCALE/*radio_false*/,/*distancia_false*/8*ESCALE,
/*rotacion_false*/TRASLADO_SOL/17,/*traslacion_false*/TRASLADO_ORB/14);
astros.put(io.getNombre(),io);
Astro europa = new Satelite("europa",1561, 671000, dir_text_sat + "europa.jpg", m_planetas,
3.54,3.54,3*ESCALE/*radio_false*/,/*distancia_false*/7*ESCALE,
/*rotacion_false*/TRASLADO_SOL/16,/*traslacion_false*/TRASLADO_ORB/13);
astros.put(europa.getNombre(), europa);
Astro calisto = new Satelite("calisto",2410, 1880000, dir_text_sat + "calisto.jpg", m_planetas,
16.7, 16.7,4*ESCALE/*radio_false*/,/*distancia_false*/5*ESCALE,
/*rotacion_false*/TRASLADO_SOL/14,/*traslacion_false*/TRASLADO_ORB/11);
astros.put(calisto.getNombre(), calisto);
Astro titania = new Satelite("titania",788, 436000, dir_text_sat + "titania.jpg", m_planetas,
8.7, 8.7,3*ESCALE/*radio_false*/,/*distancia_false*/5*ESCALE,
/*rotacion_false*/TRASLADO_SOL/15,/*traslacion_false*/TRASLADO_ORB/12);
astros.put(titania.getNombre(), titania);
Astro ariel = new Satelite("ariel", (float) 578.5, 190000, dir_text_sat + "ariel.jpg", m_planetas,
2.52,2.52,3*ESCALE/*radio_false*/,/*distancia_false*/5*ESCALE,
/*rotacion_false*/TRASLADO_SOL/18,/*traslacion_false*/TRASLADO_ORB/13);
astros.put(titania.getNombre(), titania);
Astro miranda = new Satelite("miranda",236, 130000 ,dir_text_sat + "miranda.jpg", m_planetas,
1.41,1.41,2*ESCALE/*radio_false*/,/*distancia_false*/5*ESCALE,
/*rotacion_false*/TRASLADO_SOL/19,/*traslacion_false*/TRASLADO_ORB/14);
astros.put(miranda.getNombre(), miranda);
Astro triton = new Satelite("triton", (float) 1353.5, 354760, dir_text_sat + "triton.jpg", m_planetas,
-5.88,-5.88,4*ESCALE/*radio_false*/,/*distancia_false*/5*ESCALE,
/*rotacion_false*/-TRASLADO_SOL/15,/*traslacion_false*/TRASLADO_ORB/-12);
astros.put(triton.getNombre(), triton);
// *******************************************************************/
//Planetas
// distancia respecto al sol expresados en millones de km
// radio expresado en km
// rotación y traslación expresados en días
// *******************************************************************/
Astro mercurio = new Planeta("mercurio", (float) 2439.7, (float) 57.8, dir_text_pla + "mercurio.jpg", m_planetas,
58.6, 87.6, (Estrella) sol, null,6*ESCALE/*radio_false*/,
10*ESCALE/*distancia_false*/,/*rotacion_false*/TRASLADO_SOL/10,/*traslacion_false*/TRASLADO_ORB/14);
Astro venus = new Planeta("venus",6052, (float) 108.2, dir_text_pla + "venus.jpg", m_planetas,
243 , 224.5, (Estrella) sol, null,8*ESCALE/*radio_false*/,
20*ESCALE/*distancia_false*/,/*rotacion_false*/TRASLADO_SOL/5,/*traslacion_false*/TRASLADO_ORB/11);
Astro tierra = new Planeta("tierra", 6378, (float) 149.6, dir_text_pla + "tierra.jpg", m_planetas,
1, 365.25, (Estrella) sol, new HashMap(),8*ESCALE/*radio_false*/,
30*ESCALE/*distancia_false*/,/*rotacion_false*/TRASLADO_SOL/20,/*traslacion_false*/TRASLADO_ORB/8);
((Planeta) tierra).addSatelite((Satelite) luna);
Astro marte = new Planeta("marte", (float) 3393.5, (float) 227.9, dir_text_pla + "marte.jpg", m_planetas,
1.03, 686.6, (Estrella) sol, new HashMap(),6*ESCALE/*radio_false*/,
40*ESCALE/*distancia_false*/,/*rotacion_false*/TRASLADO_SOL/20,/*traslacion_false*/TRASLADO_ORB/7);
((Planeta) marte).addSatelite((Satelite) fobos);
((Planeta) marte).addSatelite((Satelite) deimos);
Astro jupiter = new Planeta("jupiter",71492, (float) 778.5, dir_text_pla + "jupiter.jpg", m_planetas,
0.414, 4331.86, (Estrella) sol, new HashMap(),/*radio_false*/15*ESCALE,
60*ESCALE/*distancia_false*/,/*rotacion_false*/TRASLADO_SOL/25,/*traslacion_false*/TRASLADO_ORB/5);
((Planeta) jupiter).addSatelite((Satelite) io);
((Planeta) jupiter).addSatelite((Satelite) europa);
((Planeta) jupiter).addSatelite((Satelite) calisto);
Astro saturno = new Planeta("saturno",60268, 1433, dir_text_pla + "saturno.jpg", m_planetas,
0.426, 10760.27, (Estrella) sol, null,/*radio_false*/13*ESCALE,
80*ESCALE/*distancia_false*/,/*rotacion_false*/TRASLADO_SOL/25,/*traslacion_false*/TRASLADO_ORB/3);
Anillo anillo_a = new Anillo("anillo a",saturno.getRadioFalse()* 1.7, dir_text_pla + "anillo_saturno.png",m_planetas,Color.white);
//Anillo anillo_b = new Anillo("anillo b",saturno.getRadioFalse()* 1.8, dir_text_pla + "anillo_saturno.png",m_planetas,Color.white);
//Anillo anillo_c = new Anillo("anillo c",saturno.getRadioFalse()* 2.0, dir_text_pla + "anillo_saturno.png",m_planetas,Color.white);
((Planeta)saturno).addAnillo(anillo_a);
//saturno.addAnillo(anillo_b);
//saturno.addAnillo(anillo_c);
/********* 3 anillos representados en una textura ************/
Astro urano = new Planeta("urano", 25554, 2877, dir_text_pla + "urano.jpg", m_planetas,
0.718, 30684, (Estrella) sol, new HashMap(),/*radio_false*/10*ESCALE,
90*ESCALE/*distancia_false*/,/*rotacion_false*/TRASLADO_SOL/23,/*traslacion_false*/TRASLADO_ORB/2);
((Planeta) urano).addSatelite((Satelite) titania);
((Planeta) urano).addSatelite((Satelite) ariel);
((Planeta) urano).addSatelite((Satelite) miranda);
Astro neptuno = new Planeta("neptuno", 24769, 4498, dir_text_pla + "neptuno.jpg", m_planetas,
0.6745, 60189, (Estrella) sol, new HashMap(),/*radio_false*/10*ESCALE,
100*ESCALE/*distancia_false*/,/*rotacion_false*/TRASLADO_SOL/24,/*traslacion_false*/TRASLADO_ORB);
((Planeta) neptuno).addSatelite((Satelite) triton);
astros.put(mercurio.getNombre(), mercurio);
astros.put(venus.getNombre(), venus);
astros.put(tierra.getNombre(), tierra);
astros.put(marte.getNombre(), marte);
astros.put(jupiter.getNombre(), jupiter);
astros.put(saturno.getNombre(), saturno);
astros.put(urano.getNombre(), urano);
astros.put(neptuno.getNombre(), neptuno);
}
public SimpleUniverse createUniverso(){
/*********** VIEWINGPLATFORM **********/
// Creamos lo que hace que se vean las cosas, manualmente, para poder asignarlo
// y personalizarlo
ViewingPlatform viewingPlatform = new ViewingPlatform();
// Radio de activación
viewingPlatform.getViewPlatform().setActivationRadius(100f);
// Transformación de vista -> dónde se está , a dónde se mira , Vup
TransformGroup viewTransformGroup = viewingPlatform.getViewPlatformTransform();
Transform3D viewTransform3D = new Transform3D ( );
viewTransform3D.lookAt (new Point3d (20 ,20 ,20 ),
new Point3d (0, 0, 0), new Vector3d (0 ,1 ,0));
viewTransform3D.invert();
viewTransformGroup.setTransform(viewTransform3D);
// Mover la cámara con el ratón
OrbitBehavior orbit = new OrbitBehavior (aCanvas, OrbitBehavior.REVERSE_ALL);
orbit.setSchedulingBounds(new BoundingSphere (new Point3d (0.0f, 0.0f , 0.0f) , 100.0f));
orbit.setZoomFactor(1.0f);
viewingPlatform.setViewPlatformBehavior(orbit);
/************** VIEWER **************/
Viewer viewer = new Viewer(aCanvas);
View view = viewer.getView();
view.setFieldOfView(Math.toRadians(45));
view.setBackClipDistance(50.0);
return (new SimpleUniverse(viewingPlatform,viewer));
}
public BranchGroup createBackground(){
Background background = new Background();
BoundingSphere bound = new BoundingSphere (new Point3d (0.0, 0.0, 0.0), 100000.0);
background.setApplicationBounds(bound);
Appearance apariencia = new Appearance();
Texture textura = new TextureLoader ("src/background/back.jpg" , null).getTexture();
apariencia.setTexture(textura);
Sphere esfera = new Sphere(1.5f, Primitive.GENERATE_TEXTURE_COORDS |
Primitive.GENERATE_NORMALS_INWARD, 500, apariencia );
BranchGroup geometria = new BranchGroup();
geometria.addChild(esfera);
background.setGeometry(geometria);
BranchGroup backgroundBranch = new BranchGroup ();
backgroundBranch.addChild(background);
// colgamos la luz ambiental al branchgroup
LuzAmbiental la = new LuzAmbiental(new Color3f(0.8f,0.2f,0.2f));
backgroundBranch.addChild(la);
return backgroundBranch;
}
}