Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (784) Games in Android Showcase (234) games submitted by our members Games in WIP (858) 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 2006 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 »

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

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).
Dont worry about that.
 Games published by our own members! Check 'em out!
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

 hadezbladez (1636 views) 2018-11-16 13:46:03 hadezbladez (649 views) 2018-11-16 13:41:33 hadezbladez (1633 views) 2018-11-16 13:35:35 hadezbladez (339 views) 2018-11-16 13:32:03 EgonOlsen (2685 views) 2018-06-10 19:43:48 EgonOlsen (2952 views) 2018-06-10 19:43:44 EgonOlsen (1648 views) 2018-06-10 19:43:20 DesertCoockie (2358 views) 2018-05-13 18:23:11 nelsongames (2266 views) 2018-04-24 18:15:36 nelsongames (2959 views) 2018-04-24 18:14:32
 Deployment and Packagingby philfrei2019-02-17 20:25:53Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20
 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