Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Become a Java Expert!

Join 149,087 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,429 people online right now. Registration is fast and FREE... Join Now!





Collision Detection Object

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.

Submitted By: WolfCoder
Actions:
Rating:
Views: 9,334

Language: Java

Last Modified: March 17, 2008
Instructions: 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.

Snippet


  1. import java.awt.*;
  2. import java.util.*;
  3. public class SolidObject // Applies to all objects who need collision detection
  4. {
  5.      public Rectangle collisionHull; // Rectangle tester
  6.      public double posX,posY; // Position
  7.      public int sizeX,sizeY; // Size of collision HULL
  8.      public void makeSolidObject(double pos_x,double pos_y,int size_x,int size_y) // Creates a new solid object
  9.      {
  10.           posX = pos_x;
  11.           posY = pos_y;
  12.           sizeX = size_x;
  13.           sizeY = size_y;
  14.      }
  15.      public void compileHull()
  16.      {
  17.           // Compiles the hull into a Rectangle class for colliding
  18.           collisionHull = new Rectangle((int)posX,(int)posY,sizeX,sizeY);
  19.      }
  20.      public boolean isCollidingWith(SolidObject what)
  21.      {
  22.           // Test whether the collision between two solid objects is happening
  23.           compileHull();
  24.           what.compileHull();
  25.           if(collisionHull.intersects(what.collisionHull))
  26.                return true;
  27.           return false;
  28.      }
  29.      public boolean isCollidingWith(Rectangle hull,SolidObject what)
  30.      {
  31.           // Same, but with raw rectangles
  32.           what.compileHull();
  33.           if(hull.intersects(what.collisionHull))
  34.                return true;
  35.           return false;
  36.      }
  37.      public boolean isColliding(ArrayList solidObjects)
  38.      {
  39.           // Test whether the collision between this object and any one in the list of objects is happening
  40.           for(int index = 0;index < solidObjects.size();index++)
  41.           {
  42.                if(isCollidingWith((SolidObject)solidObjects.get(index)))
  43.                {
  44.                     return true;
  45.                }
  46.           }
  47.           return false;
  48.      }
  49.      public void transport(double pos_x,double pos_y)
  50.      {
  51.           // Teleport this object to the location given
  52.           posX = pos_x;
  53.           posY = pos_y;
  54.      }
  55.      public void move(double pos_x,double pos_y)
  56.      {
  57.           // Move this object by the amount given ignoring collision detection
  58.           posX += pos_x;
  59.           posY += pos_y;
  60.      }
  61.      public void move(double pos_x,double pos_y,ArrayList solidObjects)
  62.      {
  63.           // Move this object by the amount given with collision detection
  64.           posX += pos_x;
  65.           posY += pos_y;
  66.           if(isColliding(solidObjects))
  67.           {
  68.                posX -= pos_x;
  69.                posY -= pos_y;
  70.           }
  71.      }
  72.      public int getDirectionToSlideIn(SolidObject what,int direction)
  73.      {
  74.           // Find out what direction to slide in. Returns zero if nothing was hit.
  75.           Rectangle a,b;
  76.           switch(direction)
  77.           {
  78.                case 3: // MOVE DOWN
  79.                case 1: // MOVE UP
  80.                a = new Rectangle((int)posX,(int)posY,sizeX/2,sizeY);
  81.                b = new Rectangle((int)(posX+sizeX/2),(int)posY,sizeX/2,sizeY);
  82.                if(!(isCollidingWith(a,what) && isCollidingWith(b,what)))
  83.                {
  84.                     if(isCollidingWith(a,what))
  85.                          return 2;
  86.                     if(isCollidingWith(b,what))
  87.                          return 4;
  88.                }
  89.                case 2: // MOVE RIGHT
  90.                case 4: // MOVE LEFT
  91.                a = new Rectangle((int)posX,(int)posY,sizeX,sizeY/2);
  92.                b = new Rectangle((int)posX,(int)(posY+sizeY/2),sizeX,sizeY/2);
  93.                if(!(isCollidingWith(a,what) && isCollidingWith(b,what)))
  94.                {
  95.                     if(isCollidingWith(a,what))
  96.                          return 3;
  97.                     if(isCollidingWith(b,what))
  98.                          return 1;
  99.                }     
  100.           }
  101.           return 0;
  102.      }
  103.      public void moveSmart(int direction,double length,ArrayList solidObjects,int num_moves)
  104.      {
  105.           // Move this object, and slide it along the corners
  106.           if(num_moves < 2)
  107.           {
  108.                double pos_x = 0,pos_y = 0;
  109.                switch(direction)
  110.                {
  111.                     case 1:
  112.                     pos_x = 0;
  113.                     pos_y = -length;
  114.                     break;
  115.                     case 2:
  116.                     pos_x = length;
  117.                     pos_y = 0;
  118.                     break;
  119.                     case 3:
  120.                     pos_x = 0;
  121.                     pos_y = length;
  122.                     break;
  123.                     case 4:
  124.                     pos_x = -length;
  125.                     pos_y = 0;
  126.                     break;
  127.                }
  128.                posX += pos_x;
  129.                posY += pos_y;
  130.                for(int index = 0;index < solidObjects.size();index++)
  131.                {
  132.                     if(isCollidingWith((SolidObject)solidObjects.get(index)))
  133.                     {
  134.                          int movedir = getDirectionToSlideIn((SolidObject)solidObjects.get(index),direction);
  135.                          posX -= pos_x;
  136.                          posY -= pos_y;
  137.                          moveSmart(movedir,length,solidObjects,num_moves+1);
  138.                     }
  139.                }
  140.           }
  141.      }
  142. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month