Hello,
I'm using Kwigbo's collision detection code, and what I get is an in-accurate graphical result but accurate mathematically.
OK, so I checked the collision code using the vector normalization, it took the ball like 3-4 minutes to reach the other ball.
The result is as follows:
The distance between the two centers was 27.49823, it's tremendously accurate, as the radiuses combined are only 27.5.
An image:
http://img162.imageshack.us/i/picture3m.png/
As you can see, it is not graphically accurate (more accurate than before though), although mathematically it's super accurate.
Any ideas? (Maybe Anchor points, I don't know)
Implementation in GameLayer:
- (void)checkIfOut:(ccTime)dt {
NSMutableArray *arrayToRemove = [[NSMutableArray alloc] init];;
for(int i = 0 ; i < [bulletArray count] ; i++){
Bullet *b = [bulletArray objectAtIndex:i];
if(b.outOfScreen){
[arrayToRemove addObject:b];
[self removeChild:b cleanup:YES];
} else {
if([self circle:b.position withRadius:b.radius hitsCircle:mr.position withRadius:mr.radius])
{
[[Director sharedDirector] stopAnimation];
}
else [b update:dt];
}
b = nil;
}
[bulletArray removeObjectsInArray:arrayToRemove];
[arrayToRemove release];
}
- (BOOL)circle:(CGPoint)center1 withRadius:(float)radius1 hitsCircle:(CGPoint)center2 withRadius:(float)radius2 {
float xdif = center2.x - center1.x;
float ydif = center2.y - center1.y;
float distance = sqrt(xdif*xdif + ydif* ydif);
float minDist = radius1 + radius2;
if(distance <= minDist)
return kHit;
return kNoHit;
}
Ball sprite object implementation:
- (id)init {
if(self = [super init]){
outOfScreen = NO;
Sprite *spr = [Sprite spriteWithFile:@"Bullet.png"];
self.contentSize = spr.contentSize;
radius = self.contentSize.width /2;
[self addChild:spr];
self.anchorPoint = ccp(0.5, 0.5);
self.position = ccp(168.0, 45.0);
mass = 10;
}
return self;
}
- (void)update:(ccTime)dt {
float x = self.position.x;
float y = self.position.y;
float width = self.contentSize.width;
float height = self.contentSize.height;
x += velocity.x * dt;
y += velocity.y * dt;
NSLog(@"x:%f y:%f", x, y);
self.position = CGPointMake(x, y);
if(x - width > 320.0)
outOfScreen = YES;
if(x + width < 0)
outOfScreen = YES;
if(y - height > 460)
outOfScreen = YES;
if(y + height < 0)
outOfScreen = YES;
}
Meteor sprite is pretty much alike... except for out of screen property which is set a bit differently but that doesn't matter...