Circle Collision inaccurate

使用Kwigbo的碰撞检测代码,在数学上准确但在图形显示上有偏差。通过检查代码发现,两个球体之间的距离计算准确,但视觉效果不匹配。可能与锚点设置有关。
 

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

### Unity 中的碰撞检测与处理 在 Unity 的物理引擎中,`Collision Detection` 是指两个具有 `Collider` 和 `Rigidbody` 的物体之间的交互过程。以下是关于如何有效使用和优化 Unity 中的碰撞检测机制的相关信息。 #### 1. 碰撞事件及其触发条件 Unity 提供了三种主要的碰撞回调函数来响应不同阶段的碰撞行为: - **OnCollisionEnter**: 当一个带有刚体或 Collider 的对象首次与其他对象发生接触时调用此方法[^1]。 ```csharp void OnCollisionEnter(Collision collision) { Debug.Log($"开始碰撞到对象: {collision.gameObject.name}"); foreach (ContactPoint contact in collision.contacts) { Debug.DrawRay(contact.point, contact.normal, Color.red); } } ``` - **OnCollisionStay**: 如果两个对象保持接触状态,则每一帧都会调用该方法[^1]。 - **OnCollisionExit**: 当两者的接触结束时会调用这个方法[^1]。 这些方法都接收一个名为 `Collision` 的参数,它提供了丰富的数据结构用于获取详细的碰撞信息,比如接触点的位置、法向量以及相对速度等。 #### 2. 物理更新频率调整 为了提高性能并减少不必要的计算开销,可以考虑修改固定时间步长(`Fixed Delta Time`)设置。通过进入菜单栏路径 `Edit->Project Settings->Time`, 可以将固定的更新间隔设定在一个合理的范围内(通常建议介于0.04至0.067秒之间),从而降低每秒钟执行 FixedUpdate 方法次数的同时也减少了物理引擎对于碰撞探测及刚体刷新操作的要求[^3]。 另外值得注意的是如果角色模型本身关联了一个 Rigidbody 组件的话还可以开启插值选项(interpolation), 这样即使采用较低的时间分辨率也能获得较为流畅的画面效果. #### 3. 输入控制集成实例分析 下面展示了一段简单的玩家控制器脚本示例,展示了如何结合新的输入系统读取方向键或者手柄摇杆的数据,并将其应用到实际运动逻辑当中去[^2]: ```csharp using System.Collections; using UnityEngine; public class PlayerMovement : MonoBehaviour { public float speed = 5f; private Vector2 movementInput; void Start() { // 初始化 InputAction 并启用 var playerControls = new PlayerInputControl(); playerControls.Enable(); playerControls.Gameplay.Move.performed += ctx => movementInput = ctx.ReadValue<Vector2>(); playerControls.Gameplay.Move.canceled += ctx => movementInput = Vector2.zero; } void FixedUpdate() { MovePlayer(movementInput); } void MovePlayer(Vector2 direction) { transform.Translate(direction.normalized * speed * Time.fixedDeltaTime, Space.World); } } ``` 以上代码片段定义了一个基本的角色移动框架,其中利用现代 API 来捕获用户的按键动作并将它们映射成相应的位移矢量。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值