I'm trying to make a game (using irrlicht engine with c++) where you can trap your enemy using boxes. But I just don't get how to detect what should be moved when a collision between the user and one or more boxes is detected. Another thing is that there will also be some objects called bricks which will be able to block movements.
Since I'm not very good in explaining things, I included an image, so it will hopefully clarify what I mean:
I tried several things with my code but without success. So I really hope someone will take the effort to give an answer to this issue. Thanks in advance. By the way, I don't need the answer necessarily in c++, java or .Net languages are also ok.
For anyone interested in the code:
content of bool Game::tryMove(user, dir) which tries to move everthing away from the player
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);