Okay, so Im doing this pixel precise collision thingi for Slick2D Images and it works perfect so far but now I got some problems which are simply not cool (not for me, but this should be a framework so stuff like this, need to work.
What I’m doing is drawing to the stencil buffer and then i get the pixels which collided, while drawing to images to the buffer, the code looks like this:
/**
* Starts the collision test. must be called before using <code>intersects</code>.
*
* @throws CrashHatException if begin was called without an end.
*/
public void begin() throws CrashHatException {
if(!initQueryGet) {
waitForSamples = false;
// get id for the query
IntBuffer buffer = BufferUtils.createIntBuffer(1);
GL15.glGenQueries(buffer);
checkID = buffer.get(0);
initQueryGet = true;
}
if(!initialized) {
GL11.glEnable(GL11.GL_ALPHA_TEST);
GL11.glAlphaFunc(GL11.GL_GEQUAL, 1f);
GL11.glEnable(GL11.GL_STENCIL_TEST);
GL11.glColorMask(false, false, false, false);
initialized = true;
}else
throw new CrashHatException("begin was called twice (maybe a end is missing)");
}
/**
* Tests to images if there are intersecting.
*
* @param x1 the x position of the first image
* @param y1 the y position of the first image
* @param image1 the first image to check
* @param x2 the x position of the second image
* @param y2 the y position of the second image
* @param image2 the second image to check
* @return the number of pixels which are collide
* @throws CrashHatException if begin was not called.
*/
public int intersetcs(float x1, float y1, Image image1, float x2, float y2, Image image2) throws CrashHatException {
int tempPixels = -1;
if (!initialized)
throw new CrashHatException("collision not initialized! must call begin() before using intersets()");
// check if the rectangle intersect, helps to increase overall performance
if(!new Rectangle(x1,y1, image1.getWidth(), image1.getHeight()).intersects(new Rectangle(x2,y2, image2.getWidth(), image2.getHeight())))
return 0;
GL11.glClear(GL11.GL_STENCIL_BUFFER_BIT);
GL11.glStencilFunc(GL11.GL_ALWAYS, 1, 1);
GL11.glStencilOp(GL11.GL_REPLACE, GL11.GL_REPLACE, GL11.GL_REPLACE);
image1.draw(x1, y1);
GL11.glStencilFunc(GL11.GL_EQUAL, 1, 1);
GL11.glStencilOp(GL11.GL_KEEP, GL11.GL_KEEP, GL11.GL_KEEP);
GL15.glBeginQuery(GL15.GL_SAMPLES_PASSED, checkID);
image2.draw(x2, y2);
GL15.glEndQuery(GL15.GL_SAMPLES_PASSED);
if(waitForSamples) {
do {
GL15.glGetQueryObject(checkID, GL15.GL_QUERY_RESULT_AVAILABLE, pixelCounts);
}while(pixelCounts.get(0) == 0);
GL15.glGetQueryObject(checkID, GL15.GL_QUERY_RESULT, pixelCounts);
}else {
GL15.glGetQueryObject(checkID, GL15.GL_QUERY_RESULT_AVAILABLE, pixelCounts);
GL15.glGetQueryObject(checkID, GL15.GL_QUERY_RESULT, pixelCounts);
}
tempPixels = pixelCounts.get(0);
return tempPixels;
}
/**
* exits the collision test. must be called after <code>intersects</code>.
*
* @throws CrashHatException if end was called without an begin.
*/
public void end() throws CrashHatException {
if(initialized) {
GL11.glColorMask(true, true, true, true);
GL11.glDisable(GL11.GL_STENCIL_TEST);
GL11.glDisable(GL11.GL_ALPHA_TEST);
initialized = false;
}else
throw new CrashHatException("end was called twice (maybe a begin is missing)");
}
This works fine (if someone knows a better way to get the pixel count let me know!), problem is… it’s not working is the postions are negative… Any suggestions WHY this is happening? I can’t get around this, it’s really annoying.
R.D.