Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (624) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Affine Transformation Coordinate System Explain  (Read 1533 times) 0 Members and 1 Guest are viewing this topic.
8bitooze

Senior Newbie

 « Posted 2011-02-05 18:14:39 »

I am using Affine Transformation to position my game objects on screen. My applet is 320 * 240, yet the leftmost edge is -80 for one of the objects so far and the rightmost being 240. The object in question is a 32 * 8 rectangle shape. To me it seems my coordinates are "crooked". Could someone please explain this to me? Any assistance in this matter would be greatly appreciated.

Sincerely,

8BitOoze

[size=3]8BitOoze[/size]
krasse
 « Reply #1 - Posted 2011-02-06 11:36:02 »

Is it 2D or 3D?

How do you create the transformation? Directly with the matrix or using a helper function like translate(), rotate(), lookAt() etc. ?

It is possible to create a shew transformation with with an affine transform, if that is what you mean by "crooked".

Show some of your code!

8bitooze

Senior Newbie

 « Reply #2 - Posted 2011-02-07 14:32:19 »

Quote
Is it 2D or 3D?
2D

Quote
How do you create the transformation? Directly with the matrix or using a helper function like translate(), rotate(), lookAt() etc. ?
Using g2d.translate(object.getX(), object.getY());

Quote
It is possible to create a shew transformation with with an affine transform, if that is what you mean by "crooked".

Show some of your code!
Ok, here's a link to the latest version of my code with the shrewed affine transformation link

Any assistence in this matter would be greatly appreciated.

Sincerely,

8BitOoze

[size=3]8BitOoze[/size]
 Games published by our own members! Check 'em out!
krasse
 « Reply #3 - Posted 2011-02-07 17:01:22 »

The problem is that you both translate the shape in the constructor for Ball and Paddle AND in the drawing.

This is the explanation for the extra offset. Just set the translation in the constructors to zero and it will work.

8bitooze

Senior Newbie

 « Reply #4 - Posted 2011-02-07 20:01:18 »

I don't understand what you mean.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14 `// class constructor   public Paddle(double x, double y, double width, double height)   {      // set the paddles x position      setX(x);      // set the paddles y position      setY(y);      // set the paddles width      setWidth(width);      // set the paddles height      setHeight(height);      // set the paddles shape      setShape(new Rectangle((int)getX(), (int)getY(), (int)getWidth(), (int)getHeight()));   }`

Ball Constructor:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14 `// class constructor   public Ball(double x, double y, double width, double height)   {      // set the balls x position      setX(x);      // set the balls y position      setY(y);      // set the balls width      setWidth(width);      // set the balls height      setHeight(height);      // set the balls shape      setShape(new Rectangle((int)getX(), (int)getY(), (int)getWidth(), (int)getHeight()));   }`

I don't see any g2d.transform() in there anywhere. If you mean changing my EightBitOoze constructor as follows:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24 `public final void init()   {      // create the backbuffer for smooth graphics      backbuffer = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_INT_RGB);      g2d = backbuffer.createGraphics();      // create the identity transform      identity = new AffineTransform();      // create an instance of the paddle object      paddle = new Paddle(getSize().width / 4, 106, 32, 8);      // set the paddle's x velocity      paddle.setVX(2);      // create an instance of the ball object      ball = new Ball(getSize().width / 4, 10, 8, 8);      // set the players score to 0      score = 1234;      // start the user input listener      addKeyListener(this);   }`

And changing the update method to:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26 `public final void update(Graphics g)   {      // erase the background      g2d.setPaint(Color.LIGHT_GRAY);      g2d.fillRect(0, 0, getSize().width, getSize().height);      // draw the statusbar      g2d.setPaint(Color.GREEN);      g2d.fillRect(0, getSize().height - STATUSBARHEIGHT, getSize().width, STATUSBARHEIGHT);      // draw the players score      g2d.setPaint(Color.WHITE);      g2d.drawString("Score: " + Integer.toString(score), 0, (getSize().height - STATUSBARHEIGHT) + (STATUSBARHEIGHT /2));      // draw the ball      g2d.setPaint(Color.BLUE);      g2d.translate(ball.getX(), ball.getY());      g2d.fill(ball.getShape());      // draw the paddle      g2d.translate(paddle.getX(), paddle.getY());      g2d.fill(paddle.getShape());      // repaint the applet window      paint(g);   }`

Then please let me know as I'm not sure what you mean when you say I'm translating on the ball/paddle constructor when I can't see a single translate call in them. Any assistance in this matter would be greatly appreciated.

Sincerely,

8BitOoze

[size=3]8BitOoze[/size]
krasse
 « Reply #5 - Posted 2011-02-07 21:38:03 »

The shape is translated with this statement:
 1  2  3  4 `setShape(new Rectangle((int)getX(), (int)getY(), (int)getWidth(), (int)getHeight()));// Should be this instead:// setShape(new Rectangle(0, 0, (int)getWidth(), (int)getHeight()));`

In the animation you draw the same shape translated once more but then you draw the shape that is positioned at the initial (getX(), getY()) position and you get an offset.

8bitooze

Senior Newbie

 « Reply #6 - Posted 2011-02-08 16:15:10 »

The shape is translated with this statement:
 1  2  3  4 `setShape(new Rectangle((int)getX(), (int)getY(), (int)getWidth(), (int)getHeight()));// Should be this instead:// setShape(new Rectangle(0, 0, (int)getWidth(), (int)getHeight()));`

In the animation you draw the same shape translated once more but then you draw the shape that is positioned at the initial (getX(), getY()) position and you get an offset.

Wow i didn't know that thanks. I did what you said and it worked.

Sincerely,

8BitOoze

[size=3]8BitOoze[/size]
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 ClaasJG (26 views) 2015-04-27 13:36:51 BurntPizza (34 views) 2015-04-23 03:42:11 theagentd (37 views) 2015-04-22 16:23:07 Riven (51 views) 2015-04-16 10:48:47 Duke0200 (60 views) 2015-04-16 01:59:01 Fairy Tailz (43 views) 2015-04-14 20:13:12 Riven (47 views) 2015-04-12 21:36:37 bus hotdog (65 views) 2015-04-10 02:39:32 CopyableCougar4 (67 views) 2015-04-10 00:51:04 BurntPizza (72 views) 2015-04-06 22:06:58
 theagentd 23x BurntPizza 17x wessles 15x Spasi 12x alwex 11x 65K 11x kingroka123 11x kevglass 7x Olo 7x Ecumene 7x ra4king 7x Rayvolution 7x Hanksha 7x Riven 7x chrislo27 7x KevinWorkman 6x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org