I have this code to generate a red rectangle inside a grey rectangle:

new Rectangle(grey_rectangle_position_x, Game.SCREEN_HEIGHT/2-Rectangle.height/2,0);

This code makes the following:

current behavior

Now, I want to rotate the red rectangle and randomize his X coordinate but keeping it inside the grey rectangle.

This is my actual code of the rectangle:

public class Rectangle {

    public static int width=30;
    public static int height=60;
    public static Color color=Color.RED;

    public Rectangle(int x, int y, int angle) {
        super(x, y,angle);

    public void render(ShapeRenderer shapeRenderer) {
        shapeRenderer.translate(pos_x+width/2, pos_y, 0.f);
        shapeRenderer.rotate(0.f, 0.f, 1.f, angle);
        shapeRenderer.rect(-width/2, -height/2, width, height);

And this is what I should do to randomize the position of the rectangle:

new Rectangle(randomized_x_position, Game.SCREEN_HEIGHT/2-Rectangle.height/2,0);

The problem is that I don't know how to calculate the randomized_x_position in order to keep the rectangle inside the grey rectangle.

Another problem is that the current Rectangle.render() method is making my rectangle outside the grey rectangle after rotation:

What's happening

When what I really want is:

What I want


2 Answers 2

  • Pick random angle(a)
  • Compute AABB: http://willperone.net/Code/coderr.php

    w' = h*abs(sin(a)) + w*abs(cos(a))

    h' = h*abs(cos(a)) + w*abs(sin(a))

  • Pick random positions so aabb fits inside bigger rectangle. p_x <= Width - w' p_y <= Height - h'

  • \$\begingroup\$ The Width and Height that you indicated on the 3rd step are from the grey rectangle? \$\endgroup\$ Commented Feb 17, 2013 at 21:29
  • 1
    \$\begingroup\$ Yep. And p1:p2 is position of aabb. Center of the red rectangle would be at [p1 + w'/2 ; p2 + h'/2 ], I think. \$\endgroup\$
    – szotp
    Commented Feb 17, 2013 at 22:32

You have a two options. (1) You can either make sure your rotation doesn't move the rectangle outside the bounds, or (2) you can move the rectangle back after the rotation.

  1. To rotate it without leaving the gray box, pick the top or bottom left edge. If it's the top choose a rotation less than 90 degrees that rotates counter clockwise, if it's the bottom choose a rotation less than 90 degrees that rotates clockwise. Then rotate by the chosen rotate around the selected corner. This will essentially pivot the rectangle by a corner that's already on the edge, ensuring it doesn't rotate outside the grey rectangle. Unfortunately this option limits your choices a good deal.

  2. Moving it back inside the grey rectangle isn't too bad, and gives you a lot more flexibility. Essentially, you'll need to either find the bounds of the rectangle after rotation, or maintain the bounds through rotation. Maintaining the bounds would be something like keeping track of the points that make up the corners and rotating those points when you rotate the rectangle. Either way, you'll end up with the bounds of the rectangle, and then you can easily compare its edges to that of the grey rectangle. Allowing you to easily translate the rectangle by the difference.

The first option is easier to implement, but less flexible. The second option will be harder to implement, but far more flexible.

  • \$\begingroup\$ Doesn't libgdx have something that could help finding the bounds of the rectangle for the 2nd option? \$\endgroup\$ Commented Feb 17, 2013 at 18:05
  • \$\begingroup\$ If I take the first option, is it possible to know the max value that randomized_x_position could have? \$\endgroup\$ Commented Feb 17, 2013 at 18:06
  • \$\begingroup\$ I don't know libgdx, but from a quick search I didn't find anything built in for that. Likely not since you're using the ShapeRenderer to rotate the shape. The first option would not require any translation because the rotation would not move it outside the grey rectangle. \$\endgroup\$
    – House
    Commented Feb 17, 2013 at 18:17
  • \$\begingroup\$ But if I want to put my red rectangle on the right side of the grey rectangle, what do I need to do? \$\endgroup\$ Commented Feb 17, 2013 at 21:19
  • \$\begingroup\$ The answer you've accepted is the second option. If that makes things more clear. \$\endgroup\$
    – House
    Commented Feb 17, 2013 at 22:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .