Getting translated coordinates from Android canvas - java

I am passing in an object for Android to paint using it's canvas. The object to rotated to match the horizon. I am trying to find the translated points which canvas calculates given the point and angle I give it.
For example:
I use the following canvas methods to paint it. Where x,y are the screen coordinates and rotation is the rotation of the screen relative to the horizon.
canvas.save();
canvas.translate(x,y);
canvas.rotate(rotation);
obj.paint(canvas);
canvas.restore();
obj.paint() looks like this (but you probably don't need to know it):
canvas.save();
canvas.translate(-width/2, -height/2);
setFill(true);
setColor(backgroundColor);
paintRect(canvas, x, y, width, height);
setFill(false);
setColor(borderColor);
paintRect(canvas, x, y, width, height);
canvas.restore();
paintRect() looks like this:
canvas.drawRect(x, y, x + width, y + height, paint);
What I am trying to figure out is; what are the corner points of the rectangle (green in this picture) after it has been rotated.
Here is an image of the result which is what I expect.

Related

How to select what sprite or texture to render first?

I'm making a game using libgdx and I encountered a problem when rendering textures
batch.draw(background, x, y, width, height);
sprite.draw(batch); //of course the background will be rendered first
And when I do this:
sprite.draw(batch);
batch.draw(background, x, y, width, height); //the background will cover the sprite
My problem is that I use two cameras, on is static and the other is non-static. So I need a way on how to select which sprite or texture to be rendered first so that it wont be covered by other textures.

Rotated rect changes location

I am trying to rotate a rect and i have to rotate the canvas to do so but the rect changes its location for example, if the rect is in the middle of the screen after the rotation it will go to the side and i cannot figure out how to get it back to the middle.
Here is the code i used:
canvas.save();
canvas.rotate(45);
canvas.drawRect(width/2,height/2, width/2+200, height/2+200, paint);
canvas.restore();
Is there a way to rotate the rect without rotating the canvas so its location will stay the same?
Your rectangle's center differs from the pivot point of the canvas. You can try using the other rotate method to specify the x/y coordinates of the pivot point. As in:
int offset = 200;
int left = width/2;
int top = height/2;
int right = left + offset;
int bottom = top + offset;
canvas.save();
canvas.rotate(45, left + offset/2, top + offset/2);
canvas.drawRect(left, top, right, bottom, paint);
canvas.restore();
Edit:
Just tested the code above and verified that it works (given a few tweaks that I just added, above).
'canvas.rotate' rotates the canvas around the origin of canvas. Origin is top left corner of canvas at initial state.
If you want to rotate a rectangle around the center of the rectangle, move origin of canvas to the center of the rectancle before using 'canvas.rotate'.
Try following code:
canvas.save();
canvas.drawRect(width/2,height/2, width/2+200, height/2+200, paint); // drow rectangle
canvas.translate(width/2+100, height/2+100); // move origin of canvas to the center of the rectangle
canvas.rotate(45); // canvas is roteted around the origine that is the center of the rectangle also. As a result, rectangle is roteted around its center.
canvas.translate(-(width/2+100), -(height/2+100)); // restore origin of canvas at original position
canvas.restore();

Draw the rectangle and fill the rectangle based on (x,y) points

I was not able to figure out how to fill the drawn rectangle based on (x,y) points. Basically inside (x,y) points. There can be a array of (x,y) points.
Anyone can help on this.....
Rectangle r = new Rectange(length, width, x, y);
r.fill(Color.yourColor);
Graphics g = getGraphics();
g.draw(r);

Image not drawn to a fixed position when rotating with AffineTransform

I have a problem with rotating image in a fixed position with Graphcis2D and AffineTransform.
The idea is to rotate an image according to body's rotation.
The rotation is happening correctly as the rotation angle of the image matches the angle of the body's rotation. However, as the rotation takes place, the image is not drawn to the same position as the body should be drawn. The code of the method painting the picture is the following:
public void paintPicture(Graphics g, Body body) {
Graphics2D g2 = (Graphics2D) g;
Vector2f[] vertices = ((Box) body.getShape()).getPoints(body.getPosition(), body.getRotation());
Vector2f topLeftCorner = vertices[0];
AffineTransform oldTransform = g2.getTransform();
AffineTransform at = new AffineTransform();
at.rotate(body.getRotation());
g2.setTransform(at);
g2.drawImage(this.img, (int) topLeftCorner.x, (int) topLeftCorner.y, null);
g2.setTransform(oldTransform);
}
Any ideas what might cause the movement of the image instead of drawing it according to the coordinates (topLeftCorner.x, topLeftCorner.y)?
You need to first translate you object so the anchor point (the point around which you want it to rotate) is at the origin, perform your rotation, then translate it back. So if you want to rotate around the point (50, 75), you'd do the following:
at.translate (-50, -75);
at.rotate (body.getRotation());
at.translate (50, 75);
I'm assuming that your AffineTransform class can accumulate transformations. If not, you'll need 3 different transforms.

Rotating a Rectangle on an Image using Swing

Ok when a user draws a Rectangle on top of an image, I want to find all the rotated Rectangles ahead of time for all image rotation angles (90,180,270,360).
According to Java API I can just keep calling the Graphics2D rotate() method. I can then use this Graphics2D transformer to get the rotated Rectangle.
This works for the very first rotation(1.5708) call. I get the correct Rectangle Point. All other calls after that return the wrong Rectangle Point after using Transformer.
I think my problem is the Graphics2D translate(x,y). I don't understand how to use it.
Anyone knows how to fix my code so that it will return the correct Rectangle after every rotation?
Thank you.
public void rotateRectangles(BufferedImage bim,int width,int height,Rectangle rect){
BufferedImage bim = new BufferedImage(height, width,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) (bufferedImage.createGraphics());
g2d.translate(bufferedImage.getHeight(),0);
//Get Rectangle for 90 degree image rotation. This always good.
g2d.rotate(1.5708);
Shape shape = g2d.getTransform().createTransformedShape(rect);
Rectangle rotatedRect = shape.getBounds();
System.out.println("rotated rectangle at 90 degrees. Point x="+rotatedRect.x+" y="+rotatedRect.y);
//Get Rectangle for 180 degree image rotation. Getting wrong rotatedRect.
g2d.rotate(1.5708);
shape = g2d.getTransform().createTransformedShape(rect);
rotatedRect = shape.getBounds();
System.out.println("rotated rectangle at 180 degrees. Point x="+rotatedRect.x+" y="+rotatedRect.y);
//Get Rectangle for 270 degree image rotation. Getting wrong rotatedRect.
g2d.rotate(1.5708);
shape = g2d.getTransform().createTransformedShape(rect);
rotatedRect = shape.getBounds();
System.out.println("rotated rectangle at 270 degrees. Point x="+rotatedRect.x+" y="+rotatedRect.y);
//Get Rectangle for 360 degree image rotation.Getting wrong rotatedRect.
g2d.rotate(1.5708);
shape = g2d.getTransform().createTransformedShape(rect);
rotatedRect = shape.getBounds();
System.out.println("rotated rectangle at 360 degrees. Point x="+rotatedRect.x+" y="+rotatedRect.y);
}
Thank you.
Instead of rotating the graphics context's affine transform via g2d.rotate(), consider using createTransformedShape() as suggested in this example.
Addendum: Note in particular that the baseline of the example Polygon is initially centered on the origin. As a result, the initial transformation is rotation around the origin. In your case, you can use the rotate() method that includes an anchor point, which would be the center of your rectangle.

Categories