checkInside for sprites

  ... the case you feel the need for a new feature or want to submit one.

checkInside for sprites

Postby NightJumper » Sat Sep 18, 2010 11:30 pm

So couldn't find this anywhere and I can think of a lot of different places to use something like this in a variety of games so figured Id post this and see if anyone else can use it.

What it does is checks to see if the sprite is entirely over top of a different different sprite, this is good for trying to make a sprite reach a certain spot (which is what I needed in my game. So heres the code, I pretty much took the check in the checkCollision method in ShapeCollisionChecker where it checks if the top left vertice is inside and then applied it to all vertices:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     public boolean checkInside(Sprite objA){
  2.         float[] VerticesA= new float[8];
  3.         float[] VerticesB= new float[8];
  4.         RectangularShapeCollisionChecker.fillVertices(this, VerticesA);
  5.         RectangularShapeCollisionChecker.fillVertices(objA, VerticesB);
  6.         if(ShapeCollisionChecker.checkContains(VerticesB, 8, VerticesA[0], VerticesA[0 + VERTEX_INDEX_Y])&&
  7.            ShapeCollisionChecker.checkContains(VerticesB, 8, VerticesA[2], VerticesA[2 + VERTEX_INDEX_Y])&&
  8.            ShapeCollisionChecker.checkContains(VerticesB, 8, VerticesA[4], VerticesA[4 + VERTEX_INDEX_Y])&&
  9.            ShapeCollisionChecker.checkContains(VerticesB, 8, VerticesA[6], VerticesA[6 + VERTEX_INDEX_Y])) {
  10.             return true;
  11.         } else {
  12.             return false;
  13.         }
  14.     }
Parsed in 0.013 seconds, using GeSHi 1.0.8.4


Currently only works for rectangular shapes but thats all I required and not sure how to do it for circles
NightJumper
 
Posts: 98
Joined: Wed Sep 01, 2010 9:42 pm

Return to Features

Who is online

Users browsing this forum: No registered users and 9 guests