Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (531)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Rectangle Objects  (Read 1379 times)
0 Members and 1 Guest are viewing this topic.
Offline Best Username Ever

Junior Member





« Posted 2012-12-21 19:06:27 »

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.

1  
2  
3  
4  
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; }

1  
2  
3  
4  
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; }

1  
2  
3  
4  
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...
Offline Rorkien
« Reply #1 - Posted 2012-12-21 19:14:08 »

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.
Offline sproingie

JGO Kernel


Medals: 201



« Reply #2 - Posted 2012-12-21 19:20:46 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #3 - Posted 2012-12-21 19:29:25 »

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

Junior Member


Medals: 2



« Reply #4 - Posted 2012-12-21 21:26:33 »

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.
Offline Roquen
« Reply #5 - Posted 2012-12-21 21:43:26 »

The center is a more logical choice for symmetry...think intersections.  And uniformity of representation...think circles.
Offline Best Username Ever

Junior Member





« Reply #6 - Posted 2012-12-21 23:07:53 »

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?
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #7 - Posted 2012-12-21 23:19:15 »

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:

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?

Offline Best Username Ever

Junior Member





« Reply #8 - Posted 2012-12-22 00:01:12 »

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
move(dx, dy)
.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
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;
}
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #9 - Posted 2012-12-22 00:03:47 »

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

The others are comparing 4 numbers.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Best Username Ever

Junior Member





« Reply #10 - Posted 2012-12-22 00:21:30 »

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

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #11 - Posted 2012-12-22 00:48:12 »

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. Smiley

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #12 - Posted 2012-12-22 00:52:53 »

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

Offline Best Username Ever

Junior Member





« Reply #13 - Posted 2012-12-22 02:09:14 »

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. Smiley

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.

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

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

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #14 - Posted 2012-12-22 02:28:29 »

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. Smiley

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.

...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.  Grin

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Roquen
« Reply #15 - Posted 2012-12-22 07:12:19 »

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?
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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (13 views)
2014-07-24 01:59:36

Riven (13 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (14 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (39 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (35 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!