我正在尝试制作一个游戏(使用带有 c++ 的 irrlicht 引擎),您可以在其中使用盒子来诱捕敌人。但是我只是不知道如何检测当检测到用户与一个或多个盒子之间的碰撞时应该移动什么。另一件事是,还会有一些称为砖块的物体能够阻挡运动。
由于我不太擅长解释事情,所以我附上了一张图片,所以希望它能澄清我的意思:(来源:jrahmati.info)
我用我的代码尝试了几件事,但没有成功。所以我真的希望有人会努力回答这个问题。提前致谢。顺便说一句,我不需要 c++ 中的答案,java 或 .Net 语言也可以。
对于任何对代码感兴趣的人:
bool Game::tryMove(user, dir) 的内容,它试图将所有东西从玩家身上移开
bool thereIsCollision = false;
bool undoMovements = false;
bool userCollision = false;
do{
thereIsCollision = false;
for (int i = 0; i < totalObjects; i++) {
//First check if object hits the user
if(gameObjects[i].hits(user)){
if (gameObjects[i]->isMovable()) {
MovableObject* mObject = (MovableObject*) gameObjects[i];
mObject->move(dir);
mObject->setPushVector(dir);
userCollision = true;
//thereIsCollision = true;
}
else{
undoMovements = true;
thereIsCollision = false; //To break do-while loop
userCollision = true;
break;
}
}
}
if(undoMovements)
break;
for (int i = 0; i < totalObjects; i++) {
//Then check if objects hit each other
for (int i2 = 0; i2 < totalObjects; i2++) {
if(i == i2)
continue;
if (gameObjects[i2].hits(gameObjects[i])){
//thereIsCollision = true;
if(gameObjects[i]->isMovable() && gameObjects[i2]->isMovable()){
MovableObject* mObject = (MovableObject*) gameObjects[i];
MovableObject* mObject2 = (MovableObject*) gameObjects[i2];
if(mObject->getPushVector().X > 0
|| mObject->getPushVector().Y > 0
|| mObject->getPushVector().Z > 0){
mObject2->move(mObject->getPushVector());
mObject2->setPushVector(mObject->getPushVector());
mObject->setPushVector(irr::core::vector3df(0, 0, 0));
}
else if(mObject2->getPushVector().X > 0
|| mObject2->getPushVector().Y > 0
|| mObject2->getPushVector().Z > 0){
mObject->move(mObject2->getPushVector());
mObject->setPushVector(mObject2->getPushVector());
mObject2->setPushVector(irr::core::vector3df(0, 0, 0));
}
}
else{
undoMovements = true;
thereIsCollision = false; //To break do-while loop
break;
}
}
}
}
}while(thereIsCollision);
for (int i = 0; i < totalObjects; i++) {
if (gameObjects[i]->isMovable()) {
MovableObject* mObject = (MovableObject*) gameObjects[i];
if(undoMovements){
// Resets position of gameObject to its previous one
mObject->undoMovement();
}
else{
// confirms movement(i.e. prevPosition=curPosition)
mObject->confirmMovement();
}
}
}
return !(userCollision);