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 5 of 5

Thread: graphics engine rotations not working

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default graphics engine rotations not working

    It's my first time here so be gently.

    I've had this old graphics project laying around (written in oberon) and since i wrote it as one of my first projects it looks kinda chaotic.
    So I descided that, since i'm bored anyway, i would rewrite it in java.

    Everything so far seems to work... Until i try to rotate and do my eye-point transformation.
    If i simply draw the image without rotating or changing the "eye's" position it works fine.

    However the moment i try to do any of the operations that require me to multiply a point with a transformation matrix it all goes bad.

    1. the eye point transformation generates stupidly small numbers with end coördinates like [-0.002027571306540029, 0.05938634628270456, -123.30022583847628]
    this causes the resulting image to look empty but upon multiplying each coordinate with 1000 it turns out it's just moved around the screen rather than being rotated

    if i then ignore the eye point and simply focus on my rotations the results are also pretty strange:

    2. setting xRotation to 90° only makes the image very narrow and way too high (resolution should be 1000x1000 and is then 138x1000
    3. setting yRotation to 90° makes it very wide (1000x138)
    4. setting zRotation to 90° simply seems to translate the image all the way to the right side of the screen.

    What i have checked so far:

    1. i have checked and re-checked my rotation matrices at least 15 times now so they are correct
    2. doing a test multiplication with a vector and the identity matrix does return the original vector
    3. my matrices are initialized as identity matrices prior to being used as rotation matrices
    4. the angles in the files are in degrees but are converted to radian when read.

    Having said that i have 2 more notes:

    1. a vector in this case is a simple 3 value array of doubles (obviously representing the x, y and z values)
    2. a matrix is a 4x4 array of doubles initialized as the identity matrix

    The reason i gave them their own classes is for code readability and to make it easier when doing operations on them.

    So without further ado here are a couple of code snippets...

    here's the code responsible for initializing the rotation matrices based on an angle or the eye-point vector if it's the eye-transformation matrix
    [spoiler]
    public static Matrix eye_transformation(Vector3D eye)throws ParseException
    {
    double r = eye.length();
    double teta = Math.atan2(eye.y(), eye.x());
    double zr = eye.z()/r;
    double fi = Math.acos(zr);
     
    Matrix v = new Matrix();
     
    v.set(0, 0, -Math.sin(teta));
    v.set(0, 1, -Math.cos(teta) * Math.cos(fi));
    v.set(0, 2, Math.cos(teta) * Math.sin(fi));
    v.set(1, 0, Math.cos(teta));
    v.set(1, 1, -Math.sin(teta) * Math.cos(fi));
    v.set(1, 2, Math.sin(teta) * Math.sin(fi));
    v.set(2, 1, Math.sin(fi));
    v.set(2, 2, Math.cos(fi));
    v.set(3, 2, -r);
     
    return v;
    }
     
    public static Matrix z_rotation(double angle) throws ParseException
    {
    Matrix v = new Matrix();;
     
    v.set(0, 0, Math.cos(angle));
    v.set(0, 1, Math.sin(angle));
    v.set(1, 0, -Math.sin(angle));
    v.set(1, 1, Math.cos(angle));
     
    return v;
    }
     
    public static Matrix x_rotation(double angle) throws ParseException
    {
    Matrix v = new Matrix();;
     
    v.set(1, 1, Math.cos(angle));
    v.set(1, 2, Math.sin(angle));
    v.set(2, 1, -Math.sin(angle));
    v.set(2, 2, Math.cos(angle));
     
    return v;
    }
     
    public static Matrix y_rotation(double angle) throws ParseException
    {
    Matrix v = new Matrix();
     
    v.set(0, 0, Math.cos(angle));
    v.set(0, 2, -Math.sin(angle));
    v.set(2, 0, Math.sin(angle));
    v.set(2, 2, Math.cos(angle));
     
    return v;
    }
     
    public static Matrix translation(double a, double b, double c) throws ParseException
    {
    Matrix v = new Matrix();;
     
    v.set(3, 0, a);
    v.set(3, 1, b);
    v.set(3, 2, c);
     
    return v;
    }
    [/spoiler

    here's the code that actually multiplies a point with a matrix:
    [spoiler]
    public static Vector3D mult(Vector3D lhs, Matrix rhs) throws ParseException
    	{
    //notes: NR_DIMS = 3
    //          since the matrix is 4x4 we need to add an extra value to our point (depending on whether it's a point or a vector)
     
    		if(rhs.get(0, 3)!=0 || rhs.get(1, 3)!=0 || rhs.get(2, 3)!=0 || rhs.get(3, 3)!=1)
    			throw new ParseException("the matrix multiplificiation thingy just borked");
     
    		Vector3D ret = new Vector3D();
    		double[] vec = new double[NR_DIMS];
     
    		double[] temp = new double[NR_DIMS+1];		
    		temp[0] = lhs.x;
    		temp[1] = lhs.y;
    		temp[2] = lhs.z;
    		temp[3] = lhs.infty? 0:1;  //is it a point or a vector?
     
    		for (int i = 0; i < NR_DIMS; i++)
            {
                    vec[i] = 0;
     
                    // Multiply the original vector with the i-th column of the matrix.
                    for (int j = 0; j <= NR_DIMS; j++)
                    {
                            vec[i] += temp[j] * rhs.get(j,i);
                    }
            }
     
    		ret.x = vec[0];
    		ret.y = vec[1];
    		ret.z = vec[2];
    		ret.infty = lhs.infty;
     
    		return ret;
    	}
    [/spoiler]

    and here's the code that should rotate the figure
    [spoiler]
    	protected void rotate() throws ParseException
    	{
    		Matrix rotate_x = Transformations.x_rotation(rotateX);
    		Matrix rotate_y = Transformations.y_rotation(rotateY);
    		Matrix rotate_z = Transformations.z_rotation(rotateZ);
    		Matrix translate = Transformations.translation(center.x(), center.y(), center.z());
     
    		for(Vector3D point : points)
    		{
    			System.out.println(point.toString());
    			point = Vector3D.mult(point, scale);
    			System.out.println(point.toString());
    			point = Vector3D.mult(point, rotate_x);
    			System.out.println(point.toString());
    			point = Vector3D.mult(point, rotate_y);
    			System.out.println(point.toString());
    			point = Vector3D.mult(point, rotate_z);
    			System.out.println(point.toString());
    			point = Vector3D.mult(point, translate);
    			System.out.println(point.toString());
    			point = Vector3D.mult(point, eye);
    			System.out.println(point.toString());
     
    			if(point.z() != 0)
    			{
    				point.setX(point.x()/(-point.z()));
    				point.setY(point.y()/(-point.z()));
    			}
     
    			System.out.println(point.toString() + "\n");
     
    			checkMinMax(point);
    		}
    	}
    [/spoiler]

    Any ideas as to what i might be doing wrong?

    edit: this forum doesn't support spoilers?
    Also i have posted this question on one other forum: http://www.teamavolition.com/topic/1...n-not-working/
    but it seems to have died there :\


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: graphics engine rotations not working

    I had to look up Oberon, but I don't think it matters.

    Can you show the difference in the math results from the old code to the Java translation and post just the Java code that provides those results? I'm trying to get you to narrow the question down to what really matters, and I'm not even sure I know what that is, but you seem to be surprised at the very small numbers generated by the Java code. Show those results compared to the Oberon code's results and the Java code than gave those numbers.

    I could be wrong, but it sounds like you're happy with the results overall, but there's an issue with the scaling. Is there a scaling factor somewhere you can point to or manipulate to change the results? Maybe I'm not even on the right track, but that's what I've gathered so far.

    Don't give up. Good luck.

    Oh, and as for the spoiler feature, I'm fairly new myself, but I've never used it on the other forums I belong to so didn't think about using it here. I never saw much use for it other than really hiding something I wanted someone to consciously decide to view. (And I consider every word I type to be viewable and a gem to be discovered and cherished by all.) If you consider it an important feature, ask for it in Forum Feedback, and maybe it's something that can be turned on.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: graphics engine rotations not working

    well, i used a specific compiler that was licensed to the university i used to study at. Since i formatted my computer i'm stuck with just the source code but no compiler :\
    So I'm afraid running them side by side will be impossible (i wish i could, i really do).

    I can, however, say that the resulting coördinates have to be positive and between 0 and image width for the x-coördinates and between 0 and image-Y for the y-coördinates (with a 0.95 factor offset to have a little border around the image).

    here's a little snippet of the list of points from a 2D image that does generate correctly:

    x0   y0   x1   y1
    126 652 126 652
    126 652 127 652
    127 652 127 652
    127 652 126 652
    126 652 126 652
    126 652 126 652
    126 652 126 652
    126 652 126 652

    the list holds thousands and thousands of these points going from 0,0 all the way up to imagewidth, imageheight

    It's also certain that is has nothing to do with the scaling factor because it is set at 1 and will not (and should not) influence the image. Even if i were to scale up the image it would not come out the way i want it to come out (which is rotated rather than translated).

  4. #4
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: graphics engine rotations not working

    no more suggestions? :c

  5. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: graphics engine rotations not working

    i guess i'll try my luck elsewhere. Thanks anyway.

Similar Threads

  1. what are game engine
    By game06 in forum Java Theory & Questions
    Replies: 3
    Last Post: April 17th, 2013, 05:05 AM
  2. 3D Rotations
    By xdonny in forum Java Theory & Questions
    Replies: 9
    Last Post: October 17th, 2012, 12:06 PM
  3. Mini Search Engine Help
    By modernmesih in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 17th, 2011, 05:53 AM
  4. 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
  5. Has anybody used Java Search Engine?
    By jayab in forum Member Introductions
    Replies: 2
    Last Post: April 26th, 2010, 04:23 AM