1
\$\begingroup\$

I have big rectangle that can move from side to side, its boundaries can't enter the small rectangle inside it, the anchor point is in the middle of both of them.

Let's say the big rect is 1000 * 1000, and the small one is 200 * 200
in the game the small rect is the "camera" and the big rect is the ground, only the ground moves.

The solution can be in any programming language.

Here is some drawing to show what I mean : enter image description here

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

It's just some basic maths. There are a lot of sides and tutorials about collision detection. For example this could help: What is the fastest way to work out 2D bounding box intersection? It is really easy, as long as no box rotates. Then it is called AABB (Axis Aligned Bounding Box). It gets a little bit more complicated, if they rotate, but it's still not taht difficult. Just ask Google for "Box Collision Detection"

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for the replay , about the simple AABB is great but in my case this is box inside abox \$\endgroup\$
    – user3689
    May 4, 2015 at 3:28
  • 1
    \$\begingroup\$ @user3689 Oh, you're right. But in this case you just need to check whether one corner is outside. something like: return o1.x < o2.x || o1.y < o2.y || o1.x+ o1.width > o2.x + o2.width || o1.y + o1.height > o2.y + o2.height. With o1 as \$\endgroup\$
    – Tobias B
    May 4, 2015 at 3:38

You must log in to answer this question.

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