- Home /
I drew a 2d Maze. How to collide with walls.
Hi,
A top down game where I move a guy through a maze. I drew a maze in paint and imported it in as a sprite (think pacman looking level)
I couldn't think of a way to make my player trigger events when colliding with the walls. I cant use 2d polygon collider as it tries to look at the sprite as a whole.
Ways I've thought, but I thought I'd just ask.
-Slice the sprite in the sprite editor, but I'd have to draw boxes over every single line.
-Research if raycasts can detect a certain colour. (I haven't learnt raycasts yet)
-Learn blender and use the maze image as a guide and plonk cubes on it to make a 3d version of it.
-Recreate the maze in game using just a line sprite.
Is there an easier way?
Thanks in advance.
Answer by frogsbo · Mar 14, 2014 at 06:41 AM
you have to figure out a collision maths that suits you. you probably mean you want the walls to be solid, you dont want to collide like bounce away at same speed you hit and in different direction, which takes more maths.
to make solid, just check position to see if it is in white or black, or check points aorund position to see if any is in black... if it is in black, change position to be +x and or +y the same so he is still moving forward up or left, but change one of the x or y so that he is not advancing into wall.
to make a 3d version in cubes, you can measure the grid of your maze, and then check every space on grid bitmap if is black or white, and build a grid of cubes, using 7-8 lines of code...
for(loop x in grid length){
for (loop y in grid height){
if space xy readpixel of bitmap is black(bitmap.red is<.5), make a square there)
}
}
Hi Frogsbo, thanks heaps for answering. I'm just wanting to have if my character touches one of the walls then a trigger event happens, I'm not going to be using physics.
To more clearly define my problem is the maze i made is one single huge image that is imported as a single sprite. Thus I can't make use of any of the 2d colliders on it as they won't map out each of the lines of a single sprite but rather the whole outside of the entire maze.
I'll try the readpixel bitmap.red thing! thanks
Thanks again for your help
Your answer
Follow this Question
Related Questions
How do you create complex collision geometry to match 2D sprites? 2 Answers
Player Destroy on Collision? 0 Answers
Generating 2D collisions for maze 0 Answers
How do I check if/where in a sprite another sprite is? 0 Answers
Changing sprites from spritesheet on collision. What am I doing wrong? 1 Answer