that wacky y-axis

912 views
Skip to first unread message

asham...@yahoo.com

unread,
Oct 10, 2008, 12:58:30 AM10/10/08
to pyglet-users
You probably get this a lot, but: how DO you invert the y-axis, so
that it works like every other screen before or since?

Paul Hoskinson

unread,
Oct 10, 2008, 1:53:45 AM10/10/08
to pyglet...@googlegroups.com
Say you have a point (x, y) with the origin in the top left as you
prefer. When you do a drawing command, substitute y with
(window.height - y).

asham...@yahoo.com

unread,
Oct 10, 2008, 2:04:53 AM10/10/08
to pyglet-users
That would have to be (window.height - image.y - image.height) to work
properly for drawing, but it's no help. I'm using batched sprites for
speed, and they're drawn automatically by the 'black box'.

There must be simple gl command I can put somewhere to flip the co-
ordinates, right? I can hardly be the first person to run up against
this...

Nathan Whitehead

unread,
Oct 10, 2008, 2:30:10 AM10/10/08
to pyglet...@googlegroups.com
On Thu, Oct 9, 2008 at 11:04 PM, asham...@yahoo.com
<asham...@yahoo.com> wrote:
> ...

> There must be simple gl command I can put somewhere to flip the co-
> ordinates, right? I can hardly be the first person to run up against
> this...

Here's what I do:

# Set default projection to use upper left as origin
pyglet.gl.glMatrixMode(pyglet.gl.GL_PROJECTION)
pyglet.gl.glLoadIdentity()
pyglet.gl.glOrtho(0.0, 1024.0, 768.0, 0.0, -1.0, 1.0)
pyglet.gl.glMatrixMode(pyglet.gl.GL_MODELVIEW)
pyglet.gl.glLoadIdentity()

This makes 0,0 the upper left corner of the screen and 1024, 768 the
bottom right corner. But if you're thinking that this is what you
want, you should consider what will happen! If you use any function
that is not expecting this, it will draw upside down. For example,
try drawing some text using:

label = pyglet.text.Label('Hello, world',
font_name='Times New Roman',
font_size=36,
x=window.width//2, y=window.height//2,
anchor_x='center', anchor_y='center')
label.draw()

It will come out beautifully rendered in the right location, but
reflected upside down.
--
Nathan Whitehead

asham...@yahoo.com

unread,
Oct 10, 2008, 3:25:39 AM10/10/08
to pyglet-users
Very amusing.

There's no simple cure? That's slightly mind-boggling. Every tool has
its quirks, but I've not seen before the kind of balls it takes to
blandly ignore a near-universal standard like this.

I suppose I can carry on what I'm doing, which is flipping every
sprite's y co-ordinate before drawing it and putting it back after,
but that's like a joke.

On Oct 10, 5:30 pm, "Nathan Whitehead" <nwhit...@gmail.com> wrote:
> On Thu, Oct 9, 2008 at 11:04 PM, ashaman...@yahoo.com

Luke Paireepinart

unread,
Oct 10, 2008, 3:36:29 AM10/10/08
to pyglet...@googlegroups.com
On Fri, Oct 10, 2008 at 7:25 AM, asham...@yahoo.com
<asham...@yahoo.com> wrote:
>
> Very amusing.
>
> There's no simple cure? That's slightly mind-boggling. Every tool has
> its quirks, but I've not seen before the kind of balls it takes to
> blandly ignore a near-universal standard like this.
>
> I suppose I can carry on what I'm doing, which is flipping every
> sprite's y co-ordinate before drawing it and putting it back after,
> but that's like a joke.

I think the pyglet way of doing it is more intuitive. The other way
makes little sense. And this is coming from a pygame user of at least
4 years, who hasn't begun to use pyglet yet. It's just more intuitive
that way for me.

Whenever I animate a ball being shot or something, it feels unnatural
to have the image offset from the top, rather than from the direction
of gravity like I would expect it to be.

