A Penny Farthing to the man...
...who can help me solve my problem. I've been trying to program in 3D and so far my results have not been good. I can make my polygon move fine around the z axis, but as soon as i try to rotate around the others and the z has to change, everything gets screwed up.
import javax.swing.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.net.*;
import java.io.*;
import java.awt.geom.*;
import javax.swing.border.*;
import java.applet.*;
import java.net.URL;
import java.awt.image.*;
import java.lang.Math;
class Rotateextends JFrameimplements KeyListener{
Point Triangle[] =new Point[3];
public Rotate(){
Triangle[0] =new Point(-100,100,1);
Triangle[1] =new Point(100,100,1);
Triangle[2] =new Point(100,-100,1);
this.setSize(600,600);
this.setVisible(true);
addKeyListener(this);
}
publicstaticvoid main(String args[]){
Rotate Begin =new Rotate();
}
publicvoid paint(Graphics g){
super.paint(g);
Polygon poly =new Polygon();
poly.addPoint((int)(Triangle[0].x / (Triangle[0].z + 50)) * 100 + 200, (int)(200-(Triangle[0].y / (Triangle[0].z + 50)) * 100));
poly.addPoint((int)(Triangle[1].x / (Triangle[1].z + 50)) * 100 + 200, (int)(200-(Triangle[1].y / (Triangle[1].z + 50)) * 100));
poly.addPoint((int)(Triangle[2].x / (Triangle[2].z + 50)) * 100 + 200, (int)(200-(Triangle[2].y / (Triangle[2].z + 50)) * 100));
g.drawPolygon(poly);
System.out.println(Triangle[0].x +"// " + Triangle[0].y +"// " + Triangle[0].z);
System.out.println(Triangle[1].x +"// " + Triangle[1].y +"// " + Triangle[1].z);
System.out.println(Triangle[2].x +"// " + Triangle[2].y +"// " + Triangle[2].z);
}
publicvoid keyTyped(KeyEvent e){
double rotan = 0.01;
if(e.getKeyChar() =='a'){ZRotation(rotan);}
elseif(e.getKeyChar() =='z'){ZRotation(-rotan);}
if(e.getKeyChar() =='n'){YRotation(rotan);}
elseif(e.getKeyChar() =='m'){YRotation(-rotan);}
if(e.getKeyChar() =='g'){XRotation(rotan);}
elseif(e.getKeyChar() =='h'){XRotation(-rotan);}
if(e.getKeyChar() =='o'){
Triangle[0].z = Triangle[0].z - 1;
Triangle[1].z = Triangle[1].z - 1;
Triangle[2].z = Triangle[2].z - 1;
}
if(e.getKeyChar() =='p'){
Triangle[0].z = Triangle[0].z + 1;
Triangle[1].z = Triangle[1].z + 1;
Triangle[2].z = Triangle[2].z + 1;
}
repaint();
}
publicvoid ZRotation(double radi){
double tempx,tempy,tempz;
for(int ctr = 0; ctr < 3; ++ ctr){
tempx = Triangle[ctr].x * Math.cos(radi) - Triangle[ctr].y * Math.sin(radi);
tempy = Triangle[ctr].x * Math.sin(radi) + Triangle[ctr].y * Math.cos(radi);
Triangle[ctr].x = tempx;
Triangle[ctr].y = tempy;
}
}
publicvoid YRotation(double radi){
double tempx,tempy,tempz;
for(int ctr = 0; ctr < 3; ++ ctr){
tempx = Triangle[ctr].x * Math.cos(radi) - Triangle[ctr].z * Math.sin(radi);
tempz = Triangle[ctr].x * Math.sin(radi) + Triangle[ctr].z * Math.cos(radi);
Triangle[ctr].x = tempx;
Triangle[ctr].z = tempz;
}
}
publicvoid XRotation(double radi){
double tempx,tempy,tempz;
for(int ctr = 0; ctr < 3; ++ ctr){
tempy = (Triangle[ctr].y * Math.cos(radi)) - (Triangle[ctr].z * Math.sin(radi));
tempz = (Triangle[ctr].y * Math.sin(radi)) + (Triangle[ctr].z * Math.cos(radi));
Triangle[ctr].y = tempy;
Triangle[ctr].z = tempz;
}
}
publicvoid keyReleased(KeyEvent e){}
publicvoid keyPressed(KeyEvent e){}
}
class Point{
double x;
double y;
double z;
public Point(double x1,double y1,double z1){
x = x1;
y = y1;
z = z1;
}
}
[8321 byte] By [
T-Leaa] at [2007-11-26 14:42:59]

