Topic: basic flight simulator

Hi

I'm trying to implement a (basic) flight simulator. I took a look at the fly demo scene and the fly_between_airport scene. I'm still trying to find out the best way to go with it.

Right now i'm trying to adapt the "followGameObject" script, which looks promising to achieve what i want. I'm adding to this script the same logic used in the "airRoute" script in flight between airport scene:

https://i.imgur.com/J4Pyc0r.jpg

So the idea is basically to adapt the map zoom level to the aircraft altitude. Also to keep a feeling of being "in the air" i don't want the aircraft to be sitting on a map that change it's zoom. So what i have done is :

-> if the aircraft altitude i less than a 100 meters, the map zoom level does'nt change and the aircraft will just "fly" at it's real altitude above the map mesh. When it reach an altitude > 100m, the map stay at a y distance of 100 meters under the aircraft (to prevent loosing sight of the map gameobject completely) and the map zoom should start to change to keep the impression of moving up.

Result is : its'kind of working. The logic seems to apply well, exept that when the map is changing it's zoom level, it give the impression that the aircraft is moving horizontally (which is not the case, right now i'm only moving my aircrapft on the Y axis to build this altitude-coherent-behavior. 

(and also the view is not realisticly following the altitude but i guess i will fix that by adapting the altitudeCurve / playing with altitude limit where map start following the aircraft y position)

As you can see, if i make variation with the altitude ( going up and down) the aircrapt always land a few meters aways of it's initial position. I think it's due to map center repositionning :

https://gph.is/g/aj1Yd3p

With a greater altitude variation :

https://gph.is/g/Z5BA5r6

(-> the fun fact is that i'm going to put some effort to reproduce this behaviour later, as i'm planning to implement Coriolis force. But right now my aircraft is not supposed to change it's horizontal position.)

and if i comment the line that make the map center reposition itself, it work well :

/// <summary>
    /// Updates map position
    /// </summary>
    private void UpdateMap()
    {
        // Store last position of GameObject
        lastPosition = target.transform.position;

        altitude = target.transform.position.y;

        // Size of map in scene
        Vector2 size = control.sizeInScene;

        // Calculate offset (in tile position)
        Vector3 offset = lastPosition - map.transform.position - control.center;
        offset.x = offset.x / OnlineMapsUtils.tileSize / size.x * map.width * map.zoomCoof;
        offset.z = offset.z / OnlineMapsUtils.tileSize / size.y * map.height * map.zoomCoof;

        // Calculate current tile position of the center of the map
        tx -= offset.x;
        ty += offset.z;

           //////////////////////////////  IT IS HERE : ///////////////////////////////////////////////////////////
        // Set position of the map center
        // If i comment this line, It will behave propely when moving along Y axis.
        // But the "follow-gamobject" logic will be broken if i want to move horizontally the aircraft.
//        map.SetTilePosition(tx, ty); 


        // Update map GameObject position
        Vector3 newMapPos = lastPosition - control.center;
        
        if(target.transform.position.y > 100)
        {
            newMapPos.y = target.transform.position.y - 100;
            map.floatZoom = altitudeZoomCurve.Evaluate(altitude / maxAltitude);
        }
        else
        {
            newMapPos.y = 0;
        }

        map.transform.position = newMapPos;
    }

https://gph.is/g/aNgoAnG

Exept of course that it won't follow my aircraft anymore in case of horizontal movement.

So how can i have both behavior - a correct y variatation and a map following the horizontal movement of my aircraft ?

Not sure if relevant but :
- i planned to use elevation
- i also woud like to use horizon. Not sure how it would behave with this followGamobject script though.
- if you think i'm not going in the correct direction to achieve what i want, could you point me toward a better solution ?

Thanks for your time and sorry for the long post smile

Re: basic flight simulator

Hello.

Formulas for converting coordinates to tiles and vice versa are not ideal and have a small error.
This error is very-very small, and in most cases it can be ignored, but in your case the error accumulates every frame and causes a map shift.

What happens in your script:
- You updated the altitude of the balloon, but did not update the position of XZ.
- Called SetTilePosition with the same tile position that you had.
- Inside SetTilePosition, it calls projection.TileToCoordinates, which generates a small error.
FollowGameObject does not know about this because it is designed in such a way as to re-use the tile position that it has within itself.
- Then you update floatZoom, which calls OnChangeZoom and reads the tile position of the map.
- GetTilePosition internally uses projection.CoordinatesToTile, which generates another small error.
- FollowGameObject reads both errors and you have an invisible map shift.
- You repeat this every frame, and have a visible map shift.

How to fix it:
Before calling SetTilePosition, make sure you have a offset.
Something like:

if (Mathf.Abs(offset.x) > float.Epsilon || Mathf.Abs(offset.z) > float.Epsilon) map.SetTilePosition(tx, ty);

or
If you plan to update X and Y and Z each frame, you need to delete OnChangeZoom, and recalculate tx and ty manually.
Something like:

int prevZoom = map.zoom;
map.floatZoom = altitudeZoomCurve.Evaluate(altitude / maxAltitude);
if (map.zoom != prevZoom)
{
    if (map.zoom > prevZoom)
    {
        tx *= 2;
        ty *= 2;
    }
    else
    {
        tx /= 2;
        ty /= 2;
    }
}
Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.