checking collision with the walls

The first thing I need to do for this walking on walls game is to make sure that I can check collision with the walls.

Right before I started programming it, I noticed that it would be very inefficient to check collision with all the walls in the level every frame, that would make collision checking very slow, but how do I know what walls to check against?

It's simple, thanks to the way I set up the walls in arrays, All I need to do is check what cell I am on and then that would give me an ID that I can use to check collision against only 4 walls.

And that is what I did, the only problem was that all the top cells don't have a roof, and all the left cells don't have a left wall, so in order to deal with this problem all I did was create a long left wall that all left cells will check against and a roof that all top cells will check against.

So simply by getting the current cell I was on I could do collision check, but when coding that part I thought that it would be very inefficient to check collision against every cell in order to know which one I am on. thankfully in this project the square set up helps me a lot, simply by using the position of the player I can determine what cell he is on, here is the code for that, just in case you were wondering:
var horizontalPos = Math.round((obj.x - xOffset)/cellWidth);
var verticalPos = Math.round(((obj.y - yOffset)/cellHeight))*cellsPerRow;

var finalNumber = horizontalPos+verticalPos;

now here is the swf, you can move the mouse around and look at the text box in the bottom, it tells you what wall it is colliding with.



you can move the mouse around the same way I explained in a previous post, think of moving this player along the walls and jumping from one to an other until you reach the top.

No comments:

Post a Comment