Imagine that the ground is the x-axis, and you're throwing a ball.
Would you choose an arbitrary line above you (say 15 feet) and state
that the ball is 5 feet from that line after .5 seconds, or would you
say the ball is 10 feet off the ground after .5 seconds? It just
makes more sense to me that way.
Do you have a reason that you prefer it the other way? Convention is
not the mother of invention, remember. Python ignored the
pretty-common convention of denoting blocks with either braces or a
start / end statement, and just used indentation solely. I personally
believe this to be one of the greatest features. It's a minor issue,
but it makes the code more readable (enforces decent code structure)
and gets rid of all those nasty braces that you just feel are a waste
of lines and are a trouble to keep up with. And this sort of
epitomizes Python tradition - do things the way that make the most
sense, not the way people have been doing them before.

At least that's my two cents. But pennies are pretty worthless (worth
even less now that the DOW is below 9k and our economy is tankin', but
that's neither here nor there.) so do with it what you will.

Nathan Whitehead

unread,
Oct 10, 2008, 3:53:18 AM10/10/08
to pyglet...@googlegroups.com
On Fri, Oct 10, 2008 at 12:36 AM, Luke Paireepinart
<rabidp...@gmail.com> wrote:
> I think the pyglet way of doing it is more intuitive. The other way
> makes little sense. And this is coming from a pygame user of at least
> 4 years, who hasn't begun to use pyglet yet. It's just more intuitive
> that way for me.

I agree that origin in the lower left is more intuitive. That's how
you learn it in math class! When you start doing any equations things
are much simpler if you don't have to convert from what you learned in
trig class to funky reversed y-axis coordinates.

But I also appreciate the origin in the upper left. That has been a
2D game programming standard a long time, as well as a standard for
bitmap images. When I use the Gimp to get rectangle coordinates from
an image, that's how it gives me coordinates.
--
Nathan Whitehead

Tartley

unread,
Oct 10, 2008, 4:53:43 AM10/10/08
to pyglet-users
> ignore a near-universal standard

That's not really true. Presumably pyglet's axis orientation is based
on OpenGL, upon which it is built, and in which increasing Y almost
always corresponds to up the screen.

DirectX is just the same.


On Oct 10, 8:25 am, "ashaman...@yahoo.com" <ashaman...@yahoo.com>
wrote:

Drew Smathers

unread,
Oct 10, 2008, 10:54:59 AM10/10/08
to pyglet...@googlegroups.com
On Fri, Oct 10, 2008 at 3:25 AM, asham...@yahoo.com
<asham...@yahoo.com> wrote:
>
> Very amusing.
>

Why? That's the way you do it.

> There's no simple cure? That's slightly mind-boggling. Every tool has
> its quirks, but I've not seen before the kind of balls it takes to
> blandly ignore a near-universal standard like this.
>

Pyglet didn't invent OpenGL. It's just provides an easy path to use
it directly if you need to.

> I suppose I can carry on what I'm doing, which is flipping every
> sprite's y co-ordinate before drawing it and putting it back after,
> but that's like a joke.
>

