Programmatically creating a Camera -> image upside down?

#1

Hi everyone,

In short: when I try to create the camera from the Sphere example programmatically, the resulting image is upside down. I have no idea why.

The Sphere example uses a prefab RON file, containing exactly one sphere, one light and one camera. The camera and resulting image look like this:

(
    data: (
        transform: (
            translation: (0.0, 0.0, -4.0),
            rotation: (0.0, 1.0, 0.0, 0.0),
        ),
        camera: Perspective(
            aspect: 1.3,
            fovy: 1.0471975512,
            znear: 0.1,
            zfar: 2000.0,
        ),
    ),
)

If I then comment out that camera from the prefab, and add this code to my state on_start(), I see the following output:

let transform = Transform::new(
    Translation3::new(0.0, 0.0, -4.0),
    UnitQuaternion::from_quaternion(Quaternion::new(0.0, 1.0, 0.0, 0.0)),
    Vector3::new(1.0, 1.0, 1.0),
);

let camera = {
    let projection = Projection::perspective(1.3, 1.0471975512, 0.1, 2000.0);
    Camera::custom_matrix(*projection.as_matrix())
};

world.create_entity().with(transform).with(camera).build();

The sphere is upside down! I’ve been debugging this for more than an hour now and I’ve yet to find the source of this problem. I’m feeding exactly the same numbers programmatically into the camera component. So, what gives?

(Azriel Hoh) #2

Heya, the order of parameters is different:

https://github.com/amethyst/amethyst/blob/v0.14.0/amethyst_core/src/transform/components/transform.rs#L641-L646

Unit::new_normalize(Quaternion::new(
    rotation[3],
    rotation[0],
    rotation[1],
    rotation[2],
)),

nalgebra uses x, y, z, w for its matrix, and I think I saw somewhere that it’s de/serialized as w, x, y, z. The change for that is in this PR. I don’t really understand why though (seeing that the serialization / deserialization type is Quaternion, which is from nalgebra).

1 Like
#3

Oh wow, how did you catch that! Thanks a ton :slight_smile: