1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • I'm having difficulty visualizing your issue. Or perhaps I don't understand what you mean by `The problem is that when the objects are too much far, the movement is not realistic`. Have you tried a factor lookup table so that scale isn't linear? – Morrison Chang Feb 10 '17 at 20:29
  • Hmm... To get a complete natural look without 'thinking 3D' might be kind of complex. My guess is that the angle from the 'joystick' to the 'views' should be at least be correlated as `1/r`, where r is the distance to the view. If we move the joystick a distance x, the angle to the object changes with ~`tan(angle) = x/r`. But this is only true for objects straight ahead, closer to the screen edge the values will be different. Sounds like you want some kind of 3D frustum? – pingul Feb 10 '17 at 21:26
  • @MorrisonChang the problem is that when the object is very far (0.1-0.2 scale etc) it moves extremely slow and when the scale increases suddenly it moves in a munch more realistic way. I did not tryed with a non linear factor because I don't know how to generate that non linear factor. Do you have any idea about how to achieve it? – NullPointerException Feb 10 '17 at 23:30
  • @pingul Don't have the angle, because it's a 2d game, also there is no distance between the items. The distance is simulated by scale factors. It is a game that simulates objects coming to the POV of the player, and he must avoid them moving the vehicle in x-y directions – NullPointerException Feb 10 '17 at 23:32
  • @NullPointerException A lookup table would either a hash or array where the input would be your scale and the factor would be some other number (not always 4 but could be). As for generating the actual numbers - trial and error. – Morrison Chang Feb 10 '17 at 23:43
  • I don't think I understand what the problem is in that case. You want a "simulated 3D effect", but then there is "no distance between the items"? A image/movie would really help to clarify things. – pingul Feb 12 '17 at 20:19

0 Answers0