Rectangle Objects

What type of variable layout do you use for rectangles and 3D boxes? I can think of three structures. The choice is probably pretty unimportant, but I’m curious what other people use, even if just out of habit and not for any practical reason. Methods are shown to demonstrate the differences, but I would probably work with public variables and no methods in practice. (To avoid creating extra classes or objects.) It should be obvious what the omitted methods do and how they extend to three dimensions.

private double left, top, width, height;
public double getRight() { return left + width; }
public void move(double dx, double dy) { left += dx; right += dy; }
public double getCenterY() { return top + height / 2; } 
private double centerX, centerY, halfWidth, halfHeight;
public double getRight() { return centerX + halfWidth; }
public void move(double dx, double dy) { centerX += dx; centerY += dy; }
public double getCenterY() { return centerY; }
private double left, top, right, bottom;
public double getRight() { return right; }
public void move(double dx, double dy) { left += dx; right += dx; top += dy; bottom += dy; }
public double getCenterY() { return (top + bottom) / 2.0; 

I can’t recall ever actually using the second version, but it seems the most convenient…

the usual x, y, width and height, with x and y being the top-left point. Your first example.

Your second example seems so strange for me, i can’t see myself drawing something based on their center position :persecutioncomplex: (I LOVE CORNERS! THINK OF THE POOR CORNERS)

And the third example is like the first one, but more confusing.

Same as Rorkien except “the usual” origin for me is lower-left.

Center position might make sense for a button. And left/right/top/bottom is useful for intersection. So I usually have methods that compute all of these.

Center, x & y extents, complex number for orientation.

Personally I’d go with the center method, but that’s because I’ve worked with 3D for many years. Also, from experience I know that exporting stuff from softimage where I am using a script to write out animation, can be a pain in the ass if you aren’t working with transforms, where the center is the origin.

The center is a more logical choice for symmetry…think intersections. And uniformity of representation…think circles.

I didn’t follow that. Can you rephrase that? What does “symmetry” achieve in intersection tests?

The second one (using the centre) is the best because of this more efficient collision detection method.

EDIT: I also, unknowingly, answered this question too:

I think the intersection tests all work the same way. One and two look like they would perform the same way. Three is shorter, but takes longer to update in [icode]move(dx, dy)[/icode].

public boolean intersects(...)
{
  return al + aw <= bl && al >= bl + bw &&
         at + ah <= bt && at >= bt + bh;
}

public boolean intersects(...)
{
  return Math.abs(ax - bx) < asw + bsw && Math.abs(ay - by) < ash + bsh;
}

public boolean intersects(...)
{
  return ar <= bl && al >= br && at <= bb && bt >= ab;
}

Method 2 should be the fastest because you are only comparing 2 numbers.

The others are comparing 4 numbers.

Depends on how much Math.abs costs. But when I think about it, it’s probably cheaper than a compare.

For 2d, I usually go for (x,y) is the corner closest to the origin. That’s more ‘normal’ compared to most 2d APIs in my experience.

For 3d, (x,y,z) as the center of the box makes most sense IMHO. The symmetry tends to make code for 3d tests like ray intersection cleaner and more consistent. I assume that’s what Roquen is getting at, it’s kinda hard to explain without doing a fully worked example with both approaches.

Either way make sure you put a comment in your variable declarations saying what’s what. Ascii diagrams in your class declaration are a big win when someone comes along later trying to decipher what you’ve done. :slight_smile:

I do bruteforce collisions with method 2, and with over 256 entities, it still runs fine without putting strain on the cpu.

[quote=“Orangy Tang,post:12,topic:40513”]
Or just use left/right/top/bottom, left/top/width/height, or centerX/centerY/halfWidth/halfHeight for your variable names. That way you never even need to go to the documentation.

Does that involve O(n) or O(n^2) checks? Either way, it does not say much about the relative efficiency of different methods.

[quote=“Best_Username_Ever,post:14,topic:40513”]

…in which case, you need a comment that says which way ‘up’ your 2d coord system is. Because top/bottom will change depending on if you’re going with origin-at-top-left-of-screen vs. origin-at-bottom-left-of-screen. ;D

You can fold the problem like this: http://www.java-gaming.org/topics/vectors-what-s-the-point/24307/msg/225743/view.html#msg225743

Same logic for rect vs. rect.