Hello Guest

Graphics2D shaking when moving rotated rectangle

  • 7 Replies
  • 9179 Views
*

Offline Daslee

  • ***
  • 126
Graphics2D shaking when moving rotated rectangle
« on: February 11, 2013, 13:55:57 »
Hello. I just noticed one problem in my game that all graphics are shaking when moving rotated rectangle. Before that I had one rotated rectangle, but I do not moving it, so I didn't seen that problem. Now with player, when I rotate him and try to move it, so all graphics are shaking, but why? Here is video about it: http://www.youtube.com/watch?v=8Xtp5UqylXE

And this is how I do rotation:
Code: [Select]
rotate(playerBounds.getRotationDegrees(), playerBounds.getCenterX(), playerBounds.getCenterY());
drawRect(playerBounds, 0);
resetRotate();

And those methods:
Code: [Select]
public void rotate(double rotation, float x, float y){
double radians = rotation / 57.2957795;
graphics.rotate(radians, x, y);
lastRotation = radians;
lastRotationPoint.setLocation(x, y);
}
public void resetRotate(){
graphics.rotate(-lastRotation, lastRotationPoint.getX(), lastRotationPoint.getY());
}

public void drawRect(DRectangle r, int round){
if(readyRender){
if(round != 0) graphics.fillRoundRect((int)r.getX(), (int)r.getY(), (int)r.getWidth(), (int)r.getHeight(), round, round);
else graphics.fillRect((int)r.getX(), (int)r.getY(), (int)r.getWidth(), (int)r.getHeight());
}
}

lastRotation is double and lastRotationPoint is point

Re: Graphics2D shaking when moving rotated rectangle
« Reply #1 on: February 11, 2013, 14:06:10 »
Your problem looks like it's caused by rounding errors. The problem is that when you rotate the rectangle, you then rotate again by the opposite rotation to reset the rotation. This results in rounding errors. The best way to do what you are wanting is to use OpenGl's pushMatrix() and popMatrix() or use your own stack of matrices.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

*

Offline Daslee

  • ***
  • 126
Re: Graphics2D shaking when moving rotated rectangle
« Reply #2 on: February 11, 2013, 18:23:42 »
The problem is that when you rotate the rectangle, you then rotate again by the opposite rotation to reset the rotation.

So what would be another way to reset rotation? If I try to set rotation to 0, so it just rotates graphics by 0 and nothing.. If I use graphics.dispose(); then there will no be shaking, but only that rectangle shakes.

And I do not want to use something from opengl, because it's too hard to copy something from opengl for me.

Re: Graphics2D shaking when moving rotated rectangle
« Reply #3 on: February 11, 2013, 20:39:14 »
...you're using Graphics2D? Why?

*

Offline Daslee

  • ***
  • 126
Re: Graphics2D shaking when moving rotated rectangle
« Reply #4 on: February 11, 2013, 20:50:42 »
...you're using Graphics2D? Why?

And... why I can't use it?

Re: Graphics2D shaking when moving rotated rectangle
« Reply #5 on: February 12, 2013, 13:46:23 »
...you're using Graphics2D? Why?

And... why I can't use it?

I didn't notice you were using Graphics2D. I thought you meant you were doing two-dimensional graphics. :P

The reason not to use Graphics2D is because OpenGL is hundreds of times faster. ;D
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Re: Graphics2D shaking when moving rotated rectangle
« Reply #6 on: February 12, 2013, 14:34:22 »
More than hundreds, even. It also allows you to delegate rendering computations - which are very expensive - off of the CPU (which isn't really designed for it) and onto the GPU (which is). Not only does this improve your performance, but you're not wasting a ton of CPU memory with rendering buffers/textures, and you have many more options with how to perform your rendering, since Java2D is extremely limited.

Trust me on this - I started making a game engine based on Java2D before I found LWJGL, and the performance difference is AMAZING.

Oh, and I found it odd you were asking about Java2D on a OpenGL-centric forum. There really isn't much of an overlap.

*

Offline basil

  • **
  • 81
Re: Graphics2D shaking when moving rotated rectangle
« Reply #7 on: February 14, 2013, 23:34:24 »
yes, its the rounding. fillRect etc takes Integers. you can achieve good results when you use a matrix (transformation) to position and scale or one of the Shape implementations. you get floats and doubles there. Strangly there is no float-polygon implementation, just a double version. so integer or double! wth.

J2D can use D3D or opengl in the backend and direct-rendering (BufferStrategy) is very very fast... if you draw basic things. all good.

what i really dont get when you compare J2D to OGL or to any other proper graphics lib. is the not existing additive color-blending! that's my biggest issue with it.

on the other hand, it's good when your stuff can run on different rendering systems without big hassle.