nearest wall

Topics regarding Scripting with Reality Factory
Post Reply
User avatar
darksmaster923
Posts: 1857
Joined: Wed Jan 03, 2007 10:32 pm
Location: Huntington Beach, California, USA

nearest wall

Post by darksmaster923 »

how can i find the nearest wall
Herp derp.
User avatar
Juutis
Posts: 1511
Joined: Thu Jan 12, 2006 12:46 pm
Location: Finland

Post 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. :?
Pain is only psychological.
shadow
Posts: 81
Joined: Mon Jul 25, 2005 5:37 am

Post 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?
User avatar
Juutis
Posts: 1511
Joined: Thu Jan 12, 2006 12:46 pm
Location: Finland

Post 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.
Pain is only psychological.
User avatar
darksmaster923
Posts: 1857
Joined: Wed Jan 03, 2007 10:32 pm
Location: Huntington Beach, California, USA

Post 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
Herp derp.
User avatar
Juutis
Posts: 1511
Joined: Thu Jan 12, 2006 12:46 pm
Location: Finland

Post 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.
Pain is only psychological.
shadow
Posts: 81
Joined: Mon Jul 25, 2005 5:37 am

Post 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 :)
User avatar
darksmaster923
Posts: 1857
Joined: Wed Jan 03, 2007 10:32 pm
Location: Huntington Beach, California, USA

Post by darksmaster923 »

okay, i used a simpler version without wall angles and it works :D
BUT i cant make the collsion detection in the dir the player is facing
Herp derp.
Post Reply