Hello Guest

dot product and rounding errors in LWJGL

  • 2 Replies
  • 8469 Views
*

Offline Endolf

  • ***
  • 101
    • http://www.computerbooth.com/
dot product and rounding errors in LWJGL
« on: June 13, 2003, 14:06:23 »
I'm trying to port some work from lwjgl to jogl for comparisons, and as jogl currently has no vector/matrix stuff i'm writting my own by looking at http://www.martinb.com/maths/index.htm the trouble is, my results and lwjgl results don't match, i've tested with java3d too, either there is a bug in my test, or in lwjgl, either way it needs fixing, can you spot it?

Cheers

Endolf

here is the results
Code: [Select]
jjgl says
(x: 1.0 y: 1.0 z: 1.0) dot (x: -1.0 y: 0.0 z: 0.0) = -57.29577951308232
(x: -1.0 y: 0.0 z: 0.0) dot (x: 1.0 y: 0.0 z: 0.0) = -57.29577951308232
(x: 1.0 y: 1.0 z: 1.0) dot (x: 0.0 y: 1.0 z: 0.0) = 57.29577951308232
(x: -1.0 y: 0.0 z: 0.0) dot (x: 0.0 y: 1.0 z: 0.0) = 0.0
(x: 1.0 y: 0.0 z: 0.0) dot (x: 0.0 y: 1.0 z: 0.0) = 0.0
(x: 1.0 y: 1.0 z: 1.0) angle (x: -1.0 y: 0.0 z: 0.0) = 125.26438895559582
(x: -1.0 y: 0.0 z: 0.0) angle (x: 1.0 y: 0.0 z: 0.0) = 180.0
(x: 1.0 y: 1.0 z: 1.0) angle (x: 0.0 y: 1.0 z: 0.0) = 54.735611044404195
(x: -1.0 y: 0.0 z: 0.0) angle (x: 0.0 y: 1.0 z: 0.0) = 90.0
(x: 1.0 y: 0.0 z: 0.0) angle (x: 0.0 y: 1.0 z: 0.0) = 90.0
(x: 1.0 y: 1.0 z: 1.0) length = 1.7320508075688772
(x: -1.0 y: 0.0 z: 0.0) length = 1.0
(x: 1.0 y: 0.0 z: 0.0) length = 1.0
(x: 0.0 y: 1.0 z: 0.0) length = 1.0
LWJGL says
(Vector3f[1.0, 1.0, 1.0]) dot (Vector3f[-1.0, 0.0, 0.0]) = -1.0
(Vector3f[-1.0, 0.0, 0.0]) dot (Vector3f[1.0, 0.0, 0.0]) = -1.0
(Vector3f[1.0, 1.0, 1.0]) dot (Vector3f[0.0, 1.0, 0.0]) = 1.0
(Vector3f[-1.0, 0.0, 0.0]) dot (Vector3f[0.0, 1.0, 0.0]) = 0.0
(Vector3f[1.0, 0.0, 0.0]) dot (Vector3f[0.0, 1.0, 0.0]) = 0.0
(Vector3f[1.0, 1.0, 1.0]) angle (Vector3f[-1.0, 0.0, 0.0]) = 125.16059
(Vector3f[-1.0, 0.0, 0.0]) angle (Vector3f[1.0, 0.0, 0.0]) = 175.28716
(Vector3f[1.0, 1.0, 1.0]) angle (Vector3f[0.0, 1.0, 0.0]) = 54.83941
(Vector3f[-1.0, 0.0, 0.0]) angle (Vector3f[0.0, 1.0, 0.0]) = 90.0
(Vector3f[1.0, 0.0, 0.0]) angle (Vector3f[0.0, 1.0, 0.0]) = 90.0
(Vector3f[1.0, 1.0, 1.0]) length = 1.7335646
(Vector3f[-1.0, 0.0, 0.0]) length = 1.0016948
(Vector3f[1.0, 0.0, 0.0]) length = 1.0016948
(Vector3f[0.0, 1.0, 0.0]) length = 1.0016948
Java3D says
((1.0, 1.0, 1.0)) dot ((-1.0, 0.0, 0.0)) = -57.29577951308232
((-1.0, 0.0, 0.0)) dot ((1.0, 0.0, 0.0)) = -57.29577951308232
((1.0, 1.0, 1.0)) dot ((0.0, 1.0, 0.0)) = 57.29577951308232
((-1.0, 0.0, 0.0)) dot ((0.0, 1.0, 0.0)) = 0.0
((1.0, 0.0, 0.0)) dot ((0.0, 1.0, 0.0)) = 0.0
((1.0, 1.0, 1.0)) angle ((-1.0, 0.0, 0.0)) = 125.26438530246989
((-1.0, 0.0, 0.0)) angle ((1.0, 0.0, 0.0)) = 180.00000500895632
((1.0, 1.0, 1.0)) angle ((0.0, 1.0, 0.0)) = 54.735609461202685
((-1.0, 0.0, 0.0)) angle ((0.0, 1.0, 0.0)) = 90.00000250447816
((1.0, 0.0, 0.0)) angle ((0.0, 1.0, 0.0)) = 90.00000250447816
((1.0, 1.0, 1.0)) length = 1.7320508
((-1.0, 0.0, 0.0)) length = 1.0
((1.0, 0.0, 0.0)) length = 1.0
((0.0, 1.0, 0.0)) length = 1.0


and the test source

Code: [Select]
package org.jez.jjgl.test;

import org.jez.jjgl.math.Tuple3f;
import org.jez.jjgl.math.Vector3f;

/**
 *
 * @author  Jeremy
 */
public class Vector {
   
    /** Creates a new instance of Vector */
    public Vector() {
        System.out.println("jjgl says");

        testjjgl();
       
        System.out.println("LWJGL says");
       
        testlwjgl();
       
        System.out.println("Java3D says");
       
        testjava3d();
    }

    private void testjava3d() {
        javax.vecmath.Vector3f vector1 = new javax.vecmath.Vector3f(1,1,1);
        javax.vecmath.Vector3f vector2 = new javax.vecmath.Vector3f(-1,0,0);
        javax.vecmath.Vector3f vector3 = new javax.vecmath.Vector3f(1,0,0);
        javax.vecmath.Vector3f vector4 = new javax.vecmath.Vector3f(0,1,0);
       
        //dot
        System.out.println("(" + vector1 + ") dot (" + vector2 + ") = " + Math.toDegrees(vector1.dot(vector2)));
        System.out.println("(" + vector2 + ") dot (" + vector3 + ") = " + Math.toDegrees(vector2.dot(vector3)));
        System.out.println("(" + vector1 + ") dot (" + vector4 + ") = " + Math.toDegrees(vector1.dot(vector4)));
        System.out.println("(" + vector2 + ") dot (" + vector4 + ") = " + Math.toDegrees(vector2.dot(vector4)));
        System.out.println("(" + vector3 + ") dot (" + vector4 + ") = " + Math.toDegrees(vector3.dot(vector4)));

        //angle
        System.out.println("(" + vector1 + ") angle (" + vector2 + ") = " + Math.toDegrees(vector1.angle(vector2)));
        System.out.println("(" + vector2 + ") angle (" + vector3 + ") = " + Math.toDegrees(vector2.angle(vector3)));
        System.out.println("(" + vector1 + ") angle (" + vector4 + ") = " + Math.toDegrees(vector1.angle(vector4)));
        System.out.println("(" + vector2 + ") angle (" + vector4 + ") = " + Math.toDegrees(vector2.angle(vector4)));
        System.out.println("(" + vector3 + ") angle (" + vector4 + ") = " + Math.toDegrees(vector3.angle(vector4)));

        //length
        System.out.println("(" + vector1 + ") length = " + vector1.length());
        System.out.println("(" + vector2 + ") length = " + vector2.length());
        System.out.println("(" + vector3 + ") length = " + vector3.length());
        System.out.println("(" + vector4 + ") length = " + vector4.length());
    }
   
