Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: 3D Rotations

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 3D Rotations

    import java.applet.Applet;
    import java.awt.*;
    import java.util.Timer;
    import java.util.TimerTask;
     
    public class ThreeD extends Applet {
     
    	float[] x = new float[4];
    	float[] y = new float[4];
    	float[] z = new float[4];
     
    	int angle = 10;
     
    	int[] sx = new int[3];
    	int[] sy = new int[3];
     
            public void init() {
            setSize(854,480);
            Timer t = new Timer();
            t.schedule(new TimerTask(){
     
               public void run(){
     
            	float angle2 = (float) Math.toRadians(angle++);
                x[0] = -50;
                y[0] = 10;
                z[0] = 1;
                x[1] = 50;
                y[1] = 10;
                z[1] = 1;
                x[2] = 10;
                y[2] = -150;
                z[2] = 1;
     
                float  newx0 = (float) (y[0] * Math.cos(angle2) - z[0] * Math.sin(angle2));
                float newy0 = (float) (y[0] * Math.sin(angle2) + z[0] * Math.cos(angle2));
                float newx1 = (float) (y[1] * Math.cos(angle2) - z[1] * Math.sin(angle2));
                float newy1 = (float) (y[1] * Math.sin(angle2) + z[1] * Math.cos(angle2));
                float newx2 = (float) (y[2] * Math.cos(angle2) - z[2] *Math.sin(angle2));
                float newy2 = (float) (y[2] * Math.sin(angle2) + z[2] *Math.cos(angle2));
     
                /*
                 * X-ROT
                 * float newY = y*cosAngle - z*sinAngle;
                 * float newZ = y*sinAngle + z*cosAngle;
                 * 
                 * Y-ROT
                 * float newX = z*sinAngle + x*cosAngle;
        		 * float newZ = z*cosAngle - x*sinAngle;
                 * 
                 * Z-ROT
                 * 
                 * float newX = x*cosAngle - y*sinAngle;
        		 * float newY = x*sinAngle + y*cosAngle;
                 * 
                */
                y[0] = newx0;
                z[0] = newy0;
                y[1] = newx1;
                z[1] = newy1;
                y[2] = newx2;
                z[2] = newy2;
     
                sx[0] = (int) (((x[0])/z[0]) + (854/2));
        		sy[0] = (int) (((y[0])/z[0]) + (480/2));
        		sx[1] = (int) (((x[1])/z[1]) + (854/2));
        		sy[1] = (int) (((y[1])/z[1]) + (480/2));
        		sx[2] = (int) (((x[2])/z[2]) + (854/2));
        		sy[2] = (int) (((y[2])/z[2]) + (480/2));
     
                repaint();
            }
    }, 20,20);
        }
     
        public void paint(Graphics g) {
        	g.drawLine(sx[0], sy[0], sx[1], sy[1]);
        	g.drawLine(sx[1], sy[1], sx[2], sy[2]);
        	g.drawLine(sx[2], sy[2], sx[0], sy[0]);
        }
     
    }


    Hello All,


    My name is Donny and I'm very proficient at a few programming languages, my problem is this:

    I'm new to 3D Math, so forgive me if it is a simple fix, but any rotation other than Z will not produce a desired result!.

    The above code is an example of what I'm trying to do, and produces the same result. When I try to do a rotation on the X or Y axis they do not work like they should.

    I'm trying to make a basic 3D game (software rendered, without any libraries needed by the user) that is multi-platform and multi-player. I have the server and basic client(a 2d variation) completed, I'm just trying to employ a nicer client.

    Any help is greatly appreciated, sorry for my ignorance with the concept, I'm sure I'll get it down someday.

    Thanks,

    Donny.


    EDIT: Sorry about the formatting, it didn't carry over well.
    Last edited by xdonny; October 15th, 2012 at 08:47 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: 3D Rotations

    I see the computation to determine the s[xy] values in the run method of the TimerTask divide by the z values...any reason? For example

    sx[0] = (int) (((x[0])/z[0]) + (854/2));//division by z[0]

    The view is looking at a cross section (z=0), so I'm not sure of the purpose for the division. You might consider formatting the math into matrix math (rotation/scale/translation/sheer in a single matrix)

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 3D Rotations

    On a slightly unrelated note, you could try adopting the same method used by OpenGL/DirectX for performing 3D transformations by using a 4x4 matrix. This way all transformations (scaling, rotation, translation, even projection) can be described and modified using basic matrix multiplication.

    Matrices can be your Friends

    The old OpenGL documentation describes what's being performed by the old matrix transformation functions such as glTranslate, glRotate, etc.

    edit: Should have read the rest of copeg's post suggesting the same thing
    Last edited by helloworld922; October 15th, 2012 at 10:44 PM.

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 3D Rotations

    Thank you both for your speedy answers,

    copeg: That division is to plot the X,Y,Z coordinates onto the screen, I think my scaling might be off by quite a bit because it appears to be moving back and forth quite severly.

    EDIT for copeg: Sorry I guess I should actually acknowledge your advice, a Matrix would be a good idea, I've tried that method before but my mind doesn't work in that way, I suppose it might be the easiest way though and I'll just have to write the wrapper around it like Java3D does. (For things such as add, subtract, multiply, divide, rotate)

    helloworld922: I have experience with OpenGL with libraries such as LWJGL and JOGL, I'm not looking to make a commercial game, just something as more of a proof of concept, I just want to make a game from the ground up. I've also used DirectX with Java3D which also requires the user to load libraries onto their computer.

    I'm following along with the book called: "Devloping Games in Java". I'm using the formula's extracted from the text, I'm not so sure it's the formula's that's the problem but probably my syntax, I'll look at it again when I get home from school today and try to figure it out.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: 3D Rotations

    Quote Originally Posted by xdonny View Post
    copeg: That division is to plot the X,Y,Z coordinates onto the screen, I think my scaling might be off by quite a bit because it appears to be moving back and forth quite severly.
    Mathematically, I still don't understand why. Try removing the division by z values and see if this produces the result you expect.

  6. #6
    Member
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: 3D Rotations

    The division for perspective projection. I think he got confused. Here's what I do for perspective projections. A perspective projection needs a field of view, a constant of projection. This is usually 60 degrees, but can be changed depending on the situation and special effects. A perspective projection also needs a viewing distance. To find the viewing distance, we use some trig. (Don't worry, if you don't remember your trig [or just don't know it], the below came right out of a book

    int fov = 60;
    double d = DISPLAY_WIDTH / 2 / Math.tan(Math.toRadians(fov));

    And now we can do our projection!

    int sx[0] = x[0] * d / z[0];
    int sy[0] = y[0] * d / z[0];
    //etc...

    Simple, no? Admittedly it's not the most intuitive thing, but it works, unless I made a total derp on my part XD. The only thing you have to be careful with is z equals 0. But if you think about it, if the z (relative to the camera) is 0, that means it's right out of our vision, so...we just skip the calculations (or else we crash!) and handle it as necessary, which really depends on what your render engine currently does with the points after it gets them from your projection. It is unclear how you should handle z equals 0 until you have frustum culling, and other such things.

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 3D Rotations

    Quote Originally Posted by xdonny View Post
    helloworld922: I have experience with OpenGL with libraries such as LWJGL and JOGL, I'm not looking to make a commercial game, just something as more of a proof of concept, I just want to make a game from the ground up. I've also used DirectX with Java3D which also requires the user to load libraries onto their computer.
    I wasn't suggesting you had to use those libraries, but rather use the same approach they take to performing transformations.

    From what I see all you need to do is this:

    1. Create a 4x4 matrix class with the ability to access individual elements and the ability to multiple matrices.
    2. Create "helper functions" such as translate, rotate, scale, etc. which simply set and/or multiple the matrix with the appropriate transformation matrix.

    This makes it extremely easy to perform perspective or orthogonal transformations to 2D screen space, and even perform viewport/frustrum culling.

    If you're really intent on pursuing this route, you can back-calculate the proper equations from the 4x4 matrix transformations. The difference is that these transformations don't keep track of 'angles' per-say, rather they keep track of the orientation vectors (tangential, normal, binormal). This gets around the Gimbal Lock problem which is intrinsic with keeping track of Euler angles.

  8. #8
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 3D Rotations

    Thanks lightOfDay I tried to recreate that but never got anywhere. I've tried again and it's still not working as it should so I'm just going to scrap it and go with helloworld922's suggestion.

  9. #9
    Member
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: 3D Rotations

    Admittedly I've only gotten the perspective to work once XD. I'm interested in this now though... I have yet to find a matrix that can do projections...how do you do that? I'm glad you went with the Matrices, because it is the way to go, even if you end up using my projection.

  10. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 3D Rotations

    This page has a bunch of useful information on various transformations, including C sample code and mathematical explainations. If you scroll down to Perspective Projection section (about halfway down the page) you'll find the formula for calculating the projection matrix based off of the vertical FOV, aspect ratio, and near/far planes.

Similar Threads

  1. AVL Tree. root not changing from rotations
    By beginnerprogrammer in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 11th, 2011, 03:36 PM