3 Replies - 2453 Views - Last Post: 08 December 2011 - 11:41 PM

#1 WolfCoder   User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 828
  • View blog
  • Posts: 7,696
  • Joined: 05-May 05

Collision Detection Object

Posted 17 January 2006 - 06:13 PM

Description: Simply put this in it's own file, and make classes that extend from it. Ex: public class Bomberman extends SolidObject And I simply have the object draw itself, and smart move to the keyboard.Sets up a solid object that can transport, move without detection, move with detection, and smart move to slide around the corners. Make your own objects that extend to this one.
import java.awt.*;
import java.util.*;
public class SolidObject // Applies to all objects who need collision detection
{
     public Rectangle collisionHull; // Rectangle tester
     public double posX,posY; // Position
     public int sizeX,sizeY; // Size of collision HULL
     public void makeSolidObject(double pos_x,double pos_y,int size_x,int size_y) // Creates a new solid object
     {
          posX = pos_x;
          posY = pos_y;
          sizeX = size_x;
          sizeY = size_y;
     }
     public void compileHull()
     {
          // Compiles the hull into a Rectangle class for colliding
          collisionHull = new Rectangle((int)posX,(int)posY,sizeX,sizeY);
     }
     public boolean isCollidingWith(SolidObject what)
     {
          // Test whether the collision between two solid objects is happening
          compileHull();
          what.compileHull();
          if(collisionHull.intersects(what.collisionHull))
               return true;
          return false;
     }
     public boolean isCollidingWith(Rectangle hull,SolidObject what)
     {
          // Same, but with raw rectangles
          what.compileHull();
          if(hull.intersects(what.collisionHull))
               return true;
          return false;
     }
     public boolean isColliding(ArrayList solidObjects)
     {
          // Test whether the collision between this object and any one in the list of objects is happening
          for(int index = 0;index < solidObjects.size();index++)
          {
               if(isCollidingWith((SolidObject)solidObjects.get(index)))
               {
                    return true;
               }
          }
          return false;
     }
     public void transport(double pos_x,double pos_y)
     {
          // Teleport this object to the location given
          posX = pos_x;
          posY = pos_y;
     }
     public void move(double pos_x,double pos_y)
     {
          // Move this object by the amount given ignoring collision detection
          posX += pos_x;
          posY += pos_y;
     }
     public void move(double pos_x,double pos_y,ArrayList solidObjects)
     {
          // Move this object by the amount given with collision detection
          posX += pos_x;
          posY += pos_y;
          if(isColliding(solidObjects))
          {
               posX -= pos_x;
               posY -= pos_y;
          }
     }
     public int getDirectionToSlideIn(SolidObject what,int direction)
     {
          // Find out what direction to slide in. Returns zero if nothing was hit.
          Rectangle a,b;
          switch(direction)
          {
               case 3: // MOVE DOWN
               case 1: // MOVE UP
               a = new Rectangle((int)posX,(int)posY,sizeX/2,sizeY);
               b = new Rectangle((int)(posX+sizeX/2),(int)posY,sizeX/2,sizeY);
               if(!(isCollidingWith(a,what) && isCollidingWith(b,what)))
               {
                    if(isCollidingWith(a,what))
                         return 2;
                    if(isCollidingWith(b,what))
                         return 4;
               }
               case 2: // MOVE RIGHT
               case 4: // MOVE LEFT
               a = new Rectangle((int)posX,(int)posY,sizeX,sizeY/2);
               b = new Rectangle((int)posX,(int)(posY+sizeY/2),sizeX,sizeY/2);
               if(!(isCollidingWith(a,what) && isCollidingWith(b,what)))
               {
                    if(isCollidingWith(a,what))
                         return 3;
                    if(isCollidingWith(b,what))
                         return 1;
               }     
          }
          return 0;
     }
     public void moveSmart(int direction,double length,ArrayList solidObjects,int num_moves)
     {
          // Move this object, and slide it along the corners
          if(num_moves < 2)
          {
               double pos_x = 0,pos_y = 0;
               switch(direction)
               {
                    case 1:
                    pos_x = 0;
                    pos_y = -length;
                    break;
                    case 2:
                    pos_x = length;
                    pos_y = 0;
                    break;
                    case 3:
                    pos_x = 0;
                    pos_y = length;
                    break;
                    case 4:
                    pos_x = -length;
                    pos_y = 0;
                    break;
               }
               posX += pos_x;
               posY += pos_y; 
               for(int index = 0;index < solidObjects.size();index++)
               {
                    if(isCollidingWith((SolidObject)solidObjects.get(index)))
                    {
                         int movedir = getDirectionToSlideIn((SolidObject)solidObjects.get(index),direction);
                         posX -= pos_x;
                         posY -= pos_y;
                         moveSmart(movedir,length,solidObjects,num_moves+1);
                    }
               }
          }
     }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Collision Detection Object

#2 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2972
  • View blog
  • Posts: 11,223
  • Joined: 15-July 08

Re: Collision Detection Object

Posted 16 July 2009 - 07:09 PM

Very Nice Wolf! I'll be very happy to use this!
Was This Post Helpful? 0
  • +
  • -

#3 sErvEr79   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 14-May 09

Re: Collision Detection Object

Posted 15 December 2009 - 08:06 PM

hello there.. maybe this sound stupid, how to run this code I already do the instruction by extend it. but how to run it exactly in main? TQ
Was This Post Helpful? 0
  • +
  • -

#4 micro_   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 27-April 11

Re: Collision Detection Object

Posted 08 December 2011 - 11:41 PM

yes i aslso find main file!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1