Cocoa collision problems
Cocoa collision problems
- Subject: Cocoa collision problems
- From: Mike Brinkman <email@hidden>
- Date: Mon, 13 May 2002 07:08:26 -0400
I'm making a break-out type clone in Cocoa, but I've hit a snag when it
comes to telling when the ball hits a brick. I've defined my own data
structure for a brick, and the ball is simply an NSBezierPath. The brick
looks like this:
typedef struct _brick{
NSRect r;
NSBezierPath *bp;
BOOL active;
}brick;
Later in my interface file, I declare an array of bricks as such:
brick myBrick[120];
In my implementation file I initialize the array using a for loop, and have
the following lines:
myBrick[h].active = YES;
myBrick[h].r = NSMakeRect(k,j,30,18);
myBrick[h].bp = [[NSBezierPath bezierPathWithRect:myBrick[h].r] retain];
k & j are variables to make the bricks appear in a row & column layout.
Here is what I do in collision detection:
- (void)checkCollision
{
NSRect brickRect;
NSRect ballRect = [ball bounds];
NSRect viewRect = [self bounds];
// Loop through all 120 bricks
for(h = 0; h < 120; h++)
{
brickRect = [myBrick[h].bp bounds];
// is the brick active?
if(myBrick[h].active == YES){
// if the top of the ball is greater than the
// bottom of the brick...
if((ballRect.origin.y + ballRect.size.height) >
brickRect.origin.y){
dy = -dy; // change the y direction of the ball
[at release];
at = [[NSAffineTransform transform] retain];
[at translateXBy:dx yBy:dy];
[[NSSound soundNamed:@"Pong2003"] play];
[[NSColor blackColor] set];
[myBrick[h].bp fill];
myBrick[h].active = NO;
}
}
}
....
}
This isn't the complete collision detection code, but it's the part that
checks to see if the ball's top is higher than the brick's bottom.
It has two if statements, both of which seem to pass the test, as the
"Pong2003" sound plays. But for some reaon, the direction of the ball
doesn't change, and the brick doesn't become black.
I thought I had the logic right, and it is set up pretty much the same as
the other collision detections for the top & sides of my game screen (both
of which work). I'm not sure if this is a logic problem with the way my code
is constructed, or if it is a Cocoa problem and I'm using AppKit &
Foundation incorrectly. Any help would be appreciated!
Mike Brinkman
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.