Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Pixel Precise collision not working with negative values.  (Read 691 times)
0 Members and 1 Guest are viewing this topic.
Offline R.D.

Senior Member


Medals: 2
Projects: 1


"For the last time, Hats ARE Awesome"


« Posted 2011-08-24 23: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.

Mr. Hat I
- RadicalFish Engine - Ideas, Bugs? Open an Issue ticket!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (88 views)
2013-05-17 21:29:12

alaslipknot (96 views)
2013-05-16 21:24:48

gouessej (128 views)
2013-05-16 00:53:38

gouessej (123 views)
2013-05-16 00:17:58

theagentd (131 views)
2013-05-15 15:01:13

theagentd (119 views)
2013-05-15 15:00:54

StreetDoggy (161 views)
2013-05-14 15:56:26

kutucuk (184 views)
2013-05-12 17:10:36

kutucuk (185 views)
2013-05-12 15:36:09

UnluckyDevil (191 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.066 seconds with 20 queries.