Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (622) 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 1121 times) 0 Members and 1 Guest are viewing this topic.
P0jahn

Senior Devvie

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

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: 121
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

You cannot reply to this message, because it is very, very old.

 BurntPizza (27 views) 2015-04-23 03:42:11 theagentd (29 views) 2015-04-22 16:23:07 Riven (44 views) 2015-04-16 10:48:47 Duke0200 (52 views) 2015-04-16 01:59:01 Fairy Tailz (38 views) 2015-04-14 20:13:12 Riven (41 views) 2015-04-12 21:36:37 bus hotdog (58 views) 2015-04-10 02:39:32 CopyableCougar4 (61 views) 2015-04-10 00:51:04 BurntPizza (60 views) 2015-04-06 22:06:58 ags1 (62 views) 2015-04-02 10:58:48
 theagentd 30x BurntPizza 16x wessles 15x 65K 11x Rayvolution 11x alwex 11x kingroka123 11x KevinWorkman 9x phu004 8x kevglass 8x ra4king 7x Ecumene 7x Roquen 7x chrislo27 7x Hanksha 7x Riven 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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