Note: This function name is terrible. I'm not good at this.
WallIntersect(x1, y1, x2, y2)
What is this? Simple! I believe it was Dusty who was requesting this before: a function to check for walls on a line. This is a GS2 implemenation, obviously, so not as complex.
What does it do? It takes the coordinates for two points and checks for walls on a (dotted) line between them. One regular onwall() check about every tile length and a guaranteed check on the destination coordinate (that's the second one!). It also does what has been requested of this feature and (kinda) returns where it hit a wall. It will, however, not continue to check along the line for more walls once it encounters one.
How does it work? You feed it the two points and it will return an array in this format:
PHP Code:
{ boolean hitWall, int stepsBeforeWall, int totalSteps, float angle }
- hitWall is true if it hit a wall at some point, otherwise false
- stepsBeforeWall is the number of checks it did before it hit a wall; when hitWall is false, this is equal to totalSteps
- totalSteps is the amount of checks it has or would have done
- angle is the computed angle using the two coordinates and getangle(); so you don't have to calculate that again, heh~
What do I do now? Well, when you have the return values, you either didn't hit a wall and everything's fine. Or you hit a wall. You can then use the returned
stepsBeforeWall and
angle to calculate where you can move to savely without hitting a wall. It's not that accurate, but if you need it to be, you can make some additional checks.
PHP Code:
public function WallIntersect(x1, y1, x2, y2) {
temp.angle = getangle(x2 - x1, y2 - y1);
temp.dist = ((x2 - x1)^2 + (y2 - y1)^2)^0.5;
temp.maxSteps = ceil(temp.dist);
temp.steps = 0;
for (temp.i = 0; i < temp.maxSteps; i++) {
temp.k = (i == temp.maxSteps - 1 ? temp.dist - 1 : i);
temp.x = x1 + cos(temp.angle) * (17 / 16 + k);
temp.y = y1 - sin(temp.angle) * (17 / 16 + k);
if (onwall(temp.x, temp.y))
return { true, temp.steps, temp.maxSteps, temp.angle };
else
temp.steps++;
}
return { false, temp.steps, temp.maxSteps, temp.angle };
}
function ceil(n)
return (int(n) == n ? n : int(n) + 1);
ceil() included because it needs it. Duh~
Maybe this will be of use to some of you!
One more thing: this is mostly meant for players and other things with a hitbox that is 32x32 pixels. This is why I add 17 / 16 to the base of my onwall() checks. You can simply modify that if you need.