Re: Two NSRects adjacent/touching?
Re: Two NSRects adjacent/touching?
- Subject: Re: Two NSRects adjacent/touching?
- From: William Squires <email@hidden>
- Date: Mon, 2 Jul 2007 21:44:19 -0500
As my papa always used to say, "If it's too difficult, you're
probably doing it the wrong way!" And I think that applies here; the
clue being "tile"!
Most tile games are laid out on orthogonal (square) or hexagonal
(hexagon) grids. The following discussion assumes you're using an
orthogonal grid.
This means each tile can have a simple integer coordinate in that
grid. So if you have an area on screen of size MxN (pixels) and each
tile is size PxQ pixels (horiz. x vert.) where P divides M and Q
divides N (i.e. P Mod M = 0, and Q Mod N = 0), then each time can
have an (x,y) coordinate that describes how many tiles over/down it
is. This is a lot easier than trying to figure out if two NSRects
intersect or touch! It's also easy to translate the mouse X,Y
position (relative to the origin of the drawing surface) into (x,y)
coordinates of a particular tile with integer division.
x = X \ P
y = Y \ Q
(where X, Y are mouse coordinates, and P, Q are the size in pixels of
a tile.)
so:
Tile.h
#import <Foundation/Foundation.h>
@interface Tile : NSObject
{
int x;
int y;
}
- (id)init;
- (void)setX: (int) xVal;
- (int)x;
- (void)setY: (int) yVal;
- (int)y;
- (BOOL)isAdjacentToTile: (Tile *) theTile;
...
@end
Tile.m
#import "Tile.h"
@implementation Tile : NSObject
// implementation of the getters and setters not shown for brevity;
they're easy enough to write.
- (BOOL)isAdjacentToTile: (Tile *) theTile
{
BOOL result;
result = NO;
If (((x == [theTile x]) && (y == [theTile y] - 1)) ||
((x == [theTile x] - 1) && (y == [theTile y])) ||
((x == [theTile x]) && (y == [theTile y] + 1)) ||
((x == [theTile x] + 1) && (y == [theTile y])))
{
result = YES; // Yeah, I know the braces aren't required, but I'm
a*** retentive, okay? :)
}
return result;
}
@end
The same reasoning applies to a hexagonal grid, but the checks for
adjacency are a bit more complex (as is wrapping your thought
processes around a hexagonal coordinate!) Given (view this with a
monospaced font):
(1, 0) \
_| __
__/ \__
(0, 0) / \__/ \ <- (1, 1)
\__/ \__/
\__/
_
(0, 1) /|
I tend to give them coordinates as such:
(uld, lld) where uld = upper-left-diagonal and lld = lower-left-
diagonal. That is, imagine diagonal lines
traversing the screen (or your graph paper) from the upper-left
corner to the lower-right, and regularly spaced. These are the uld
grid lines (uld=0 and increasing from lower-left->upper-right).
Another set of intersecting lines traverses the screen from the
lower-left to the upper-right. These are the lld grid lines and are
indexed from 0 at the upper-left, and increasing as you go down and
to the right. These intersecting lines intersect at the geometrical
center of each hexagon. You can think of this as like taking your
orthogonal grid and tilting your view camera so that you get an
isometric view. Technically, if you tilt an orthogonal grid of
squares as an isometric view, you get an orthogonal grid of
rhombuses! But it's not too difficult to imagine my hexagons as
rhombahedrons!
The big "gotcha!" is mapping the mouse (X,Y) coordinates in such a
system!! :(
On Jun 27, 2007, at 5:29 AM, Brad Peterson wrote:
Hi all,
Summary:
Is there a simple way to determine if two NSRects are
touching/adjacent to each other?
Background:
I'm working on an instructional tile game, and need a
way to determine if tiles are touching.
Currently, I have a tile class, each of which knows
its own NSRect, whether it's been clicked, etc. At the
time of a new game, all of the tiles are created and
stored in an NSMutableArray.
When the game's being played, and the user clicks
somewhere in the view, I iterate through the tile
array to see which one the user clicked, using a
simple NSPointInRect() call.
This is where I'm running into trouble: only some
tiles are clickable at any given moment. Part of what
determines clickability is whether the tile has any
sides that aren't touching another tile.
So, I was hoping to find a way to determine this
programatically by feeding the clicked tile's rect
into a function which could then quickly compare that
rect against those of the other tiles.
Is there an easy way to determine whether two NSRects
are touching/adjacent to each other?
Thanks!
B
______________________________________________________________________
______________
Boardwalk for $500? In 2007? Ha! Play Monopoly Here and Now (it's
updated for today's economy) at Yahoo! Games.
http://get.games.yahoo.com/proddesc?gamekey=monopolyherenow
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
40satx.rr.com
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden