[solved] 2d pushing after collision don't work as intended

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?

I would check for a collision before executing movements. Then I’ll know which two objects have collided and do stuff accordingly instead of checking a whole list of objects.

Theres also the Rectangle class that helps to check for intersections of two objects.

If a ghost hits the wall then he should occupy the space directly above/below/next to the wall. Then if another ghost comes along he should occupy the space directly adjacent to the ghost he just bumped into

I agree with @ndnwarrior15 here;

The best bet with collision detection is that, if an object is not wanting to change position, it doesn’t need to check if it is going to collide with anything, this reduces some of the checks required.

If you want the ghosts to be pushed, then you would start with the arrow, checking each frame if it will hit anything, if it hits the ghost and it gets pushed back, each frame that ghost would check if it will collide with anything else.

I could be wrong, but looking at the pictures, are you filtering the ghosts so that they don’t collide with other ghosts? If that’s the case, and the intention, perhaps, add a different flag so they will filter the collisions with other ghosts, unless it’s being pushed by an arrow.

I had the similar problem that I went through recently… the way I did it was; and remember this is done by cycling through all objects before the object is moved.

  • First, determine how far the object will move
  • draw a line from each edge in the direction it will move
  • if one or more of those lines crosses a collision with another collidable object, then get that point.
  • determine the collision point from the shortest line
  • move the object the length of that line instead of the full length.

Hope that’s helpful.

Thanks it really helped me out.
So moving - check collision - pushing back is not the way to do it.

I rewrote my collision checks like ten times, no lie.

Were you able to get it to work?