Anyone good with 3D?
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;
}
}
[8277 byte] By [
T-Leaa] at [2007-11-26 14:50:55]

Probably here: http://forum.java.sun.com/forum.jspa?forumID=21
I posted the same topic there and no one replied, so i thought i would have better luck here. this is becoming really frustrating and i dont know what to do.
I thought it looked pretty bad around all three axes. The problem is that you've put the cast to int in the wrong place. Change (int)(Triangle[0].x / (Triangle[0].z + 50)) * 100 + 200
to (int)(Triangle[0].x / (Triangle[0].z + 50) * 100) + 200
and do similarly in the 5 other transforms.
You should also read up on the Swing rendering model, because overriding paint in a JFrame is an awful way of trying to render anything.
> I posted the same topic there and no one replied, so
> i thought i would have better luck here. this is
> becoming really frustrating and i dont know what to
> do.
Actually, I did make a reply to the thread in the 3D forum about a half hour ago, if you want to go take another look.
I might be able to help you out, but I need more info, as I mentioned in the other thread.
I will continue to watch both threads.
Cheers,
- Adam
Thank you both for your replies. Unfortunately i was unable to check the forum in the past few days because exams are coming up and life is getting busy.
Thanks to you, YAT_Archivist, i managed to smooth out the choppiness i was getting before. thanks a lot for that, who knew that such a simple problem could cause such endless troubles.
I changed the code so it can no accept multiple polygons that it reads from a file. The problem i still have is the rotations on the x and y axis.
I'll post the code here and also in my thread on the other forum, because i dont know which one would be looked at more.
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
thank you both for all the help.
Could you explain what the problem you're seeing is? i.e. what do you expect to see, and what are you seeing?
I don't have Java3D on my machine right now, so I can't run the code you provided, but if you describe the problem a little better, I might not have to run it to figure it out.
- Adam
> I don't have Java3D on my machine right now, so I can't run the code you provided
You can, because it doesn't use the Java3D libraries.
There are a couple of problems I've noticed: firstly, that you transform to (x/(z+50), y/(z+30)) which will do strange things; secondly that you don't have any near-face clipping. I think the latter is responsible for a lot of the odd things which happen. Finally, I wonder whether it would help to add support for quadrilaterals (or back-face culling) to your library so that you don't have the diagonals confusing things.
> > I don't have Java3D on my machine right now, so I
> can't run the code you provided
>
> You can, because it doesn't use the Java3D
> libraries.
>
> There are a couple of problems I've noticed: firstly,
> that you transform to (x/(z+50), y/(z+30)) which will
> do strange things; secondly that you don't have any
> near-face clipping. I think the latter is responsible
> for a lot of the odd things which happen. Finally, I
> wonder whether it would help to add support for
> quadrilaterals (or back-face culling) to your library
> so that you don't have the diagonals confusing things.
Ok...i fixed the problem with the +50 and +30.....i dont know why it was like that, i guess i just bungled up somewhere.
I also dont understand the concept of near face clipping or back face culling, but i will look that up on the internet right now.
I finally got it to rotate properly though, all i did was divide the turning angle by 30 on the z points in both x and y axis rotations. It rotates fine but it kind of goes off into the distance... i dont know it sounds like too easy a fix if you ask me.
heres exactly what i did to get it to rotate "properly" in all axis:
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 / 30)) + (Triangle[ctr1][ctr2].z * Math.cos(radi / 30));
Triangle[ctr1][ctr2].y = tempy;
Triangle[ctr1][ctr2].z = tempz;
}
}
}
I guess what i still need help with is any general advice anyone can give to help me along...as well as how to make the rotations ACTUALLY work, instead of the sloppy guesswork i did. Thanks a lot.
> > I don't have Java3D on my machine right now, so I
> can't run the code you provided
>
> You can, because it doesn't use the Java3D
> libraries.
>
... oh, wow. now I feel kinda dumb. I didn't even look at the code yet. I just assumed that since I first saw this post on the Java 3D forum that it was using Java 3D.
Still, I think any thread like this warrants a description of
a) What the OP expected to see; and
b) What the program actually does.
Trying to find the cause of a bug when you don't know what the bug is, is a rather fruitless undertaking.