Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.begin(ShapeType.Filled);
        shapeRenderer.setColor(color);
        shapeRenderer.identity();
        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);
        shapeRenderer.end();
    }
}

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

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted
  • 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'

share|improve this answer
    
The Width and Height that you indicated on the 3rd step are from the grey rectangle? –  Cristiano Santos Feb 17 '13 at 21:29
1  
Yep. And p1:p2 is position of aabb. Center of the red rectangle would be at [p1 + w'/2 ; p2 + h'/2 ], I think. –  krzat Feb 17 '13 at 22:32
add comment

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.

share|improve this answer
    
Doesn't libgdx have something that could help finding the bounds of the rectangle for the 2nd option? –  Cristiano Santos Feb 17 '13 at 18:05
    
If I take the first option, is it possible to know the max value that randomized_x_position could have? –  Cristiano Santos Feb 17 '13 at 18:06
    
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. –  Byte56 Feb 17 '13 at 18:17
    
But if I want to put my red rectangle on the right side of the grey rectangle, what do I need to do? –  Cristiano Santos Feb 17 '13 at 21:19
    
The answer you've accepted is the second option. If that makes things more clear. –  Byte56 Feb 17 '13 at 22:42
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.