Re: Cocoa collision problems
Re: Cocoa collision problems
- Subject: Re: Cocoa collision problems
- From: email@hidden
- Date: Mon, 13 May 2002 12:23:31 +0100
Try using boolean NSView.hitTest(NSPoint topOfBrick).
I dunno if your Bricks are individual NSViews though in which case this
wont work.
I cant see anything immediately wrong with the code but does C allow the
the expression dy = -dy?
what happens if you make that dy = (-1)*dy
Mike Brinkman <email@hidden>@lists.apple.com on 13/05/2002 12:08:26 PM
Sent by: email@hidden
To: Cocoa <email@hidden>
cc:
Subject: Cocoa collision problems
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.
**********************************************************************************
Our network may monitor outgoing and incoming e-mail messages for
security and customer service purposes, but this e-mail is confidential.
Please notify the sender immediately if you receive it in error, and then delete it.
Thank you.
_______________________________________________
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.