Skip to main content
added 67 characters in body
Source Link
Engineer
  • 30.4k
  • 4
  • 76
  • 124
for (int i = 0; i < entities.length; i++) //run through ALL entities
{
   Entity e1 = entities[i];
   for (int j = i+1; j < entities.length; j++) //run through each entity AFTER e1
   {
       Entity e2 = entities[j];
       if (isColliding(e1.rect, e2.rect))
           resolveCollisionFor(e1, e2);
   }
}
for (int i = 0; i < entities.length; i++)
{
   Entity e1 = entities[i];
   for (int j = i+1; j < entities.length; j++)
   {
       Entity e2 = entities[j];
       if (isColliding(e1.rect, e2.rect))
           resolveCollisionFor(e1, e2);
   }
}
for (int i = 0; i < entities.length; i++) //run through ALL entities
{
   Entity e1 = entities[i];
   for (int j = i+1; j < entities.length; j++) //run through each entity AFTER e1
   {
       Entity e2 = entities[j];
       if (isColliding(e1.rect, e2.rect))
           resolveCollisionFor(e1, e2);
   }
}
added 76 characters in body
Source Link
Engineer
  • 30.4k
  • 4
  • 76
  • 124
//This checks overlap in a single dimension -- either x or in y
public boolean checkOverlap(int start0, int end0, int start1, int end1) 
{
    if(start0 > start1 && start0 < end1)
        return true;
    if(start1 > start0 && start1 < end0)
        return true;
    return false;
}

//This does it for two dimensions. If overlapping in X AND Y, you've collided.
//You can call this in your movePlayer handler, but if the "females" (er!) move
//AS WELL, then you must do it per update or you will miss collisions (unless
//you make sure all "females" are moved before the player, on each update).    
public bool isColliding(Rectangle rect0, Rectangle rect1)
{
    if (checkOverlap(rect0.x, rect0.x + rect0.width, rect1.x, rect1.x + rect1.width) &&
        checkOverlap(rect0.y, rect0.y + rect0.height, rect1.y, rect1.y + rect1.height))
    {
        //resolve your collision. You don't need to store isColliding -- just
        //calculate, then act on the result. 
    }    
}

On each game loop update: Get your inputs, move the player, get all inputs for other entities and move them, then at the end, run through the full entities list (player + "females") and run isColliding() on each. This the standard way to do 2D AABB collisions.

Actually if you are running through the full list of n entities and comparing them against n-1 other entities, then for the first entity in the list, call it a, you will have to check against b through n, but on checking b you will not have to check it against a (since a was just checked against b in the prior step) or itself, so you would check from c through n... and so on for each remaining entity. This essentially looks like:

for (int i = 0; i < entities.length; i++)
{
   Entity e1 = entities[i];
   for (int j = i+1; j < entities.length; j++)
   {
       Entity e2 = entities[j];
       if (isColliding(e1.rect, e2.rect))
           resolveCollisionFor(e1, e2);
   }
}
//This checks overlap in a single dimension -- either x or in y
public boolean checkOverlap(int start0, int end0, int start1, int end1) 
{
    if(start0 > start1 && start0 < end1)
        return true;
    if(start1 > start0 && start1 < end0)
        return true;
    return false;
}

//This does it for two dimensions. If overlapping in X AND Y, you've collided.
//You can call this in your movePlayer handler, but if the "females" (er!) move
//AS WELL, then you must do it per update or you will miss collisions (unless
//you make sure all "females" are moved before the player, on each update).    
public bool isColliding(Rectangle rect0, Rectangle rect1)
{
    if (checkOverlap(rect0.x, rect0.x + rect0.width, rect1.x, rect1.x + rect1.width) &&
        checkOverlap(rect0.y, rect0.y + rect0.height, rect1.y, rect1.y + rect1.height))
    {
        //resolve your collision. You don't need to store isColliding -- just
        //calculate, then act on the result. 
    }    
}

On each game loop update: Get your inputs, move the player, get all inputs for other entities and move them, then at the end, run through the full entities list (player + "females") and run isColliding() on each. This the standard way to do 2D AABB collisions.

//This checks overlap in a single dimension -- either x or in y
public boolean checkOverlap(int start0, int end0, int start1, int end1) 
{
    if(start0 > start1 && start0 < end1)
        return true;
    if(start1 > start0 && start1 < end0)
        return true;
    return false;
}

