Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (537)
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  
  Translating XNA method to Slick2D  (Read 888 times)
0 Members and 1 Guest are viewing this topic.
Offline P0jahn

Senior Member


Projects: 3



« Posted 2013-04-03 23:04:59 »

There is a method by Microsoft which perform a pixel perfect collision detection on two sprites. The special thing about this function is that it supports rotations!
Here is the method:

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  
        /// <summary>
       /// Determines if there is overlap of the non-transparent pixels between two
       /// sprites.
       /// </summary>
       /// <param name="transformA">World transform of the first sprite.</param>
       /// <param name="widthA">Width of the first sprite's texture.</param>
       /// <param name="heightA">Height of the first sprite's texture.</param>
       /// <param name="dataA">Pixel color data of the first sprite.</param>
       /// <param name="transformB">World transform of the second sprite.</param>
       /// <param name="widthB">Width of the second sprite's texture.</param>
       /// <param name="heightB">Height of the second sprite's texture.</param>
       /// <param name="dataB">Pixel color data of the second sprite.</param>
       /// <returns>True if non-transparent pixels overlap; false otherwise</returns>
       public static bool IntersectPixels(
                            Matrix transformA, int widthA, int heightA, Color[] dataA,
                            Matrix transformB, int widthB, int heightB, Color[] dataB)
        {
            // Calculate a matrix which transforms from A's local space into
           // world space and then into B's local space
           Matrix transformAToB = transformA * Matrix.Invert(transformB);

            // When a point moves in A's local space, it moves in B's local space with a
           // fixed direction and distance proportional to the movement in A.
           // This algorithm steps through A one pixel at a time along A's X and Y axes
           // Calculate the analogous steps in B:
           Vector2 stepX = Vector2.TransformNormal(Vector2.UnitX, transformAToB);
            Vector2 stepY = Vector2.TransformNormal(Vector2.UnitY, transformAToB);

            // Calculate the top left corner of A in B's local space
           // This variable will be reused to keep track of the start of each row
           Vector2 yPosInB = Vector2.Transform(Vector2.Zero, transformAToB);

            // For each row of pixels in A
           for (int yA = 0; yA < heightA; yA++)
            {
                // Start at the beginning of the row
               Vector2 posInB = yPosInB;

                // For each pixel in this row
               for (int xA = 0; xA < widthA; xA++)
                {
                    // Round to the nearest pixel
                   int xB = (int)Math.Round(posInB.X);
                    int yB = (int)Math.Round(posInB.Y);

                    // If the pixel lies within the bounds of B
                   if (0 <= xB && xB < widthB &&
                        0 <= yB && yB < heightB)
                    {
                        // Get the colors of the overlapping pixels
                       Color colorA = dataA[xA + yA * widthA];
                        Color colorB = dataB[xB + yB * widthB];

                        // If both pixels are not completely transparent,
                       if (colorA.A != 0 && colorB.A != 0)
                        {
                            // then an intersection has been found
                           return true;
                        }
                    }

                    // Move to the next pixel in the row
                   posInB += stepX;
                }

                // Move to the next row
               yPosInB += stepY;
            }

            // No intersection found
           return false;
        }

I am familiar with java so I can translate most of the code myself. width, height and color data I have access to so we dont have to worry about them.
It is the Matrix and Transform classes that confuse me. What is their equivalence in Slick2D? Also, in my game, the rotation of the images is stored as an float.
Offline davedes
« Reply #1 - Posted 2013-04-03 23:28:28 »

Read up on linear algebra.
http://betterexplained.com/articles/linear-algebra-guide/
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/#An_introduction_to_matrices

Since Slick uses LWJGL, you can actually make use of Vector2f/Matrix4f/etc.

The real problem is that since Slick uses OpenGL for rendering, you won't have access to the color data without performing a copy from GPU to CPU (which can be slow).

Offline P0jahn

Senior Member


Projects: 3



« Reply #2 - Posted 2013-04-04 00:08:01 »

Do I really need to learn linear algebra? That would require a background of math to, which I dont have.
I checked the Slick2D api and I found a few interesting things but I am stuck at the first line in the method body:
Matrix transformAToB = transformA * Matrix.Invert(transformB);
How do I translate this?

The real problem is that since Slick uses OpenGL for rendering, you won't have access to the color data without performing a copy from GPU to CPU (which can be slow).
Dont worry about that.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #3 - Posted 2013-04-04 10:14:21 »

The real problem is that since Slick uses OpenGL for rendering, you won't have access to the color data without performing a copy from GPU to CPU (which can be slow).

Or you could keep a 'Pixmap' (<- LibGDX) storing all the data on the CPU from the beginning on. I think that's totally okay on today's hardware. Today we have enough RAM space to store two copies of texture data, once on the CPU Memory once on the GPU memory.

The only problem is that I don't have a clue of how to do that with Slick2D. I'd go with writing a simple Pixmap class storing an int[] array with color information from the loaded BufferedImage, which Slick can obviously read and convert to a ogl texture.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
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.

CogWheelz (17 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (20 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (42 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

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

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

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

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

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
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!