Java-Gaming.org
Java4K - to go         Javadoc:
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, peek at the official java tutorials or join us at irc #jgo.
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Pixel Precise collision not working with negative values.  (Read 396 times)
0 Members and 1 Guest are viewing this topic.
Offline R.D.

Jr. Member
**

Posts: 90
Medals: 2


"For the last time, Hats ARE Awesome"


« on: 2011-08-24 17:21:26 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
   /**
    * 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.

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.128 seconds with 21 queries.