So with the game I was building I was using this line of code to detect when 2 object collided which worked, kind of.
I notices that it detected when 2 square frames collided even though the images I used were not square. For instance 2 balls colliding could trigger the if statement even though the actual balls never touched since they are round on a square frame from a CGRectMake();.So the edges of the frames colliding would trigger it.
In searching for a solution I discovered an idea that I adapted to my code. When the collision is detected I call a Class that I created and pass in the 2 objects as UIImageViews that should have collided. I create a new CGBitmapContextCreate and only get the Alpha which I assign to a 50% opacity. I combined both images to the new context.
Each image at 50% alpha opacity would give a pixel value of 127. But when the pixels over lapped that pixel level would increase to over 127 which would indicate a TRUE collision of the object. It is taxing on the system since it must iterate over each pixel but it works with no visual slow down in the game.
Here is the Method that does the job. The for loop was right from the original poster and I get what it is doing. But the part in read I am unsure of.
It is an 'unsigned char'? A char only hold a character from what I remember in my pascal class, also why unsigned unless it is to hold more positive values? Also why char if it is holding int values like RGBA, why not an int array? 2. It described it as an array but I don't see the tell signs of the brackets [] like data[] when you declare a char array?
Can someone shed light on the char array line?
Thanks!
Code:
if (CGRectIntersectsRect(rock.frame, ship.frame))
I notices that it detected when 2 square frames collided even though the images I used were not square. For instance 2 balls colliding could trigger the if statement even though the actual balls never touched since they are round on a square frame from a CGRectMake();.So the edges of the frames colliding would trigger it.
In searching for a solution I discovered an idea that I adapted to my code. When the collision is detected I call a Class that I created and pass in the 2 objects as UIImageViews that should have collided. I create a new CGBitmapContextCreate and only get the Alpha which I assign to a 50% opacity. I combined both images to the new context.
Each image at 50% alpha opacity would give a pixel value of 127. But when the pixels over lapped that pixel level would increase to over 127 which would indicate a TRUE collision of the object. It is taxing on the system since it must iterate over each pixel but it works with no visual slow down in the game.
Here is the Method that does the job. The for loop was right from the original poster and I get what it is doing. But the part in read I am unsure of.
Code:
-(BOOL)checkAlphaCollision:(UIImageView*)ship withRock:(UIImageView*)rock{
imageCollide = NO;
int colorBits = 8;
CGContextRef context = CGBitmapContextCreate(NULL, device_w, device_h, colorBits, 0, NULL, kCGImageAlphaOnly);
CGRect rect;
CGImageRef shipRef = ship.image.CGImage;
CGImageRef rockRef = rock.image.CGImage;
CGContextSetAlpha(context, 0); // create a rect and fill it with the dimensions of the screen.
rect = CGRectMake(0, 0, device_w, device_h);
CGContextFillRect(context, rect);
CGContextSetAlpha(context, 0.5); //Adjust the alpha level to 50% and add the ship and rock
rect = CGRectMake(ship.frame.origin.x, (460 - ship.frame.origin.y) - ship.frame.size.height, ship.frame.size.width, ship.frame.size.height);
CGContextDrawImage(context, rect, shipRef);
rect = CGRectMake(rock.frame.origin.x, (460 - rock.frame.origin.y) - rock.frame.size.height , rock.frame.size.width, rock.frame.size.height);
CGContextDrawImage(context, rect, rockRef);
[COLOR="Red"]unsigned char* data = CGBitmapContextGetData(context);[/COLOR]
//NSLog(@"%p",data);
for (size_t i = 0; i<device_w*device_h; i++) {
//NSLog(@"%d", (int)data[i]);
if (data[i] > 128) {
//NSLog(@"collidepoint %d",(int)data[i]);
imageCollide = YES;
break;
}
}
return imageCollide;
}
It is an 'unsigned char'? A char only hold a character from what I remember in my pascal class, also why unsigned unless it is to hold more positive values? Also why char if it is holding int values like RGBA, why not an int array? 2. It described it as an array but I don't see the tell signs of the brackets [] like data[] when you declare a char array?
Can someone shed light on the char array line?
Thanks!