Registrierung Kalender Mitgliederliste Teammitglieder Suche Häufig gestellte Fragen Zur Startseite

Informatiker Board » Themengebiete » Praktische Informatik » ProjectionsMatrix » Antwort erstellen » Hallo Gast [Anmelden|Registrieren]

Antwort erstellen
Benutzername: (du bist nicht eingeloggt!)
Thema:
Nachricht:

HTML ist nicht erlaubt
BBCode ist erlaubt
Smilies sind erlaubt
Bilder sind erlaubt

Smilies: 21 von 33
smileWinkDaumen hoch
verwirrtAugenzwinkerngeschockt
Mit ZungeGottunglücklich
Forum Kloppebösegroßes Grinsen
TanzentraurigProst
TeufelSpamWillkommen
LehrerLOL HammerZunge raus
Hilfe 
aktuellen Tag schließen
alle Tags schließen
fettgedruckter Textkursiver Textunterstrichener Text zentrierter Text Hyperlink einfügenE-Mail-Adresse einfügenBild einfügen Zitat einfügenListe erstellen CODE einfügenPHP CODE farbig hervorheben
Spamschutz:
Text aus Bild eingeben
Spamschutz

Die letzten 2 Beiträge
Tommy1234

Hallo nochmal,

habs mathematisch und vor allem auch grafisch hinbekommen. Also Thema geschlossen das Ding läuft.
Tommy1234 ProjectionsMatrix

Hallo,

ich versuche grade ein Terrain mit Punkten und Linien zu generieren.

Das Problem dabei ist, dass ich zwar richtig rotieren kann, aber die Perspektive stimmt nicht, will heißen beim drehen eines Quadrats ist der vordere Teil ( der Teil der auf mich zukommt) größer als der hintere. Und genau das bekomm ich nicht hin.

Hier der Code:

code:
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:
private int w = 1024;
	private int h = 768;
	private int scl = 50;
	private int cols = w/scl;
	private int rows = h/scl;
	
	private Point3D[] p = new Point3D[5];
	private Point3D[] pnew = new Point3D[5];
	private Point projection = null;
	
	int angle = 0;
	double angle_x = 0.001;
    double angle_y = 0.001;
    double angle_z = 0.001;
    
    double px,py,pz;
    
	public Terrain() {
		addKeyListener(new KeyInput());
		setFocusable(true);
		requestFocus();
		initTerrain();
	}

	private void initTerrain() {
		p[0] = new Point3D(0,0,0);
		p[1] = new Point3D(-scl,-scl,0);
		p[2] = new Point3D(scl,-scl,0);
		p[3] = new Point3D(scl,scl,0);
		p[4] = new Point3D(-scl,scl,0);
		
		for(int k = 1;k<p.length;k++) {
			pnew[k] = new Point3D(p[k].x,p[k].y,p[k].z);
		}
		
		projection = new Point(0,0);
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponents(g);
		
		g.clearRect(0, 0, getWidth(), getHeight());
		
		g.translate(getWidth()/2, getHeight()/2);
		
		if(KeyInput.right) {
			angle_x+=Math.PI/3;
			for(int k = 1;k<p.length;k++) {
				
				px = pnew[k].x;
				py = pnew[k].y;
				pz = pnew[k].z;
				
				pnew[k].y = (py*Math.cos(angle_x)-pz*Math.sin(angle_x));
			    pnew[k].z = (py*Math.sin(angle_x)+pz*Math.cos(angle_x));
			    
			    //Projectionspunkte berechnen
			    projection.x = (int)(((pnew[k].x*getWidth()/2)/pnew[k].z)+getWidth()/2)/1000;
			    projection.y = (int)(((pnew[k].y*getHeight()/2)/pnew[k].z)+getHeight()/2)/1000;
			    
			    System.out.println(projection.x);
			}
		}
		
		if(KeyInput.left) {
			angle_y+=0.001;
			for(int k = 1;k<p.length;k++) {
				
				px = pnew[k].x;
				py = pnew[k].y;
				pz = pnew[k].z;
				
		        pnew[k].x = px*Math.cos(angle_y)+pz*Math.sin(angle_y);
		        pnew[k].z =-px*Math.sin(angle_y)+pz*Math.cos(angle_y);
		    }
		}
		
		if(KeyInput.up) {
			angle_z+=0.001;
			for(int k = 1;k<p.length;k++) {
				
				px = pnew[k].x;
				py = pnew[k].y;
				pz = pnew[k].z;
				
	        	pnew[k].x = px*Math.cos(angle_z)-py*Math.sin(angle_z);
	            pnew[k].y = py*Math.cos(angle_z)+px*Math.sin(angle_z);
	            
	        }
		}
		
		for(int y = 0;y<1;y++) {
			for(int x = 0;x<1;x++) {
				g.drawLine((int)(pnew[1].x+(x*scl))+projection.x,(int)(pnew[1].y+(y*scl))+projection.y,(int)(pnew[2].x+(x*scl))+projection.x,(int)(pnew[2].y+(y*scl))+projection.y);
				g.drawLine((int)(pnew[2].x+(x*scl))+projection.x,(int)(pnew[2].y+(y*scl))+projection.y,(int)(pnew[3].x+(x*scl))+projection.x,(int)(pnew[3].y+(y*scl))+projection.y);
				g.drawLine((int)(pnew[3].x+(x*scl))+projection.x,(int)(pnew[3].y+(y*scl))+projection.y,(int)(pnew[4].x+(x*scl))+projection.x,(int)(pnew[4].y+(y*scl))+projection.y);
				g.drawLine((int)(pnew[4].x+(x*scl))+projection.x,(int)(pnew[4].y+(y*scl))+projection.y,(int)(pnew[1].x+(x*scl))+projection.x,(int)(pnew[1].y+(y*scl))+projection.y);
				
			}
		}
		
//		g.drawLine((int)(pnew[1].x),(int)(pnew[1].y),(int)(pnew[2].x),(int)(pnew[2].y));
//    	g.drawLine((int)(pnew[2].x),(int)(pnew[2].y),(int)(pnew[3].x),(int)(pnew[3].y));
//    	g.drawLine((int)(pnew[3].x),(int)(pnew[3].y),(int)(pnew[4].x),(int)(pnew[4].y));
//    	g.drawLine((int)(pnew[4].x),(int)(pnew[4].y),(int)(pnew[1].x),(int)(pnew[1].y));
//    	
//    	g.drawLine((int)(pnew[5].x),(int)(pnew[5].y),(int)(pnew[6].x),(int)(pnew[6].y));
//    	g.drawLine((int)(pnew[6].x),(int)(pnew[6].y),(int)(pnew[7].x),(int)(pnew[7].y));
//    	g.drawLine((int)(pnew[7].x),(int)(pnew[7].y),(int)(pnew[8].x),(int)(pnew[8].y));
//    	g.drawLine((int)(pnew[8].x),(int)(pnew[8].y),(int)(pnew[5].x),(int)(pnew[5].y));
//    	
//    	g.drawLine((int)(pnew[1].x),(int)(pnew[1].y),(int)(pnew[5].x),(int)(pnew[5].y));
//    	g.drawLine((int)(pnew[2].x),(int)(pnew[2].y),(int)(pnew[6].x),(int)(pnew[6].y));
//    	g.drawLine((int)(pnew[3].x),(int)(pnew[3].y),(int)(pnew[7].x),(int)(pnew[7].y));
//    	g.drawLine((int)(pnew[4].x),(int)(pnew[4].y),(int)(pnew[8].x),(int)(pnew[8].y));
		
    	try {
			Thread.sleep(100);
		} catch (InterruptedException e) {e.printStackTrace();}
    	
		//repaint();
	}

	public static void main(String[] args) {
		JFrame window = new JFrame();
    	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setSize(1024,768);
		window.add(new Terrain());
		window.setLocationRelativeTo(null);
		window.setVisible(true);
	}
h