Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 09-01-2012, 10:32 PM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
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.
__________________
Reply With Quote
  #2  
Old 09-01-2012, 11:32 PM
Tricxta Tricxta is offline
The Muffin Man
Tricxta's Avatar
Join Date: Oct 2010
Location: Australia
Posts: 563
Tricxta is a jewel in the roughTricxta is a jewel in the rough
Quote:
Originally Posted by Crow View Post
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
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 11:03 PM.


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