[Solved]Entity collision

I have been working on a entity system that works fairly well but I have run into a problem that I can’t figure out. What I want is for a entity to collide with another entity on lets say the x axis, but can still move freely on the y, entity sliding on another entity. I found little on the subject and what I did find I can’t get it to work.

excert of the entity class

	public void move(float xa, float ya, int delta){
	CollisionManager(delta);
		if(canPass){
			pos.x += xa * delta;
			pos.y += ya * delta;
		}
	}

	// collision method----------------------------------------------
	// can be overridden to add new collision
	public void CollisionManager(int delta) {
		int x0 = (int)(pos.x + (vel.x * delta));
		int y0 = (int)(pos.y + (vel.y * delta));
		int x1 = (int)(pos.x + width + (vel.x * delta));
		int y1 = (int)(pos.y + height + (vel.y * delta));
		
		List<Entity> entities = world.getEntities(x0, y0, x1, y1);
		for(int i = 0; i < entities.size(); i++){
			Entity e = entities.get(i);
			if(this == e) continue;
				this.touchedBy(e);
				
				if(this.blocks(e)){
					canPass = false;
				}
			}
		
	}

	// if the player intersects a object with 4 side
	public boolean intersects(int xt0, int yt0, int xt1, int yt1) {
		int xe0 = (int) (pos.x);
		int ye0 = (int) (pos.y);
		int xe1 = (int) (pos.x + width);
		int ye1 = (int) (pos.y + height);
		return !(xe1 < xt0 || ye1 < yt0 || xe0 > xt1 || ye0 >  yt1);

	}

and the part of the world class that looks to see if a entity is colliding with another

public List<Entity> getEntities(int x0, int y0, int x1, int y1){
		List<Entity> result = new ArrayList<>();
		
		int xt0 = (x0 >> Tile.Tile_Width / 2) - 1;
		int yt0 = (y0 >> Tile.Tile_Height / 2) - 1;
		int xt1 = (x1 >> Tile.Tile_Width / 2) + 1;
		int yt1 = (y1 >> Tile.Tile_Height / 2) + 1;
		for (int y = yt0; y <= yt1; y++) {
			for (int x = xt0; x <= xt1; x++) {
			if(x < 0 || y < 0 || x >= width || y >= height)continue;
		   
			for(int i = 0; i < entities.size(); i++){
			 Entity e = entities.get(i);
			if(e.intersects(x0,y0,x1,y1)) result.add(e);
			}
		}
		
		
	}
		return result;

	}

thank you
EDIT: the list entities referse to all the entities in the gameworld

if i undestand what you mean

you want to move for example in y if it is not block also if x is block, right?
in this case you can modify easily your code like this


 public void CollisionManager(int delta) {
      int x0 = (int)(pos.x + (vel.x * delta));
      int y0 = (int)(pos.y + (vel.y * delta));
      int x1 = (int)(pos.x + width + (vel.x * delta));
      int y1 = (int)(pos.y + height + (vel.y * delta));
      
      List<Entity> entities = world.getEntities(x0, y0, x1, y1);
      for(int i = 0; i < entities.size(); i++){
         Entity e = entities.get(i);
         if(this == e) continue;
            this.touchedBy(e);
            
            if(this.blocksX(e)){
               canPassX = false;
            }
            if(this.blocksY(e)){
               canPassY = false;
            }
         }
      
   }


and in the move code function


 public void move(float xa, float ya, int delta){
   CollisionManager(delta);
      if(canPassX){
         pos.x += xa * delta;
      }
      if(canPassY){
         pos.y += ya * delta;
      }

   }

that mean that you have to modify your function block and split in 2 function
one to verify if block X and another to verify if block Y
so you need also to boolean variables to instead that one
canPassX
canPassY

I am not sure but I am guessing you ment something like


 public void CollisionManager(int delta) {
      int x0 = (int)(pos.x + (vel.x * delta));
      int y0 = (int)(pos.y + (vel.y * delta));
      int x1 = (int)(pos.x + width + (vel.x * delta));
      int y1 = (int)(pos.y + height + (vel.y * delta));
      
      List<Entity> entities = world.getEntities(x0, y0, x1, y1);
      for(int i = 0; i < entities.size(); i++){
         Entity e = entities.get(i);
         if(this == e) continue;
            this.touchedBy(e);
            
            if(this.blocksX(e)){
               canPassX = false;
            }
if(this.blocksY(e)){
               canPassY = false;
            }
         }
      
   }

and yeah I like that idea but it conflicts with my intersects method

 // if the player intersects a object with 4 side
   public boolean intersects(int xt0, int yt0, int xt1, int yt1) {
      int xe0 = (int) (pos.x);
      int ye0 = (int) (pos.y);
      int xe1 = (int) (pos.x + width);
      int ye1 = (int) (pos.y + height);
      return !(xe1 < xt0 || ye1 < yt0 || xe0 > xt1 || ye0 >  yt1);

   }

the reason I didn’t seprerate this in the first place is becaues I have done tile collision that slides in the past that didn’t need to be seperated.
the tile collision I did in the past was simalar to the getEntities(int x0, int y0, int x1, int y1) method in the world class
and I sepeate my move method logic by calling it twice as move(xa,0) and move(0,ya)

so I think it should work but I am guessing there is a mistake somewhere that I didn’t notice

what happens is when a entity hits another entity it sticks and I wan’t it to be able to slide

Collision is easier if you use prediction instead of moving everything first and rolling back collisions afterward. It also prevents the phenomenon called “tunneling”. With this model you can use “real world” physics. So not only can you slide on surfaces, you can also use bouncing and friction. To slide, you project your velocity vector on to a line, and to bounce you reflect off a line. (Look up the math and diagrams for both.) If the walls are axis aligned, you just set the x or y velocity to zero. Then you use your simulation with your new velocity and position for the remaining duration of the turn. You must “stop” the player before it touches the wall so that you do not intersect with it at the end of the update or while still applying sliding.

you can continue to work also with this logic
also the intersection function is ok that mean that this two rectangles intersect in same point
and you are shore 100%

the problem is after:
understand where, in what side, they intersect so that you can decide where you can move

the chance can be four

A over B
___
| |
| A |
||
| |
| B |
|
|

this case is when min Y A < max Y B
come move X = true
come move Y = false

A under B
___
| |
| B |
||
| |
| A |
|
|

this case is when max Y A > max Y B
come move X = true
come move Y = false

A before B
_______
| | |
| A | B |
|_| |

this case is when max X A > min X B
come move X = false
come move Y = true

A after B
_______
| | |
| B | A |
|_| |

this case is when min X A < max X B
come move X = false
come move Y = true

now
the 2 case
A over B
A under B
are checked inside the new function blockY

the 2 case
A before B
A after B
are checked inside the new function blockX

i hope is cleare
also my hash art :slight_smile:

something I messed up and would like to correct

the move method was ment to be like this

   public void move(float xa, float ya, int delta){
    if(xa != 0 && ya != 0){
     move(xa,0, delta);
     move(0,ya,delta);
     return;
    }
   CollisionManager(delta);
      if(canPass){
         pos.x += xa * delta;
         pos.y += ya * delta;
      }
   }

this is what the code was ment to look like but it still errors and dosen’t let me slide on walls

Thanks everyone I found the problem the collisonManager class was calling the global velocity for the method and wasent spliting them up