libgdx intersects color?

Hi i’m learning the libGDX framework and i’m wondering is there some sort of intersects color method?
I have a Rectangle hitbox on a character and i want it to collide with everything but the floor and the floor will always be this constant color (brown) and everything else with be some other color red, green, blue, etc.

Any ideas on how i can achieve this would be great. Thanks! :slight_smile:

Edit: is there anyway at all one can detect the color (of a background Texture) at any given vector on the screen?

I really advise against using graphics to do your logic. Maintain a logical representation of your objects and perform logic on those, rendering is just how they are shown.

Hi,

you should ALWAYS separate the logic and the view (visual representation) of your game.
That means, you should somehow store your Entitys type + it’s hitbox (the Rectangle).

When drawing, you can use this type to draw the Rectangle with the right color:

if (entity.type == Entities.FLOOR)
    color = Color.MAROON;
else
    ...

Doing this, can make thigs, such as changing the view (maybe you want to have a 2.5D or even 3D view some time, or you only want to change from static Textures to Animations), much easier.

thanks for the advice i was just looking for a way other than hard coding it all, i guess the only other way for me to do it is with either a tile map or hard coding some polygons to the walls for the collision. But then how would you do it if it’s procedurally generated? my game is not but i’m curious :clue:

Does it make a difference, if it is procedural generated or hardcoded?
You always have to store your Map/Level data somewhere. This data can then be used for collision detection and rendering.

In your case, you should have a class “Entity”, which stores the type (FLOOR, CHARACTER…) and it’s hitbox (the Rectangle).
The you could have a class “World”, which stores a list of all “Entities”.
For collision detection you can iterate through all the Entities (in a nested for-loop) and check, if they can collide (you said character can’t collide with floor) and if they can, check if they do.
It’s not the most efficient way, but i guess, in your case it’s just efficient enough.

For rendering you can iterate through all Entities and just draw a Rectangle, using the dimensions of their hitbox and the color, associated to their type.

Never use graphics to dictate game logic.

You should have a list of tiles… regardless if you hardcode the map, procedurally generate it, or load it from an image etc etc.

In your tile class you can simply have an “isBlocked” flag or something, then from there just grab the players current X and current Y, and when the player is moving up, down, left, right or where ever, simply check if the players next move will collide with an isBlocked tile, and if it does don’t move them there.

It isn’t super difficult, research it a bit more and I’m positive you’ll come across something that explains it pretty well.

Use a bitmap image to represent your level and load that in. Then you can use colors from the image to build you map.

Thank you all! I think i have found a way to do it by in each room hardcode a polyline around the edges and create collision based on that. Also i guess that the only way to do random generation is with a tile based system, but i’m too lazy to do that :slight_smile: thanks for your advice ill just hardcode the thing.