Abstract away the code Nathan gave you in a function called
flip_screen(). Or just use the more natural coordinate system
provided. Your explanation above ("they're drawn automatically by
the 'black box'") doesn't make any sense. I use batches as well, and
they don't pose any technical obstacle with regard to window origin.
Many other developers (obviously) have used sprites and batches
without a need to flip the screen. Maybe you could elaborate more on
the technical nature of your problem?

Drew

Matthew Marshall

unread,
Oct 10, 2008, 10:57:31 AM10/10/08
to pyglet...@googlegroups.com
Tartley wrote:
>> ignore a near-universal standard
>
> That's not really true. Presumably pyglet's axis orientation is based
> on OpenGL, upon which it is built, and in which increasing Y almost
> always corresponds to up the screen.
>
> DirectX is just the same.

And let's not forget mathematics. In my high school math classes most
(all?) plots using the rectangular coordinate system were drawn with
positive Y pointing up.

The origin in the top left is a holdout from the days when display
controllers were wired to print text from top to bottom.

MWM

asham...@yahoo.com

unread,
Oct 10, 2008, 12:38:16 PM10/10/08
to pyglet-users
> Abstract away the code Nathan gave you in a function called
> flip_screen().

Nathan's code is useless (but amusing) because it turns everything
upside down. Images, text, the works. Not very helpful.

>  Or just use the more natural coordinate system
> provided.  Your explanation  above ("they're drawn automatically by
> the 'black box'") doesn't make any sense.  I use batches as well, and
> they don't pose any technical obstacle with regard to window origin.
> Many other developers (obviously) have used sprites and batches
> without a need to flip the screen.  Maybe you could elaborate more on
> the technical nature of your problem?
>
> Drew

Of course itsn't a technical problem, you silly bugger. It's a user
interface problem. Imagine a brand new os, full of useful features --
except every time you push the mouse _up_, the cursor goes _down_.
When you point out this out its users, they act shocked that anyone
wold want it any other way: it's the rest of the world that's wrong!

On one hand you have all word processors, all spreadsheets, all paint
programs, all map-making tools, all game-making libraries.

On the other hand, you have... Pyglet.

I am not reprogramming my brain to just to use this thing.

Kao Cardoso Felix

unread,
Oct 10, 2008, 12:47:44 PM10/10/08
to pyglet...@googlegroups.com
On Fri, Oct 10, 2008 at 2:38 PM, asham...@yahoo.com
<asham...@yahoo.com> wrote:
> Of course itsn't a technical problem, you silly bugger. It's a user
> interface problem. Imagine a brand new os, full of useful features --
> except every time you push the mouse _up_, the cursor goes _down_.

Wow. This is SO not the case in here.

> When you point out this out its users, they act shocked that anyone
> wold want it any other way: it's the rest of the world that's wrong!

Most people in this list that responded to this thread seem to find
the Y axis up convention more intuitive...

> On one hand you have all word processors, all spreadsheets, all paint
> programs, all map-making tools, all game-making libraries.
>
> On the other hand, you have... Pyglet.

And OpenGL and DirectX and High School Math...

> I am not reprogramming my brain to just to use this thing.

You sound like it's a humongous effort to switch the way you work
about coordinates in 2D. For me it's simply a matter of knowing wich
convention each library uses and stick to it. I know some people
understand some things faster and better than others, but I just can't
believe it's that hard to think with the Y axis up once you know it's
up. I hope you never get introduced to 3D programming and have to deal
with right-hand vs. left-hand conventions that are far more confusing
than that...

--
Kao Cardoso Félix

Página pessoal: http://www.inf.ufrgs.br/~kcfelix
Blog: http://kaofelix.blogspot.com

asham...@yahoo.com

unread,
Oct 10, 2008, 1:05:39 PM10/10/08
to pyglet-users
> > When you point out this out its users, they act shocked that anyone
> > wold want it any other way: it's the rest of the world that's wrong!
>
> Most people in this list that responded to this thread seem to find
> the Y axis up convention more intuitive...

People on Pyglet forum express preference for Pyglet way of doing
things. Shocking.

> > On one hand you have all word processors, all spreadsheets, all paint
> > programs, all map-making tools, all game-making libraries.
>
> > On the other hand, you have... Pyglet.
>
> And OpenGL and DirectX and High School Math...

Don't tie yourself up trying to justify why one arbitrary co-ordinate
system is 'better' than another arbitrary one. Pyglet's way is an
extreme minority. No doubt the developes knew that when they chose it,
for what must have seemed compelling reasons at the time.

> > I am not reprogramming my brain to just to use this thing.
>
> You sound like it's a humongous effort to switch the way you work
> about coordinates in 2D. For me it's simply a matter of knowing wich
> convention each library uses and stick to it. I know some people
> understand some things faster and better than others, but I just can't
> believe it's that hard to think with the Y axis up once you know it's
> up. I hope you never get introduced to 3D programming and have to deal
> with right-hand vs. left-hand conventions that are far more confusing
> than that...

You, know I was really hoping for more helpfulness and less iq-penis-
measuring. But, well, internet...

Thomas Woelz

unread,
Oct 10, 2008, 1:15:27 PM10/10/08
to pyglet-users
Internet troll DETECTED.

Abort?

[x] Yes
[ ] No

asham...@yahoo.com

unread,
Oct 10, 2008, 1:29:53 PM10/10/08
to pyglet-users
For fuck's sake. I was already thinking: 'this thread rotted away
three posts ago, there's realy no reason to click on it again, it can
only raise your blood pressure'...

But no. And some guy's calling me a troll. Because I said I thought
that choice of cartesian co-ordinate system used by a niche media
library was poor.

Christ on a pogo stick. That must be the most niche kind of troll
ever.

This is most surreally overdefensive thing I've ever seen. This thread
needs only two kinds of posts:

A) "Yes, it's possible to change how co-ordinates work by doing
[this]."

or

B) "Sorry, it's not possible to change how co-odinates work."