# 1
I might be able to help you out, but you need to define what you mean by "screwed up", and you'll have to be more specific about the intended behavior (not as seen in the code, but by what you REALLY want it to do).
For example, are you trying to make it always rotate around the x, y, and z axes as YOU perceive them, or do you want it to always rotate around its own, internal x, y, and z axes (which move as it rotates).
Also, maybe list a scenario that cause the problem you are seeing - i.e. you rotate it x1 degrees (or radians) about some axis, and then x2 degrees about another axis, then x3 degrees about the original again, and you expect blank1, but you get instead blank2.
Something like that. That would more easily allow us to help you figure out what's going wrong.
On the surface, it appears that you might have run into one of two problems:
1. You are making the rotations with respect to the wrong reference location and orientation; or
2. The mathematics you are using is too elementary to obtain the behavior you REALLY want, and you might be well served looking into the algebra of rotation matrices, and possibly learning about quaternions.
Hope some of this helped!
- Adam
# 3
Here again is a sampling of the new code, thanks a lot for giving it a look.
import javax.swing.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import java.awt.geom.*;
import javax.swing.border.*;
import java.awt.image.*;
import java.lang.Math;
class TRotate extends JFrame implements KeyListener{
Point Triangle[][];
FileDecoder fd;
double polyarray[][] = new double[36][3];
int mag = 20;
Polygon poly;
public TRotate(){
int ctr3 = 0;
fd = new FileDecoder("TestCube.tri");
polyarray = fd.GetMatrix();
Triangle = new Point[12][9];
for(int ctr = 0; ctr < 12; ++ ctr){
for(int ctr2 = 0; ctr2 < 3; ++ ctr2){
Triangle[ctr][ctr2] = new Point(polyarray[ctr3][0],polyarray[ctr3][1],polyarray[ctr3][2]);
ctr3++;
}
}
getContentPane().setBackground(Color.BLACK);
this.setSize(600,600);
this.setVisible(true);
addKeyListener(this);
}
public static void main(String args[]){
TRotate Begin = new TRotate();
}
public void paint(Graphics g){
super.paint(g);
poly = new Polygon();
for(int ctr1 = 0; ctr1 < 12; ++ ctr1){
for(int ctr2 = 0; ctr2 < 3; ++ ctr2){
poly.addPoint((int)(Triangle[ctr1][ctr2].x / (Triangle[ctr1][ctr2].z + 50) * mag) + 200, (int)(Triangle[ctr1][ctr2].y / (Triangle[ctr1][ctr2].z + 30) * mag) + 200);
}
}
System.out.println("");
g.setColor(Color.GREEN);
g.drawPolygon(poly);
}
public void keyTyped(KeyEvent e){
double rotan = 0.02;
if(e.getKeyChar() == 'a'){ZRotation(rotan);}
else if(e.getKeyChar() == 'z'){ZRotation(-rotan);}
if(e.getKeyChar() == 'n'){YRotation(rotan);}
else if(e.getKeyChar() == 'm'){YRotation(-rotan);}
if(e.getKeyChar() == 'g'){XRotation(rotan);}
else if(e.getKeyChar() == 'h'){XRotation(-rotan);}
repaint();
}
public void ZRotation(double radi){
double tempx,tempy,tempz;
for(int ctr1 = 0; ctr1 < 12; ++ ctr1){
for(int ctr2 = 0; ctr2 < 3; ++ ctr2){
tempx = Triangle[ctr1][ctr2].x * Math.cos(radi) - Triangle[ctr1][ctr2].y * Math.sin(radi);
tempy = Triangle[ctr1][ctr2].x * Math.sin(radi) + Triangle[ctr1][ctr2].y * Math.cos(radi);
Triangle[ctr1][ctr2].x = tempx;
Triangle[ctr1][ctr2].y = tempy;
}
}
}
public void YRotation(double radi){
double tempx,tempy,tempz;
for(int ctr1 = 0; ctr1 < 12; ++ ctr1){
for(int ctr2 = 0; ctr2 < 3; ++ ctr2){
tempx = Triangle[ctr1][ctr2].x * Math.cos(radi) - Triangle[ctr1][ctr2].z * Math.sin(radi);
tempz = Triangle[ctr1][ctr2].x * Math.sin(radi) + Triangle[ctr1][ctr2].z * Math.cos(radi);
Triangle[ctr1][ctr2].x = tempx;
Triangle[ctr1][ctr2].z = tempz;
}
}
}
public void XRotation(double radi){
double tempx,tempy,tempz;
for(int ctr1 = 0; ctr1 < 12; ++ ctr1){
for(int ctr2 = 0; ctr2 < 3; ++ ctr2){
tempy = (Triangle[ctr1][ctr2].y * Math.cos(radi)) - (Triangle[ctr1][ctr2].z * Math.sin(radi));
tempz = (Triangle[ctr1][ctr2].y * Math.sin(radi)) + (Triangle[ctr1][ctr2].z * Math.cos(radi));
Triangle[ctr1][ctr2].y = tempy;
Triangle[ctr1][ctr2].z = tempz;
}
}
}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e){}
}
class Point{
double x;
double y;
double z;
public Point(double x1,double y1,double z1){
x = x1;
y = y1;
z = z1;
}
}
and the class that handles the file reading is
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import java.awt.geom.*;
import javax.swing.border.*;
import java.awt.image.*;
import java.lang.Math;
class FileDecoder{
FileReader fr;
BufferedReader br;
String line;
StringTokenizer st;
int lim = 9;
int ctr2 = 0;
double[][] tarray = new double[12][9];
double[][] polarray = new double[36][3];
public FileDecoder(String path){
try{fr = new FileReader(path);}
catch(Exception e){}
br = new BufferedReader(fr);
for(int ctr1 = 0; ctr1 < 12; ++ ctr1){
try{line = br.readLine();}catch(Exception e){}
st = new StringTokenizer(line.trim(), " ");
ctr2 = 0;
while(st.hasMoreTokens()){
tarray[ctr1][ctr2] = Double.valueOf((st.nextToken())).doubleValue();
if(ctr2 < 9){ctr2++;}
}
}
MatrixConverter(tarray);
}
public static void main(String args[]){
FileDecoder fd = new FileDecoder("TestCube.tri");
}
public double[][] GetMatrix(){
return polarray;
}
public void MatrixConverter(double[][] tmparray){
int ctr7 = 0, ctr8 = 0;
for(int ctr5 = 0; ctr5 < 36; ++ ctr5){
for(int ctr6 = 0; ctr6 < 3; ++ ctr6){
polarray[ctr5][ctr6] = tmparray[ctr7][ctr8];
if(ctr8 < 8 && ctr7 != 12){++ctr8;}
else{ctr8=0;++ctr7;}
}
}
}
}
and finally if anyone wants the points that i used,
49.65 -49.6 56.725 49.65 50.4 -43.275 49.65 50.4 56.725
49.65 50.4 -43.275 49.65 -49.6 56.725 49.65 -49.6 -43.275
-50.35 -49.6 -43.275 -50.35 50.4 56.725 -50.35 50.4 -43.275
-50.35 50.4 56.725 -50.35 -49.6 -43.275 -50.35 -49.6 56.725
-50.35 -49.6 -43.275 49.65 50.4 -43.275 49.65 -49.6 -43.275
49.65 50.4 -43.275 -50.35 -49.6 -43.275 -50.35 50.4 -43.275
-50.35 50.4 56.725 49.65 -49.6 56.725 49.65 50.4 56.725
49.65 -49.6 56.725 -50.35 50.4 56.725 -50.35 -49.6 56.725
49.65 50.4 -43.275 -50.35 50.4 56.725 49.65 50.4 56.725
-50.35 50.4 56.725 49.65 50.4 -43.275 -50.35 50.4 -43.275
-50.35 -49.6 -43.275 49.65 -49.6 56.725 -50.35 -49.6 56.725
49.65 -49.6 56.725 -50.35 -49.6 -43.275 49.65 -49.6 -43.275
end