//This does it for two dimensions. If overlapping in X AND Y, you've collided.
//You can call this in your movePlayer handler, but if the "females" (er!) move
//AS WELL, then you must do it per update or you will miss collisions (unless
//you make sure all "females" are moved before the player, on each update).    
public bool isColliding(Rectangle rect0, Rectangle rect1)
{
    if (checkOverlap(rect0.x, rect0.x + rect0.width, rect1.x, rect1.x + rect1.width) &&
        checkOverlap(rect0.y, rect0.y + rect0.height, rect1.y, rect1.y + rect1.height))
    {
        //resolve your collision. You don't need to store isColliding -- just
        //calculate, then act on the result. 
    }    
}

On each game loop update: Get your inputs, move the player, get all inputs for other entities and move them, then at the end, run through the full entities list (player + "females") and run isColliding() on each. This the standard way to do 2D AABB collisions.

Actually if you are running through the full list of n entities and comparing them against n-1 other entities, then for the first entity in the list, call it a, you will have to check against b through n, but on checking b you will not have to check it against a (since a was just checked against b in the prior step) or itself, so you would check from c through n... and so on for each remaining entity. This essentially looks like:

for (int i = 0; i < entities.length; i++)
{
   Entity e1 = entities[i];
   for (int j = i+1; j < entities.length; j++)
   {
       Entity e2 = entities[j];
       if (isColliding(e1.rect, e2.rect))
           resolveCollisionFor(e1, e2);
   }
}
added 76 characters in body
Source Link
Engineer
  • 30.4k
  • 4
  • 76
  • 124
//This checks overlap in a single dimension -- either x or in y
public boolean checkOverlap(int start0, int end0, int start1, int end1) 
{
    if(start0 > start1 && start0 < end1)
        return true;
    if(start1 > start0 && start1 < end0)
        return true;
    return false;
}

//This does it for two dimensions. If overlapping in X AND Y, you've collided.
//You can call this eitherin everyyour updatemovePlayer handler, but if the "females" (typicaler!) move
//AS WELL, then you must do it per update or inyou yourwill movePlayermiss handlercollisions (unless
//you make sure all "females" are moved before the player, on each update).    
public bool isColliding(Rectangle rect0, Rectangle rect1)
{
    if (checkOverlap(rect0.x, rect0.x + rect0.width, rect1.x, rect1.x + rect1.width) &&
        checkOverlap(rect0.y, rect0.y + rect0.height, rect1.y, rect1.y + rect1.height))
    {
        //resolve your collision. You don't need to store isColliding -- just
        //calculate, then act on the result. 
    }    
}

On each game loop update: Get your inputs, move the player, get all inputs for other entities and move them, then at the end, run through the full entities list (player + "females") and run isColliding() on each. This the standard way to do 2D AABB collisions.

//This checks overlap in a single dimension -- either x or in y
public boolean checkOverlap(int start0, int end0, int start1, int end1) 
{
    if(start0 > start1 && start0 < end1)
        return true;
    if(start1 > start0 && start1 < end0)
        return true;
    return false;
}

//This does it for two dimensions. If overlapping in X AND Y, you've collided.
//You can call this either every update (typical), or in your movePlayer handler.    
public bool isColliding(Rectangle rect0, Rectangle rect1)
{
    if (checkOverlap(rect0.x, rect0.x + rect0.width, rect1.x, rect1.x + rect1.width) &&
        checkOverlap(rect0.y, rect0.y + rect0.height, rect1.y, rect1.y + rect1.height))
    {
        //resolve your collision. You don't need to store isColliding -- just
        //calculate, then act on the result. 
    }    
}

This the standard way to do 2D AABB collisions.

//This checks overlap in a single dimension -- either x or in y
public boolean checkOverlap(int start0, int end0, int start1, int end1) 
{
    if(start0 > start1 && start0 < end1)
        return true;
    if(start1 > start0 && start1 < end0)
        return true;
    return false;
}

//This does it for two dimensions. If overlapping in X AND Y, you've collided.
//You can call this in your movePlayer handler, but if the "females" (er!) move
//AS WELL, then you must do it per update or you will miss collisions (unless
//you make sure all "females" are moved before the player, on each update).    
public bool isColliding(Rectangle rect0, Rectangle rect1)
{
    if (checkOverlap(rect0.x, rect0.x + rect0.width, rect1.x, rect1.x + rect1.width) &&
        checkOverlap(rect0.y, rect0.y + rect0.height, rect1.y, rect1.y + rect1.height))
    {
        //resolve your collision. You don't need to store isColliding -- just
        //calculate, then act on the result. 
    }    
}

On each game loop update: Get your inputs, move the player, get all inputs for other entities and move them, then at the end, run through the full entities list (player + "females") and run isColliding() on each. This the standard way to do 2D AABB collisions.

Source Link
Engineer
  • 30.4k
  • 4
  • 76
  • 124
Loading