Page 1 of 1
nearest wall
Posted: Mon Mar 12, 2007 4:14 pm
by darksmaster923
how can i find the nearest wall
Posted: Mon Mar 12, 2007 4:49 pm
by Juutis
I did it with this little piece of script:
Code: Select all
nearestwall = 10000;
nearestwalldir = 0;
for tmp = 0 to 18
{
wallangle = tmp*0.3490658504;
walldist = GetCollideDistance("check_upper",10000*sin(wallangle),0,10000*cos(wallangle));
if(walldist < nearestwall)
{
nearestwall = walldist + 0;
nearestwalldir = tmp + 0;
}
walldist = GetCollideDistance("check_lower",10000*sin(wallangle),0,10000*cos(wallangle));
if(walldist < nearestwall)
{
nearestwall = walldist + 0;
nearestwalldir = tmp + 0;
}
walldist = GetCollideDistance("check_feet",10000*sin(wallangle),0,10000*cos(wallangle));
if(walldist < nearestwall)
{
nearestwall = walldist + 0;
nearestwalldir = tmp + 0;
}
walldist = GetCollideDistance("check_head",10000*sin(wallangle),0,10000*cos(wallangle));
if(walldist < nearestwall)
{
nearestwall = walldist + 0;
nearestwalldir = tmp + 0;
}
}
The pawn has four 'check bones': 'check_upper', 'check_lower', 'check_feet' and 'check_head'. The script checks 18 different angles (= 'wallangle') starting from 0 radians (each is 0.3490658504 radians bigger than the previous). It checks if the distance to the wall in direction 'wallangle' is smaller than the previous distance to the nearest wall. It checks that seperately for each 'check bone'.
Finally, when the for-statement is finished, the variable called 'nearestwall' contains the distance to the nearest wall and 'nearestwalldir' contains the direction to the nearest wall (in imaginary units 0-18, where 18 is a full circle).
I hope this helps - I don't blame you if you don't understand, I can't explain it properly.

Posted: Mon Mar 12, 2007 9:03 pm
by shadow
Hi Juutis
Nice little bit of script there.
Not trying to hijack this thread, but why do you have ...10000*sin(wallangle), in GetCollideDistance?
Couldn't you just use the variable "wallangle" by itself
which is just 20 degree increments for the x value?
And you have 10000*cos(wallangle) for your y coordinate.
Wouldn't y be a fixed distance?
Do I have this wrong?
Posted: Mon Mar 12, 2007 9:10 pm
by Juutis
It's because the X, Y and Z values are the offsets from the bone. Not angles. Then a line is traced from the bone to the point defined by the offset and if there's a wall between the points, the distance is returned. If there is no wall, 'false' is returned. So the 'sin' and 'cos' are there because of the offset and '10000*...' is there so the line will hit a wall. If it was, say, 100, walls further away than 100 texels would produce a 'false'.
And actually, the Y value is 0. 10000*cos(wallangle) is the Z value.
Posted: Tue Mar 13, 2007 1:08 am
by darksmaster923
okay, cause for my script the player might glitch out of the wall so i need this.
edit
is there another way cuz this dont seem to be working
Posted: Tue Mar 13, 2007 8:18 am
by Juutis
There are other ways. This is just my way, and you can do it with your way.

I can't think of any simple way right now, however.
Posted: Tue Mar 13, 2007 2:13 pm
by shadow
Thanks Juutis
Its all clear now about the offset.
Funny I had used GetCollideDistance before with no problem, but only to look directly ahead of the pawn and had used GetCollideDistance( 0, 0, 1000) which works well for just checking ahead.
Now I should brush up on my sin and cos

Posted: Tue Mar 13, 2007 11:33 pm
by darksmaster923
okay, i used a simpler version without wall angles and it works

BUT i cant make the collsion detection in the dir the player is facing