If you can't provide signal, at least try not bring noise (or flames).

Luke Paireepinart

unread,
Oct 10, 2008, 1:48:40 PM10/10/08
to pyglet...@googlegroups.com
Why is there so much douchery going on here?
Pyglet is an OpenGL library. OpenGL uses a y=up coordinate system.
That should be all that needs to be said.
Stop being dumb.
You're just frustrating yourself.

dio...@gmail.com

unread,
Oct 10, 2008, 1:50:29 PM10/10/08
to pyglet-users
> But no. And some guy's calling me a troll. Because I said I thought
> that choice of cartesian co-ordinate system used by a niche media
> library was poor.

Wikipedia on Cartesian coordinates: http://en.wikipedia.org/wiki/Cartesian_coordinate_system

Notice how on the graph, they have up as the positive Y direction.

OpenGL is _the_ standard for graphics programming on OS X and Linux.
DirectX is _the_ standard for programming on Windows, unless you want
to us OpenGL there too. They both use the bottom left corner of the
screen as the origin.

Any other mainstream graphics library with a different coordinate
system, including SDL (which pygame is built on), with the possible
exception of QuickDraw or a legacy Windows API I've never heard of,
uses OpenGL or DirectX to render, which means they are doing a behind-
the-scenes coordinate conversion.

You have two options:
1. Write a function to convert. Something like (in shorthand):
convert_y_func = lambda y: main_window.height - y
call it like this: pogostick.y =
convert_y_func(desired_upperleft_based_y)
That won't invert anything but the coordinate you pass it.
2. Use the industry standard.

Luke Paireepinart

unread,
Oct 10, 2008, 1:53:35 PM10/10/08
to pyglet...@googlegroups.com
On Fri, Oct 10, 2008 at 11:38 AM, asham...@yahoo.com
<asham...@yahoo.com> wrote:
>
>> Abstract away the code Nathan gave you in a function called
>> flip_screen().
>
> Nathan's code is useless (but amusing) because it turns everything
> upside down. Images, text, the works. Not very helpful.

So do something creative instead of bitching and whining when we don't
give you a direct solution.
For example, store your values in a class, and use a property to
automatically convert your y value from a top-measured to a
bottom-measured value whenever you retrieve it. Or use a conversion
function for the coordinate as noted by dordna.
This isn't rocket science, seriously.


> I am not reprogramming my brain to just to use this thing.

So reprogram the computer to work how you expect it to. Core tenet of
Computer Science, friend.

asham...@yahoo.com

unread,
Oct 10, 2008, 2:01:19 PM10/10/08
to pyglet-users
> So reprogram the computer to work how you expect it to.  Core tenet of
> Computer Science, friend.

Good advice. :) As mentioned in an earlier post, I already have a very
in-elegant method for doing that; I started the thread in hopes of
learning a neater or more 'official' way. If no such thing exist, I'll
just solider on.

Thank you, Luke, Diordna, for your advice.
Reply all
Reply to author
Forward
0 new messages