Before I start, my first language isn’t english. So if I have issues with grammars or so, please genereously understand.
I’m making 2d java game but have some issues with collision pushing.
after object is moved, it checks if there is any collision with othe object.
It puts every collisioned object in its Arraylist;
After collision is detected, I uses this code to seperate those objects,
public abstract class MapObject {
protected Shape object;
protected float x,y;
public void collisionPushed(MapObject O){
int check=0;
float pushX=0, pushY=0;
if(O.getShape().getMaxX()<=x){
pushX=O.getShape().getMaxX()-object.getMinX();
check++;
}
if(O.getShape().getMinX()>=x){
pushX=O.getShape().getMinX()-object.getMaxX();
check++;
}
if(O.getShape().getMaxY()<=y){
pushY=O.getShape().getMaxY()-object.getMinY();
check++;
}
if(O.getShape().getMinY()>=y){
pushY=O.getShape().getMinY()-object.getMaxY();
check++;
}
if(check!=1){
if(O.getShape().getMinX()>=object.getMinX()&&O.getShape().getMaxX()<=object.getMaxX()){
pushX=0;
}
else if(O.getShape().getMinY()>=object.getMinY()&&O.getShape().getMaxY()<=object.getMaxY()){
pushY=0;
}
else if(Math.abs(pushX)>Math.abs(pushY)){
pushX=Math.signum(pushX)*Math.abs(pushY);
}
else{
pushY=Math.signum(pushY)*Math.abs(pushX);
}
}
moveObject(x+pushX,y+pushY);
}
}
At first, I thought It work pretty well, but there was a problem
(sorry, I can’t figure out how to post image here)
(Green lines represents shape)
those pink ghosts are programmed to run away from the charactor. So they need to be gathered in the corner
When there are few of them it waw Ok, like the right side of image
But when there are many of them they slip through the wall like the left side of image.
I think this happens because after colisionpushed by wall, the ghost is again collisionpushed into wall by other ghosts. making them slip through the wall.
Even though I know the reason and I tried several things on my own, but none of them worked. How can I make these objects don’t slip through the wall?