Circle Collision inaccurate

 

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...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值