Switch coordinate space(x,y)

Good evening everyone!

Is there a way to change the standard java coordinate space
( in which (0/0) is situated in the top left corner with axes right = +x ,down = +y)
to something compatible to the default OpenGL one
((0/0) is situated in the bottom left corner; right = +x axe and up = +y axe)?

I think I need to apply an affine transformation on the coordinate space,
tried to figure this out via google, but the results weren’t satisfying… ???
If this would be possible, it could save me a lot of work converting some JOGL-stuff to Java2D.

Have a nice day & thx for reading,

lebasti

You can translate Graphics object using translate method to desired position.

Graphics g=getGraphics();
g.translate(xAxe,yAxe);

Hope this helps.

You have to flip the y-axis, then translate along the y-axis.

Thanks to both of you for answering!

Yes, the mathematical approach is clear to me, but I wonder if it would be possible do generally change the used coordinate space
in a panel, so that I can more or less copy all my OpenGL-calculations to Java2D. I hope i could make my intention a little more understandable :slight_smile:

greetings

lebasti

I think you did. The advice applies nonetheless. Flip and translate your Graphics/Graphics2D object.

Why don’t you do the reverse. You can easily set up OpenGL to use any coordinate system. Much easier than Java2D.

[quote]Why don’t you do the reverse
[/quote]
The whole OpenGL-stuff is already finished, would take the same effort to change this one to a new coordinate system as it would take on Java2D (even though it’s pretty easy to switch the space in GL)

I tried to do the whole affine-transformations on he graphics-object, but it seems to slow down the whole thing…

Thanks for your help guys,

greetings

basti

AffineTransform shouldn’t slow you down. Here is what you would use:


Graphics2D g = //get it somewhere
g.translate(0, 600); //for 800x600
g.scale(1.0, -1.0); //flip around the x-axis so +y is up
//all drawing goes after above transforms

Wrong order :slight_smile:


Graphics2D g = //get it somewhere
g.scale(1.0, -1.0); //flip around the x-axis so +y is up
g.translate(0, -600); //for 800x600
//all drawing goes after above transforms

or


Graphics2D g = //get it somewhere
g.translate(0, -600); //for 800x600
g.scale(1.0, -1.0); //flip around the x-axis so +y is up
//all drawing goes after above transforms

Not the wrong order. I tested it before I posted it.

Your first method will work, but your second method will move 0,0 off the top of the screen. Until you flip the axis, +y is still down.

Will I ever learn… :wink:

Do any of us… ;D

Hello again…

thanks for the help and keeping this thread alive… :slight_smile:
I followed your suggestions and updated my code as follows :


                    Graphics2D g2d = (Graphics2D) g; //cast to Graphics2D
		img = image2d.getImage(); //fetch the BufferedImage from the Imaged2D-class
		w_displayed = (int) (img.getWidth() * zoom); //calculate the width of the image 
		h_displayed = (int) (img.getHeight() * zoom);//caluclate the height of the image
		panelHeight = this.getHeight(); //get the height of the panel
		panelWidth = this.getWidth(); //and the width
		g2d.translate(0,panelHeight); // for the display-height
		g2d.scale(1.0,-1.0); //flip around the x-axis so +y is up
		g2d.translate(transformX, transformY); //translate to the current position(default 0/0)
		g2d.drawImage(img, 0, 0, w_displayed, h_displayed, null); //draw the resized image

The image is displayed with the wrong orientation (180°) and is pretty heavy on cpu if redrawn/resized.
This operations where working fine when doing this stuff without flipping the axis.
Anyone of you got an idea, if there’s another mistake in my code?

greetings

basti

Well, your images are not rotated by 180 deg, just flipped along the y-axis, which is not the same.

Not much you can do about that… you could load your images in 1 special method, that also flips them.

Hmm, you’re right… it is obviously flipped, thx.
I decided to let the (0/0)-point (how is this correctly called in english btw. ?) stay in the top-left corner and change the rest of the
stuff manually.

Thanks for all your help,

have a nice day

basti

The term for the point at 0,0 is the ‘origin’ of the coordinate space.

Oh, I’m pretty stupid… thank you!

bye

basti

Hi,

I need to turn the java2D coordinate system into the proper cartesian system with the origin in the bottom left corner. I used the suggested transform and it works.

Problem: scale(1, -1) causes FPS to go from 40 -> 1 (loses acceleration for some reason).

My game uses Java2D to draw & a BufferStrategy for buffering, and texture images are stored as VolatileImages and painted on to the BufferStrategy’s buffer. Normal scaling by say scale(0.75, 0.75) works fine but scale(1, -1) must cause Volatilemage -> BufferStrategy acceleration to fail.

As a work-around I pre-loaded my VolatileImage textures up-side down and then without doing any scaling I translated them such that they appeared where they should in the new coordinate system. This means that I have to use the scaling transform for graphics.drawLines(…) but the work-around transform for graphics.drawImage(). But this doesn’t work either because when I go to paint the textures onto the BufferStrategy I use a clipping shape that is one of my game objects and to get the right clip without the scaling transform I need to manually flip & translate the actual shape… Grrrrrrr.

I was trying to make things easier by using a more familiar coordinate system but with all of these work-arounds its defeating the purpose. So two questions:

[b]1. What is the simple answer?

  1. Why aren’t negative scaling transforms accelerated for VolatileImages on my windows XP pc whether I use Java2D or its openGL pipeline?[/b]Thanks heaps,
    Keith