user3011902
user3011902

Reputation:

Java - Rotate Rectangle2D and set Length?

I am attempting to rotate a Rectangle around a Point and set its length dynamically. This is what I have so far:

Rectangle2D myRect = new Rectangle2D.Double(point.x - GameValues.ROPE_WIDTH, point.y, point.x + GameValues.ROPE_WIDTH, point.y + ropeLength);
    AffineTransform at = AffineTransform.getRotateInstance(
            Math.toRadians(rotation - 180), point.x, point.y);
    rope = at.createTransformedShape(myRect);

When I draw the shape, it doesn't behave as expected, the rectangles width changes as i move the Point around. How do I do this properly?

Upvotes: 1

Views: 431

Answers (1)

Michael Macha
Michael Macha

Reputation: 1781

You're misinterpreting how Rectangle2Ds are defined. For a Rectangle2D, I would suggest setting a length that is not related to point (directly), and then using affine transforms to scale it.

The issue is that Rectangle2D isn't defined between (x1, y1) and (x2, y2) like you think it is; it's (x1, y1) and (Δx, Δy), or the offsets.

alter your code to use this:

Rectangle2D myRect = new Rectangle2D.Double(point.x, point.y, GameValues.ROPE_WIDTH, ropeLength);

or some permutation of it, and you should be in the clear.

Upvotes: 1

Related Questions