Collision Detection Help / Once-over

Hi again! This is for a school assignment, but I was wondering if anyone could give my code a once-over for me / help me figure out the bug in the system. I’ve been following a wonderful tutorial by Long Nguyen (Tutorial playlist / YouTube channel here! ) very closely, so I’m pretty confident that the coding is sound - I’m just not sure what the problem is.

Edit: For anyone wanting to run the code, this link has all of the images / .csv zipped up

Pardon the long code - again, I’m not sure what/where the issue is, so I figured it was better to put too much than too little! Big thanks to anyone who takes the time to read this!

final float moveSpeed = 2;
final float spriteScale = 1;
final float spriteSize = 10;
final float gravity = 0.01;
final float jumpSpeed = 1;

final float rightMarg = 40;
final float leftMarg = 40;
final float vertMarg = 4;
PImage bg, block, block2;
Sprite player;
ArrayList <Sprite> platforms;

float viewX = 0;
float viewY = 0;

void setup()
{
    size(100,100);
    imageMode (CENTER);
    bg = loadImage("bg.png");
  player = new Sprite("dogsitright.png", 100,10);
  player.changeX = 0;
  player.changeY = 0;
  platforms = new ArrayList<Sprite>();
    
    block = loadImage("block.png");
    block2 = loadImage("block2.png");  
    createPlatforms("map.csv");
  
}

void draw()
{
  background(200);
  scroll();
 image(bg,438,50);
  player.display();
 // player.update();
  resolvePlatformCollisions(player, platforms);
  
  for (Sprite pl: platforms)
  pl.display();
}

void scroll()
{
  float rightBound = viewX + width - rightMarg;
  if (player.getRight() > rightBound)
  {
    viewX += player.getRight() - rightBound;
  }
  
  float leftBound = viewX + leftMarg;
  if (player.getLeft() < leftBound)
  {
    viewX -= leftBound - player.getLeft();
  }
  
  float botBound = viewY + height- vertMarg;
  if (player.getBottom() > botBound)
  {
    viewY += player.getBottom() - botBound;
  }
  
    float topBound = viewY + vertMarg;
  if (player.getTop() < topBound)
  {
    viewY -= topBound - player.getTop();
  }
  translate(-viewX, -viewY);
}

boolean isOnPlatforms(Sprite pl, ArrayList<Sprite> walls)
{
  pl.centerY += 5;
  ArrayList<Sprite> colisList = checkCollisionList(pl,walls);
  pl.centerY -= 5;
  if (colisList.size() >0)
  {
    return true;
  }
  else
  return false;
}

void createPlatforms(String filename)
{
  String[] lines = loadStrings(filename);
  for(int row = 0; row < lines.length; row++)
  {
    String [] values = split(lines[row],",");
    for (int col= 0;col < values.length; col++) {
      if(values[col].equals("1"))
      {
        Sprite s = new Sprite(block);
        s.centerX = spriteSize/2 + col * spriteSize;
        s.centerY = spriteSize/2 + row * spriteSize;
        platforms.add(s);
      }
       else if(values[col].equals("2"))
      {
        Sprite s = new Sprite(block2);
        s.centerX = spriteSize/2 + col * spriteSize;
        s.centerY = spriteSize/2 + row * spriteSize;
        platforms.add(s);
      }
    }
  }
}

boolean checkCollision(Sprite s1, Sprite s2)
{
  boolean noXOverlap = s1.getRight() <= s2.getLeft() || s1.getLeft() >= s2.getRight();
   boolean noYOverlap = s1.getBottom() <= s2.getTop() || s1.getTop() >=s2.getBottom();
   if (noXOverlap || noYOverlap)
   {
     return false;
   }
   else
   {
     return true;
   }
}

ArrayList<Sprite> checkCollisionList(Sprite pl, ArrayList<Sprite> list)
{
  ArrayList<Sprite> collisionList = new ArrayList<Sprite>();
  for (Sprite plist: list){
    if(checkCollision(pl,plist))
    collisionList.add(plist);
}
return collisionList;
}

void resolvePlatformCollisions(Sprite pl, ArrayList<Sprite> walls)
{
  pl.changeY += gravity;
   
  pl.centerY += pl.changeY;
  ArrayList<Sprite> colisList = checkCollisionList(pl, walls);
  if(colisList.size()> 0)
  {
    Sprite collided = colisList.get(0);
    if(pl.changeY > 0) 
    {
      pl.setBottom(collided.getTop());
    }
    else if(pl.changeY < 0)
    {
      pl.setTop(collided.getBottom());
    }
    pl.changeY =0;
  }
  
    pl.centerX += pl.changeX;
  colisList = checkCollisionList(pl, walls);
  if(colisList.size()> 0)
  {
    Sprite collided = colisList.get(0);
    if(pl.changeX > 0) 
    {
      pl.setRight(collided.getLeft());
    }
    else if(pl.changeY < 0)
    {
      pl.setLeft(collided.getRight());
    }
  }
  
}

void keyPressed()
{
    if(keyCode == RIGHT)
  {
    player.changeX = moveSpeed;
  }
  else if (keyCode == LEFT)
  {
    player.changeX = -moveSpeed;
  }
  else if (keyCode == UP)
  {
    player.changeY = -moveSpeed;
  }
  else if(key =='a' /*&& isOnPlatforms(player,platforms)*/)
  {
    player.changeY = -jumpSpeed;
  }
}

void keyReleased()
{
    if(keyCode == RIGHT)
  {
    player.changeX = 0;
  }
  else if (keyCode == LEFT)
  {
    player.changeX = 0;
  }
  else if (keyCode == UP)
  {
    player.changeY = 0;
  }
}

class Sprite {
  PImage image;
  float centerX, centerY;
  float changeX, changeY;
  float wid, hei;
  
  Sprite(String filename, float x, float y)
  {
    image = loadImage(filename);
    wid = image.width;
    hei = image.height;
    centerX = x;
    centerY = y;
    changeX = 0;
    changeY = 0;
  }
  
  Sprite(PImage img)
  {
    image = img;
  }
  
  void display()
  {
    image(image, centerX, centerY);
  }
  
  void update()
  {
    centerX += changeX;
    centerY += changeY;
  }
 void setLeft(float left)
 {
   centerX = left + wid/2;
 }
  float getLeft()
  {
    return centerX + wid/2;
  }
  
  void setRight(float right)
  {
    centerX = right - wid/2;
  }
  float getRight()
  {
    return centerX + wid/2;
  }
void setBottom(float bottom)
{
  centerY = bottom - hei/2;
}
  float getBottom()
 {
   return centerY + hei/2;
 }
  
  void setTop(float top)
  {
    centerY = top + hei/2;
  }
  float getTop()
  {
    return centerY - hei/2;
  }
  
}

Hello @moffmix. Could you describe the issue your are running into? Do you get an error?

Besides, it’s not possible to run your code without the image files so if you could produce a minimal and standalone version of the sketch, that would also help diagnose the issue.

Hi there, thanks for responding! I haven’t been getting any error messages - everything seems to run cleanly - but I believe there has to be an issue somewhere within checkCollision or resolvePlatformCollisions. The purpose of those two functions is to check whether or not the player sprite is on top of one of the platforms specified in the .csv - if it is, it creates a solid barrier that the sprite can’t jump through. It’ll also let the sprite collect objects / be hit by enemies later on, but I’m not quite there yet!
Right now, the sprite is still moving through the platforms and not sitting on top, so I’ve been trying to figure out why.

I also added a .zip above with the images needed to run the program - I’m a bit short on time at the moment, so it’s the best I can do for now!