3

When writing code like this, why is the y axis inverted, meaning positive values make an object go down while negative values make it go up?

 animation.update();

    if(up){
        dy -=1.1;

    }
    else{
        dy +=1.1;
    }

    if(dy>14)dy = 14;
    if(dy<-14)dy = -14;

    y += dy*2;
    dy = 0;
}

public void draw(Canvas canvas)
{
    canvas.drawBitmap(animation.getImage(),x,y,null);
}
TryinHard
  • 4,078
  • 3
  • 28
  • 54
Felipe Calderon
  • 675
  • 2
  • 7
  • 17

2 Answers2

9

Please note that the origin (0,0) in Android screen is situated at top-left corner of the screen. Thus, when you add values to y axis the object go towards bottom and when subtract values the object goes upwards.

In Android devices:

 Origin
 |
 V
 *-------------------------------
 | ----> X axis                  |
 | |                             |
 | |                             |
 | V Y-axis                      |
 |                               |
 |                               |
 |                               |
 |                               |
 |                               |
 |                               |
 |                               |
 |                               |
 |                               |
 |                               |
 |                               |
 |                               |
 |                               |
 |                               |
 |                               |
 |                               |
  -------------------------------

Possible History:

As screen space calculations started when television sets were used as screens. The raster gun of a TV is also starting at the top left corner, so this was accepted as the origin.

For further reference you can refer here

Transformations:

However, you can use the transformation of coordinates in android using Canvas.setMatrix().

You can flip your Canvas with using

canvas.translate(0,canvas.getHeight());
canvas.scale(1, -1) 

to shift it to the correct location.

You can refer here for transformation.

Community
  • 1
  • 1
TryinHard
  • 4,078
  • 3
  • 28
  • 54
1

This is the common way, in which computer displays are addressed. It stems probably from that video memory is mapped to screen row-by-row from the top left corner, which in turn most likely comes from the fact that this way electron beam swept the screen in old CRT displays.

You can change the coordinate system to be more "mathematical" by using Canvas.setMatrix() with appropriate transforma matrix. In order to create a "mirroring" transform matrix, use -1 as a parameter to the scale operation (i.e. matrix.setScale(0, -1))

Jaromír Adamec
  • 579
  • 4
  • 13