Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Translating XNA method to Slick2D  (Read 1781 times) 0 Members and 1 Guest are viewing this topic.
P0jahn

Senior Devvie

Medals: 1
Projects: 3

 « Posted 2013-04-03 21: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 `        ///         /// Determines if there is overlap of the non-transparent pixels between two        /// sprites.        ///         /// World transform of the first sprite.        /// Width of the first sprite's texture.        /// Height of the first sprite's texture.        /// Pixel color data of the first sprite.        /// World transform of the second sprite.        /// Width of the second sprite's texture.        /// Height of the second sprite's texture.        /// Pixel color data of the second sprite.        /// True if non-transparent pixels overlap; false otherwise        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.
davedes
 « Reply #1 - Posted 2013-04-03 21:28:28 »

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).

P0jahn

Senior Devvie

Medals: 1
Projects: 3

 « Reply #2 - Posted 2013-04-03 22: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).
matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

 « Reply #3 - Posted 2013-04-04 08: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

 nelsongames (17 views) 2018-04-24 18:15:36 nelsongames (13 views) 2018-04-24 18:14:32 ivj94 (587 views) 2018-03-24 14:47:39 ivj94 (50 views) 2018-03-24 14:46:31 ivj94 (400 views) 2018-03-24 14:43:53 Solater (65 views) 2018-03-17 05:04:08 nelsongames (110 views) 2018-03-05 17:56:34 Gornova (175 views) 2018-03-02 22:15:33 buddyBro (730 views) 2018-02-28 16:59:18 buddyBro (93 views) 2018-02-28 16:45:17
 ByerN 12x KaiHH 12x SHC 10x NuclearPixels 10x Zemlaynin 10x Guerra2442 10x Damocles 6x VaTTeRGeR 5x orangepascal 4x philfrei 4x ndnwarrior15 3x mesterh 3x ags1 3x CommanderKeith 2x dud3z 2x delt0r 2x
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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