don't click here

Dealing with the XY origin coordinate issue when coding (in high level lang)

Discussion in 'Technical Discussion' started by 0xfadec0de, Jan 4, 2020.

  1. 0xfadec0de

    0xfadec0de

    Member
    7
    1
    3
    I have tried to coding a Mario 3 clone (and would love to do a Sonic game) in Java, or maybe C++. However, using Java's AWT & Javax.swing, the coordinate is at the top-left of the viewport. That blocked the entire project, as I could not think of a way to make it so the origin is at Cartesian 0,0. My attempt to handle it resulted in problems w/ sprite locations given that some sprites are different heights.

    What tactics could people suggest as a workaround or alternative (provided it's free)?
     
  2. Billy

    Billy

    RIP Oderus Urungus Member
    2,119
    179
    43
    Colorado, USA
    Indie games
    You could always write methods that convert to/from the two coordinate systems. Or, because it sounds like you're not doing so, drop in a library focused around writing games.
     
  3. 0xfadec0de

    0xfadec0de

    Member
    7
    1
    3
    I tried to think of a method that would do that; I couldn't think of one bc it would have to alter AWT/Javax's functionality (or so I thought). Is there a good library you might recommend?
     
  4. MarkeyJester

    MarkeyJester

    Original, No substitute Resident Jester
    2,202
    432
    63
    Japan
    While in mathematics, it is customary for the ordinate (Y axis) to be positive up and negative down, TV monitors using cathode ray tubes tend to be rendered in reverse of this principle whereby the ordinate is positive down and negative up. This might have been influenced by the way us as humans read text which mostly (if not always) is from top to bottom. While some monitors might have rendered in reverse, or even in some cases the ordinate and abscissa (X axis) are swapped where the rendering is left to right (I believe some arcade machines have used this method), it's mostly the top to bottom method used.

    Because of this, some of the earliest software for rendering direct to the display needs careful timing and the calculations need to be fast. Rather than having to negate or recalculate the ordinate position in real-time, it just made sense to have everything calculated in parallel to the screen, and have the data constructed in the same ordered direction. After video hardware took over the display role from the CPU, the principles of top to bottom were kept.

    One exception might be IBM where according to this:

    So they wanted to keep the classical mathematical representation and have everything render from bottom to top, but top to bottom was still in use by the majority. This is of course until 3D rendering became prevalent, and the ordinate axis became positive up again, but since the 3D environment needs to be recalculated into a 2D display, and because the ordinate axis can render in any direction (not just up now), it really didn't matter that it conflicted with the rendering of the display.

    With that in mind, I would like to suggest using 3D rendering, and render quads (or two triangles) for your display/sprites, up is always positive in a 3D environment, though, you may have to work with floating point as opposed to a fixed point integer, but it sounds to me like the polarity of the ordinate axis is likely more of a problem for you than working with floating point.
     
  5. 0xfadec0de

    0xfadec0de

    Member
    7
    1
    3
    Is there any practicable way to do it with Javax/AWT, or is any attempt to make a Mario-like game doomed with it? I see your point about 3D, but I would like to keep it simple as possible. I have each Mario sprite as .bmp file with a pinkish background; each sprite is the whole thing, not the group of 8x8 pixel tiles from NES.
     
  6. Cooljerk

    Cooljerk

    NotEqual Tech, Inc - VR & Game Dev Oldbie
    4,505
    201
    43
    Worth noting, that while speaking about vertex coordinates, up is always positive in a 3D space, it goes wonky again when talking about UV coordinates, which can be just as useful. Example, if you're trying to mimic, say, retro console scrolling by rendering a larger screen space onto a quad (or two triangles) in a framebuffer first, then using UV coordinates to paint the "scrolling" screen onto a screen buffer, you'll have to deal with up-down issues all over again. OpenGL UV coordinates, Y begins at the bottom, where in DirectX, Y begins at the top.

    All that said, working with 3D vertices will teach you how to translate between coordinate systems, as that's how 3D rendering in general works. Rotations always happen around the origin, so to not-rotate around the origin, you have to shift your image first with a transformation. You could use the same logic in a 2D system. The way you'd do it is to keep track of your screen or object or whatever's width and height, then move over by half of those values to plop your origin right in the center.