[SOLVED] Realistic-looking airplane rotation

	float[] angles = new float[3];
	angles = airplane.getLocalRotation().toAngles(angles);

This should give you an array with x, y and z rotation in radians or degrees, I can’t remember. You’d have to test it.

But if you want the angle in world rotation then make sure to get world rotation.

Thanks all! The plane moves 95% how I would like it to move. It stays level, can be turned, and can be rolled by pressing keys. Thank you all for your help.

For future visitors, here’s the code:

// Rotation code
    flyCam.setDragToRotate(true); //So that the mouse is not grabbed. Only needs to be called once.
    float[] angles = new float[3];
angles = plane.getLocalRotation().toAngles(angles);
    double z = Math.toDegrees(angles[2]); //Returns between -90 and 90.
    
    int widt = Display.getWidth();
    int heit = Display.getHeight();
    float verti = -(getInputManager().getCursorPosition().y-15 - heit/2)*1080/heit; 
    float horiz = -(getInputManager().getCursorPosition().x+16 - widt/2)*1920/widt;
    float ro = (float) -z / 50; //Divide the value by 50 so the planes comes back "smoothly".
    if (roll != 0){
        ro = roll; //"Roll" is a outside variable, representing if the player wants to roll the plane manually using the A and D keys. Set roll to 1 to roll right, -1 to roll left.
    }
    Vector3f axis = new Vector3f(verti/400,horiz/600, ro);
    axis = plane.getLocalRotation().mult(axis); //"Plane" equals the Spatial you are moving.
    player.setAngularVelocity(axis); //You can also apply a torque here if there is high damping.
    //End of rotation

For this case, it seems that the world rotation and the local rotation return the same value.

The angle is in radians. I have used .toDegrees() to convert it.

The number in the middle of the screen is the rotation (from radians, converted to degrees) of the plane that the .toAngles method returns. Is there any way to make it more linear, so for example, upside down is 180?

1 Like

Well, good that we could help you.

For future improvements you might want to study aerodynamics and put the things you learned into your game. I recently took a two days course of astronomy as an autodidact - and at the third day I had a correctly moving sun and a moon with phase. So, sometimes puttings some hours into science stuff really helps. :chimpanzee_smile:

Marked the topic as [SOLVED].

I’m not going to call is 100% solved yet, as I still need to find the resolution to getting the plane roll in degrees between 0 and 360 (or -180 and 180), instead of -90 to 90.

Mainly because I would need to know the exact rotation to make more realistic rolls (for example, making the plane bank further depending on how much you turn).

I saw your thread, and it definitely seemed to be really awesome. Out of interest, do you know of any online videos about aerodynamics that may be relevant to games?

I’ve never touched aerodynamics. But I’m sure the internet is full with stuff like that.
I once found a very good article describing how helicopter mechanics work in detail.

You can load up KSP, make a spaceplane and enable show forces. That should give you a basic understanding of aerodynamic forces, even more so if you install the Ferram Aerospace Research mod.