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