Bonjour,

Je tente de faire un programme pour me déplacer dans un univers 3D ou se trouve normalement un cube blanc.

Mais je n'arrive pas a centrer ce cube dans le champs de la caméra.

Voici le code:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
package jeu;
 
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
 
public class Tron3D extends MIDlet implements CommandListener
{
	private Display monDisplay = null;
	private CallbackCanvas monCanvas = null;
	private Command commandeSortie = new Command("Sortir", Command.ITEM, 1);
	private World monMonde = null;
	private Timer monRafraichissement = new Timer();
	private TimerTask monRafraichissementProcessus = null;
 
	private Camera camera;
	private Graphics3D monGraphique3D;
	private Mesh scene;
 
	private float posX = 0.0f;
	private float posZ = 0.0f;
	private float rotY = 0.0f;
	private final float PI_OVER_180 = 0.0174532925f;
	private final float pas = 1.00f; 
 
	public Tron3D()
	{
		monDisplay = Display.getDisplay(this);
		monCanvas = new CallbackCanvas(this);
		monCanvas.setCommandListener(this);
		monCanvas.addCommand(commandeSortie);
	}
 
	public void startApp() throws MIDletStateChangeException
	{
		monMonde = new World();
		monDisplay.setCurrent(monCanvas);
		try 
		{
			//Object3D[] roots = Loader.load("/tron3D.m3g");
			//monMonde = (World)roots[0];
 
			monGraphique3D = Graphics3D.getInstance();
 
			camera = new Camera();
			float aspect = 320.0f/240.0f;
			camera.setPerspective(30.0f, aspect, 1.0f, 1000.0f);
			monMonde.addChild(camera);
 
			Background background = new Background();
			Image2D img = (Image2D)Loader.load("/sky.png")[0];
			background.setImage(img);
			monMonde.setBackground(background);
 
			scene = getScene();
			scene.setTranslation(0.0f, 0.0f, 0.0f);
			monMonde.addChild(scene);
			monMonde.setActiveCamera(camera);
 
			monCanvas.repaint();
 
			System.out.println ( "startApp" );
		}
		catch(Exception erreur)
		{
			erreur.printStackTrace();
		}
	}
 
	public void pauseApp()
	{
		System.out.println ( "pauseApp" );
 
		monRafraichissementProcessus.cancel();
		monRafraichissementProcessus = null;
		monMonde = null;
	}
 
	public void destroyApp(boolean unconditional) throws MIDletStateChangeException
	{
		System.out.println ( "destroyApp" );
 
		monRafraichissement.cancel();
		monRafraichissement = null;
		monRafraichissementProcessus = null;
		monMonde = null;
		monCanvas = null;
	}
 
	private void dessine( Graphics g )
	{
		try
		{
			monGraphique3D.bindTarget(g); // Binds the given Graphics or mutable Image2D as the rendering target of this Graphics3D
			monGraphique3D.render(monMonde); // Render the world
		}
		finally
		{
			monGraphique3D.releaseTarget();
		}
 
		g.setColor(0x00FFFFFF);
		g.drawString( "X="+posX+",Z="+posZ+",Y="+rotY, 0, Font.SIZE_SMALL, Graphics.LEFT|Graphics.TOP );
	}
 
	public void paint(Graphics g)
	{
		if ((monCanvas == null) || (monCanvas == null))
			return;
 
		if (monRafraichissementProcessus != null)
		{
			monRafraichissementProcessus.cancel();
			monRafraichissementProcessus = null;
		}
 
		dessine(g);
 
		monRafraichissementProcessus = new monRafraichissementProcessus();
		monRafraichissement.schedule(monRafraichissementProcessus, 500);	// 40 = 25 images / seconde
		System.out.println ( "paint" );
	}
 
	private class monRafraichissementProcessus extends TimerTask
	{
		public void run()
		{
			monMonde.getActiveCamera().setOrientation(rotY, 0, 0, 1);
			monMonde.getActiveCamera().setTranslation(posX, posZ, 0);
			System.out.println ( "monRafraichissementProcessus" );
			monCanvas.repaint();
		}
	}
 
	public void commandAction(Command cmd, Displayable disp)
	{
		if (cmd == commandeSortie)
		{
			try
			{
				destroyApp(false);
				notifyDestroyed();
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
 
	public void keyPressed(int keyCode)
	{
		switch (keyCode)
		{
			case Canvas.KEY_NUM2:	// Haut
				posX -= (float)Math.sin(rotY*PI_OVER_180) * pas;
				posZ -= (float)Math.cos(rotY*PI_OVER_180) * pas;
				break;
			case Canvas.KEY_NUM8:	// Bas
				posX += (float)Math.sin(rotY*PI_OVER_180) * pas;
				posZ += (float)Math.cos(rotY*PI_OVER_180) * pas;
				break;
			case Canvas.KEY_NUM4:	// Gauche
				rotY += 1.0f;
				break;
			case Canvas.KEY_NUM6:	// Droite
				rotY -= 1.0f;
				break;
			default:
				break;
		}
	}
 
	private class CallbackCanvas extends Canvas
	{
		Tron3D monMidlet;
		CallbackCanvas(Tron3D midlet)
		{
			monMidlet = midlet;
		}
		void init() {}
		void destroy() {}
		protected void paint(Graphics g)
		{
			monMidlet.paint(g);
		}
		protected void keyPressed(int keyCode)
		{
			monMidlet.keyPressed(keyCode);
		}
	}
 
	private Mesh getScene()
	{
		VertexBuffer cubeVertexData;
		TriangleStripArray cubeTriangles;
		/** The cube's vertex positions (x, y, z). */
		final byte[] VERTEX_POSITIONS = {
		-1, -1,  1,    1, -1,  1,   -1,  1,  1,    1,  1,  1,
		-1, -1, -1,    1, -1, -1,   -1,  1, -1,    1,  1, -1
		};
		/** Indices that define how to connect the vertices to build triangles. */
		int[] TRIANGLE_INDICES = { 0, 1, 2, 3, 7, 1, 5, 4, 7, 6, 2, 4, 0, 1 };
		cubeVertexData = new VertexBuffer();
		VertexArray vertexPositions = new VertexArray(VERTEX_POSITIONS.length/3, 3, 1);
		vertexPositions.set(0, VERTEX_POSITIONS.length/3, VERTEX_POSITIONS);
		cubeVertexData.setPositions(vertexPositions, 1.0f, null);
		cubeTriangles = new TriangleStripArray( TRIANGLE_INDICES, new int[] {TRIANGLE_INDICES.length} );
		cubeVertexData.setDefaultColor(0xFFFFFFFF); 
		Mesh mesh = new Mesh(cubeVertexData, cubeTriangles, null);
		return mesh;
	}
}
Je ne comprend pas comment sont orienté les objets 3D et la caméra.
Quelqu'un peut-il m'expliquer ?

Cordialement.