Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Function: WallIntersect (https://forums.graalonline.com/forums/showthread.php?t=134267065)

Crow 09-01-2012 10:32 PM

Function: WallIntersect
 
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 hitWallint stepsBeforeWallint totalStepsfloat 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(x1y1x2y2) {
  
temp.angle getangle(x2 x1y2 y1);
  
temp.dist  = ((x2 x1)^+ (y2 y1)^2)^0.5;
  
  
temp.maxSteps ceil(temp.dist);
  
temp.steps    0;
  for (
temp.0temp.maxStepsi++) {
    
temp.= (== temp.maxSteps temp.dist i);
    
    
temp.x1 cos(temp.angle) * (17 16 k);
    
temp.y1 sin(temp.angle) * (17 16 k);
    
    if (
onwall(temp.xtemp.y))
      return { 
truetemp.stepstemp.maxStepstemp.angle };
    else
      
temp.steps++;
  }
  
  return { 
falsetemp.stepstemp.maxStepstemp.angle };
}

function 
ceil(n)
  return (
int(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.

Tricxta 09-01-2012 11:32 PM

Quote:

Originally Posted by Crow (Post 1702936)
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, ...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.

Pretty nifty contribution, I can see this being good for a lot of novices who don't have half a clue about maths. Good job :)


All times are GMT +2. The time now is 09:21 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.