碰撞精度 [英] Collision accuracy

查看:134
本文介绍了碰撞精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不太清楚该如何措辞,但我一直在使用

Not quite sure how to word this but I've been using

if (CGRectIntersectsRect(ball.frame, bottom.frame)) {
    [self finish ];
}

用于我的碰撞检测,它可以完美地运行代码,但有时球会与底部并运行代码,但是您可以清楚地看到对象之间存在间隙。我自己创建了图像,但它们周围没有背景。我想知道是否还有其他方法可以对其进行编码或进行编码,以便等到x像素彼此相交后才运行代码。

for my collision detection and it run the code perfectly but the ball sometimes collides with the bottom and runs the code but you can clearly see there is a gap in between the objects. i have created the images my self and they have no background around them. i was wondering if theres any other way of coding it or making it so the it doesn't run the code until the intersect x amount of pixels into one another.

推荐答案

您可以通过多种方式实现直线碰撞。您的解决方案实际上是矩形-矩形碰撞检测。这是我在一个小型游戏项目中的工作方式。

You can implement ball-line collision in many ways. Your solution is in fact a rectangle - rectangle collision detection. Here is how I did it in one of my small gaming projects. It gave me best results and it's simple.

让我们说一个球的球半径为 ballRadius ,位置为(xBall,yBall)。该行由两个点(xStart,yStart)(xEnd,yEnd)定义。

Let's say that a ball has a ballRadius, and location (xBall, yBall). The line is defined with two points (xStart, yStart) and (xEnd, yEnd).

简单碰撞检测的实现:

float ballRadius = ...;
float x1 = xStart - xBall;
float y1 = yStart - yBall;

float x2 = xEnd - xBall;
float y2 = yEnd - yBall;

float dx = x2 - x1;
float dy = y2 - y1;
float dr = sqrtf(powf(dx, 2) + powf(dy, 2));
float D = x1*y2 - x2*y1;

float delta = powf(ballRadius*0.9,2)*powf(dr,2) - powf(D,2);

if (delta >= 0) 
{ 
    // Collision detected 
}

如果delta大于零,则球(圆)和直线之间有两个交点。如果delta等于零,则只有一个交点-完美碰撞。

If delta is greater than zero there are two intersections between ball (circle) and line. If delta is equal to zero there is one intersection – perfect collision.

这篇关于碰撞精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