1
\$\begingroup\$

I'm creating a rotation for my sun to move, however it quickly extends to Infinity before going into NaN. I thought that taking advantage of the Matrix4f would make this much easier but it does as previously stated. Ideally should have the sun rotate around the sunCenter. Why is it doing this and how would I fix it?

private Vector3f getSunPosition() {
    double rotation = (time / DAY_LENGTH) * 360;
    Matrix4f matrix = new Matrix4f();

    Vector3f pos = sun.getPosition();
    matrix.m03 = pos.x;
    matrix.m13 = pos.y;
    matrix.m23 = pos.z;

    Matrix4f.rotate((float) Math.toRadians(rotation), sunCenter, matrix, matrix);

    return new Vector3f(matrix.m03, matrix.m13, matrix.m23);
}

I had it print out the Vector3f and rotation. The rotation looks a bit odd at first, however I can fix that another time.

Vector3f[1.0, 1.0, 1.0] 0.0
Vector3f[288375.56, 0.18377686, 287549.88] 79.41000366210938
Vector3f[1.96323607E11, -125296.0, 1.96323607E11] 158.82000732421875
Vector3f[1.05730567E17, 0.0, 1.05730567E17] 238.23001098632812
Vector3f[5.5935644E22, 0.0, 5.5935644E22] 240.02999877929688
Vector3f[2.9050322E28, 0.0, 2.9050322E28] 241.8300018310547
Vector3f[1.4801178E34, 0.0, 1.4801178E34] 243.6300048828125
Vector3f[Infinity, 0.0, Infinity] 243.8400115966797
Vector3f[Infinity, NaN, Infinity] 244.04998779296875
Vector3f[NaN, NaN, NaN] 244.25999450683594
Vector3f[NaN, NaN, NaN] 244.41000366210938
Vector3f[NaN, NaN, NaN] 244.55999755859375
\$\endgroup\$
  • \$\begingroup\$ Is this "...matrix, matrix);" a matter of result and argument? If so, can you use the same variable for both, simultaneously? \$\endgroup\$ – Stormwind Jul 11 '17 at 20:00
  • \$\begingroup\$ You can. It is simply an "...input, output);" \$\endgroup\$ – TheGamerPlayz Jul 11 '17 at 21:18
2
\$\begingroup\$

Instead of rotating the sun position based on its current position, generate the sun position based on the parameter of time. That way you don't have repeated multiplications of the same value, and it will be easier for you to understand. I'm not familiar with the framework you're using but you might be able to just do something like this:

// Convert units of time to radians (one complete rotation per DAY_LENGTH)
const float time2radians = 2 * PI / DAY_LENGTH;
// Size of the sky in game units.
const float skyRadius = 999;

// Assume that the sky is rotating around the z axis, and the sun sits 
//on the XZ plane. Assume that the world center is (0, 0, 0). Create a 
// 3x3 matrix, multiply it by a unit vector to get a direction. Multiply
// The direction by the distance to the sun to get sun position.
vec3 getSun(float time) {
    return skyRadius * (Matrix3f.createRotationZ(time * time2radians) 
                     * vec3(0, 0, 1));
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.