내 게임에서 오브젝트와 충돌을 감지하고 있습니다. 지금은 모든 것이 수직으로 움직이지만 다른 운동을위한 옵션은 계속 열려 있습니다. 고전적인 2 차원 수직 공간 사수입니다. 충돌 검사 모든 개체를 통해산발적 충돌 감지
가 지금 내가 루프 :
여기for(std::list<Object*>::iterator iter = mObjectList.begin(); iter != mObjectList.end();) {
Object *m = (*iter);
for(std::list<Object*>::iterator innerIter = ++iter; innerIter != mObjectList.end(); innerIter++) {
Object *s = (*innerIter);
if(m->getType() == s->getType()) {
break;
}
if(m->checkCollision(s)) {
m->onCollision(s);
s->onCollision(m);
}
}
}
나는 충돌을 확인하는 방법입니다,
bool checkCollision(Object *other) {
float radius = mDiameter/2.f;
float theirRadius = other->getDiameter()/2.f;
Vector<float> ourMidPoint = getAbsoluteMidPoint();
Vector<float> theirMidPoint = other->getAbsoluteMidPoint();
// If the other object is in between our path on the y axis
if(std::min(getAbsoluteMidPoint().y - radius, getPreviousAbsoluteMidPoint().y - radius) <= theirMidPoint.y &&
theirMidPoint.y <= std::max(getAbsoluteMidPoint().y + radius, getPreviousAbsoluteMidPoint().y + radius)) {
// Get the distance between the midpoints on the x axis
float xd = abs(ourMidPoint.x - theirMidPoint.x);
// If the distance between the two midpoints
// is greater than both of their radii together
// then they are too far away to collide
if(xd > radius+theirRadius) {
return false;
} else {
return true;
}
}
return false;
}
문제는 무작위로 제대로 충돌을 감지하지만, 다른 시간은 그것을 전혀 감지하지 못합니다. 객체가 다른 유형을 가지기 때문에 객체 루프에서 이탈하는 if 문이 아닙니다. 물체가 화면 상단에 가까울수록 충돌이 더 잘 감지됩니다. 화면 하단에 가까울수록 정확하게 또는 심지어 전혀 감지되지 않을 확률이 적습니다. 그러나 이러한 상황이 항상 발생하는 것은 아닙니다. 물체의 직경은 문제가 있는지보기 위해 방대한 양 (10과 20)이지만 전혀 도움이되지 않습니다.
편집 - 당신의 내부 루프가 mObjectList.begin에서 시작해야
bool checkCollision(Object *other) {
float radius = mDiameter/2.f;
float theirRadius = other->getDiameter()/2.f;
Vector<float> ourMidPoint = getAbsoluteMidPoint();
Vector<float> theirMidPoint = other->getAbsoluteMidPoint();
// Find the distance between the two points from the center of the object
float a = theirMidPoint.x - ourMidPoint.x;
float b = theirMidPoint.y - ourMidPoint.y;
// Find the hypotenues
double c = (a*a)+(b*b);
double radii = pow(radius+theirRadius, 2.f);
// If the distance between the points is less than or equal to the radius
// then the circles intersect
if(c <= radii*radii) {
return true;
} else {
return false;
}
}
가 아직 변화가 베타의 대답의 최신 버전에 제안했다 가지고 (즉, 합을 제곱하지 -of-radii 두 번)? 문제가 해결되면 베타의 대답을 받아 들여야합니다. 그렇지 않다면, 여전히 작동하지 않는 방법을 나타내야합니다. –