Re: Cocoa collision problems
Re: Cocoa collision problems
- Subject: Re: Cocoa collision problems
- From: email@hidden
- Date: Mon, 13 May 2002 18:54:30 -0400
Have you looked at the Cocoa Sprite Kit? While it's still in first
beta, it is already VERY powerful, especially for games like that, and
more advanced features are in the works. I'd suggest giving it a look.
http://www.sugarcubesoftware.com/csk
Owen Anderson
On Monday, May 13, 2002, at 06:44 PM, John C. Randolph wrote:
That looks like an awful lot of work. Is there some reason why you
can't just use NSIntersectsRect()?
-jcr
On Monday, May 13, 2002, at 04:08 AM, Mike Brinkman wrote:
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.
John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html
_______________________________________________
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.
_______________________________________________
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.