I'm developing a game engine. It's more complex but I will reduce the environment to a very little situation in order to explain my issue easily:
The game is made in 2D using simple views. Some views are coming to the screen. The game engine simulates that those views are coming to the screen doing a scaleX and scaleY animation to the views which will crash into the screen.
The game has a joystick on screen which generates movements from -10 to +10px and the user will use it to avoid the objects.
If you move the joystick to the the maximum right then +10 is added to all the objects which are coming to the screen, and every 10ms the screen will repaint, so, each 10ms the objects will move when you move the joystick and the user will have the simulated 3d effect of avoiding the objects.
I want to represent that the objects which are more far, don't move as fast as the objects that are very near of the screen, so I did this:
view = new ImageView(context){ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); float currentScale = (Float) scaleX.getAnimatedValue(); float factor= currentScale/MAX_SCALE; view.setX(view.getX()-((float)GameState.getInstance().getJoyX()*factor)); view.setY(view.getY()-((float)GameState.getInstance().getJoyY()*factor)); } };
As you can see I calculated a factor. The furthest objects has 0.1f scale factor, and the nearest objects has a 4.0f scale factor, so these are some possible values of the factor:
0.1/4 = 0.025
0.5/4 = 0.125
1.0/4 = 0.250
2.0/4 = 0.500
4.0/4 = 1.000
When I multiply the supposed movement for that factor, I can reproduce that the furthest objects moves more slowly than the nearest objects.
The problem is that when the objects are too much far, the movement is not realistic, so I need help to find a more realistic factor for multiplying the supposed movement of the objects and get a more real 3D effect when you move the joystick.
*The game is much more complicated, and y-axis is also present in the movement, but I explained it with x-axis only to make it simpler.