This is the screenshot of my game.

Game

This is a 2d platformer game where the player jumps to many differently colored tiles. On the left-hand top corner, there is a square tile which changes its color from blue to green to red. How can i get the color of the pixel inside the square box?I need it for collision detection between player and platform. I am getting some pixel integer value which I put in if statement but it doesnt seem to work.I need this because the player has to change the color of the tile before jumping onto next colored tile of blocks.

for(int i=5;i<8;i++) {
            for (MapObject object : tileMap.getLayers().get(i).getObjects()) {

                Shape shape;
                if (object instanceof RectangleMapObject) {
                    shape = getRectangle((RectangleMapObject) object);
                } else if (object instanceof PolygonMapObject) {
                    shape = getPolygon((PolygonMapObject) object);
                } else if (object instanceof PolylineMapObject) {
                    shape = getPolyline((PolylineMapObject) object);
                } else if (object instanceof CircleMapObject) {
                    shape = getCircle((CircleMapObject) object);
                } else {
                    continue;
                }
                BodyDef bdef = new BodyDef();

                FixtureDef fdef = new FixtureDef();
                Body body;
                bdef.type = BodyDef.BodyType.StaticBody;
                float x = Float.parseFloat((object.getProperties().get("x").toString())) / PPM;
                float y = Float.parseFloat((object.getProperties().get("y").toString())) / PPM;

                body = world.createBody(bdef);
                fdef.shape = shape;
                Texture blocktex = MyMainGame.con.getTexture("blocks");
                blocks = new TextureRegion[3];
                for (int b = 0; b < blocks.length; b++) {
                    blocks = TextureRegion.split(blocktex, 32, 32)[0];
                }
                Texture texture = blocks[i-5].getTexture();
                if (!texture.getTextureData().isPrepared()) {
                    texture.getTextureData().prepare();
                }
                Pixmap pixmap = texture.getTextureData().consumePixmap();
                sb.setProjectionMatrix(displayCam.combined);
                for (int a = 0; a < 3; a++) {
                    for (int b = 0; b < 3; b++) {

                        int colorInt = pixmap.getPixel(blocks[i-5].getRegionX()+a, blocks[i-5].getRegionX()+b);
                        System.out.println(colorInt);

                        if( (i == 5 && object.getColor()==Color.RED) || colorInt == -316922625) {
                            fdef.isSensor = false;                                                       // red-red
                            isblocked = true;
                        }
                       else if ((i == 5 && object.getColor()==Color.RED) || colorInt == -1243210241) {
                            fdef.isSensor = true;                                                          //red -green
                        }
                        if ((i == 5 && object.getColor()==Color.RED) || colorInt == 10676479) {
                            fdef.isSensor = true;                                                          //red -blue
                        }
                        if ((i == 6 && object.getColor()==Color.GREEN) || colorInt == -1243210241){         //green -green
                            fdef.isSensor = false;
                            //fdef.filter.categoryBits = BIT_RED;
                            //  fdef.filter.maskBits = BIT_PLAYER;
                        }
                        if ((i == 6 && object.getColor()==Color.GREEN) || colorInt == -316922625) {          //green-red
                            fdef.isSensor = true;
                        }

                        if ((i == 6 && object.getColor()==Color.GREEN) || colorInt == 10676479) {          //green-blue
                            fdef.isSensor = true;
                        }
                        if ((i == 7 && object.getColor()==Color.BLUE) || colorInt == 10676479) {            //blue -blue
                            fdef.isSensor = false;
                            // fdef.filter.categoryBits = BIT_GREEN;
                            // fdef.filter.maskBits = BIT_PLAYER;
                        }
                        if ((i == 7 && object.getColor()==Color.BLUE) || colorInt == -316922625) {     //blue-red
                            fdef.isSensor = true;
                        }

                        if ((i == 7 && object.getColor()==Color.BLUE) || colorInt == -1243210241) {     //blue-green
                            fdef.isSensor = true;
                        }

                        System.out.println(colorInt);

                        fdef.friction = 0f;

                        body = world.createBody(bdef);
                        body.createFixture(fdef);
                        body.setUserData("ground");


                    }

                }
            }
        }

From reading your question, you want to: allow the player to only stand on blocks that have the same colour as the top-left square

The approach you describe is unnecessarily roundabout in order to achieve what you're aiming at.


Instead of sampling the colour of the top-left square after rendering it, you should store a state representing the colour (or really any other arbitary value) that is currently safe.

Then you can assign the same state/value to platform tiles and when doing collision detection you can simply check if the tile-state equals the current square-state.

incomplete, simple pseudocode to demonstrate:

//Somewhere to store what tiles can be collided with
public final class MetaData {
    public static int currentSquareState = 1;
}

...
    //Check if we can collide with the given tile
    public boolean checkCollision(Tile tile) {
        if(tile.state == MetaData.currentSquareState) {
            return true;
        }

        return false;
    }
...
    //Change what tile we can collide with
    MetaData.currentSquareState = 3;
...

So you can get the color of a Pixel from Texture:

public Color getColorOfPixel(Texture texture, int pixelPosX, int pixelPosY){
    if(!texture.getTextureData().isPrepared()){
        texture.getTextureData().prepare(); //First you must prepare the texture data before you can call consumePixmap()
    }
    Pixmap pixmap = texture.getTextureData().consumePixmap();
    return new Color(pixmap.getPixel(pixelPosX, pixelPosY));
}

Color c = getColorOfPixel(new Texture(Gdx.files.internal("badlogic.jpg")), 128,128);
System.out.println("Color: " + c.r + ", " + c.g + ", " + c.b);
//Output: "Color: 1.0, 1.0, 0.0" (yellow)

For TextureRegion:

Texture t = new Texture(Gdx.files.internal("badlogic.jpg"));
TextureRegion r = new TextureRegion(t, 30, 30, 30, 30);
Color c = getColorOfPixel(r.getTexture(), 15,15);
System.out.println("Color: " + c.r + ", " + c.g + ", " + c.b);
//Output: "Color: 0.38039216, 0.34509805, 0.0" (some green brown)

look also at this question: https://stackoverflow.com/questions/28405361/libgdx-get-pixel-color-from-sprite-or-texture

  • I dont want to add a texture like you did.I am seperating Texture into Texture regions and seperating a three coloured different tiles into one tile in Texture region.How do I extract Colour from Texture region? – satya Aug 10 at 20:45
  • @satya I edit my question, and add code for TextureRegion – Morchul yesterday
  • Downvoted as you're answering the 'question' but not addressing the actual 'problem/issue' the querent tries to solve – dot_Sp0T yesterday
  • @dot_Sp0T downvoted for answering the question? Lol, that's funny. I think for that is a Q&A site for answering the question. And his question was: How can i get the color of the pixel inside the square box? – Morchul yesterday
  • I know it sounds weird, that is why I made the comment instead of not explaining at all, and that is why I take the time to explain now as well: The goal of this site is to A) create a useful archive of high quality Q&A so people can find solutions to their problems not only by asking but also by searching; B) to help users solve the problems they encounter when creating videogames. Regarding the latter, the problem 'stated' is to get the render-colour of something on screen. But when starting to read the question you realise that their actual problem is variable collision detection based on – dot_Sp0T yesterday

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.