Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
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 901 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 21: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]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

Pippogeek (40 views)
2014-09-24 16:13:29

Pippogeek (31 views)
2014-09-24 16:12:22

Pippogeek (21 views)
2014-09-24 16:12:06

Grunnt (47 views)
2014-09-23 14:38:19

radar3301 (29 views)
2014-09-21 23:33:17

BurntPizza (65 views)
2014-09-21 02:42:18

BurntPizza (37 views)
2014-09-21 01:30:30

moogie (44 views)
2014-09-21 00:26:15

UprightPath (53 views)
2014-09-20 20:14:06

BurntPizza (55 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!