    private void testjjgl() {
        Tuple3f tuple = new Tuple3f(1,1,1);
        Vector3f vector1 = new Vector3f(tuple);
        Vector3f vector2 = new Vector3f(-1,0,0);
        Vector3f vector3 = new Vector3f(1,0,0);
        Vector3f vector4 = new Vector3f(0,1,0);
       
        //dot
        System.out.println("(" + vector1 + ") dot (" + vector2 + ") = " + Math.toDegrees(vector1.dot(vector2)));
        System.out.println("(" + vector2 + ") dot (" + vector3 + ") = " + Math.toDegrees(vector2.dot(vector3)));
        System.out.println("(" + vector1 + ") dot (" + vector4 + ") = " + Math.toDegrees(vector1.dot(vector4)));
        System.out.println("(" + vector2 + ") dot (" + vector4 + ") = " + Math.toDegrees(vector2.dot(vector4)));
        System.out.println("(" + vector3 + ") dot (" + vector4 + ") = " + Math.toDegrees(vector3.dot(vector4)));

        //angle
        System.out.println("(" + vector1 + ") angle (" + vector2 + ") = " + Math.toDegrees(vector1.angle(vector2)));
        System.out.println("(" + vector2 + ") angle (" + vector3 + ") = " + Math.toDegrees(vector2.angle(vector3)));
        System.out.println("(" + vector1 + ") angle (" + vector4 + ") = " + Math.toDegrees(vector1.angle(vector4)));
        System.out.println("(" + vector2 + ") angle (" + vector4 + ") = " + Math.toDegrees(vector2.angle(vector4)));
        System.out.println("(" + vector3 + ") angle (" + vector4 + ") = " + Math.toDegrees(vector3.angle(vector4)));

        //length
        System.out.println("(" + vector1 + ") length = " + vector1.length());
        System.out.println("(" + vector2 + ") length = " + vector2.length());
        System.out.println("(" + vector3 + ") length = " + vector3.length());
        System.out.println("(" + vector4 + ") length = " + vector4.length());
    }

    private void testlwjgl() {
        org.lwjgl.vector.Vector3f vector1 = new org.lwjgl.vector.Vector3f(1,1,1);
        org.lwjgl.vector.Vector3f vector2 = new org.lwjgl.vector.Vector3f(-1,0,0);
        org.lwjgl.vector.Vector3f vector3 = new org.lwjgl.vector.Vector3f(1,0,0);
        org.lwjgl.vector.Vector3f vector4 = new org.lwjgl.vector.Vector3f(0,1,0);
       
        //dot
        System.out.println("(" + vector1 + ") dot (" + vector2 + ") = " + org.lwjgl.vector.Vector3f.dot(vector1,vector2));
        System.out.println("(" + vector2 + ") dot (" + vector3 + ") = " + org.lwjgl.vector.Vector3f.dot(vector2,vector3));
        System.out.println("(" + vector1 + ") dot (" + vector4 + ") = " + org.lwjgl.vector.Vector3f.dot(vector1,vector4));
        System.out.println("(" + vector2 + ") dot (" + vector4 + ") = " + org.lwjgl.vector.Vector3f.dot(vector2,vector4));
        System.out.println("(" + vector3 + ") dot (" + vector4 + ") = " + org.lwjgl.vector.Vector3f.dot(vector3,vector4));
       
        //angle
        System.out.println("(" + vector1 + ") angle (" + vector2 + ") = " + org.lwjgl.vector.Vector3f.angle(vector1,vector2));
        System.out.println("(" + vector2 + ") angle (" + vector3 + ") = " + org.lwjgl.vector.Vector3f.angle(vector2,vector3));
        System.out.println("(" + vector1 + ") angle (" + vector4 + ") = " + org.lwjgl.vector.Vector3f.angle(vector1,vector4));
        System.out.println("(" + vector2 + ") angle (" + vector4 + ") = " + org.lwjgl.vector.Vector3f.angle(vector2,vector4));
        System.out.println("(" + vector3 + ") angle (" + vector4 + ") = " + org.lwjgl.vector.Vector3f.angle(vector3,vector4));
       
        //length
        System.out.println("(" + vector1 + ") length = " + vector1.length());
        System.out.println("(" + vector2 + ") length = " + vector2.length());
        System.out.println("(" + vector3 + ") length = " + vector3.length());
        System.out.println("(" + vector4 + ") length = " + vector4.length());
    }
   
    public static void main(String[] args) {
        new Vector();
    }
}

dot product and rounding errors in LWJGL
« Reply #1 on: June 13, 2003, 15:21:11 »
Your LWJGL-test doesn't call Math.toDegrees() after doing the dot-product while the other two tests do...

*

Offline Endolf

  • ***
  • 101
    • http://www.computerbooth.com/
dot product and rounding errors in LWJGL
« Reply #2 on: June 13, 2003, 15:51:48 »
D'oh
  Why is it that I can debug and fix some pieces of fairly complex code, and yet a really really stupid and obvious mistake like that foils me for aaaaaages?? ho hum


 :oops:  :oops:  :oops: