Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Author
Message
C0wbox
17
Years of Service
User Offline
Joined: 6th Jun 2006
Location: 0,50,-150
Posted: 21st Mar 2010 03:08 Edited at: 21st Mar 2010 03:11
Ok, I can check if the player is standing infront of a door fine.

I can't however, check if the player is standing behind a door...

I've just spent about 2 hours trying to get some kind of system working for any angle of door (0 < angle < 360) placed in my level to open when you're behind it and not when you're infront of it.

I know why the following code doesn't work, but I can't for the life of me find a solution:

Code without comments for people who understand what I'm doing:


Code with comments for people who don't know what I'm doing:


What should I be doing?

(Oh and making thousands of if statements doesn't seem to do me much good either... xD)
chafari
Valued Member
17
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 21st Mar 2010 03:44 Edited at: 21st Mar 2010 03:46
What are you trying to do exactly?...perhaps you want the door opens when someone approach to it, and it should do it towards the oposite side of the player !!?

You can know all the time where the player is...behind, in front or whereve he is, but you need some helpers, or make differents zone close the door to check if something is close to it.

Cheers!

I'm not a grumpy grandpa
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 21st Mar 2010 05:17
I would use vectors n stuff.


then if ang# is between 0 and 180, the player is in front of the door, otherwise the player is behind the door. If you want to check if the player is in a box around the door, you would need some rotations on the player's position.


C0wbox
17
Years of Service
User Offline
Joined: 6th Jun 2006
Location: 0,50,-150
Posted: 21st Mar 2010 06:49 Edited at: 21st Mar 2010 07:00
@ Neuro Fuzzy
It needed some tweaking but that principle worked. !

Thank you, you saved my doors.
Kira Vakaan
15
Years of Service
User Offline
Joined: 1st Dec 2008
Location: MI, United States
Posted: 21st Mar 2010 07:10 Edited at: 21st Mar 2010 07:15
Check out this example:


This uses more or less the same technique as Neuro Fuzzy's, but I hate the atanfull() command with a passion. This is more true to the math behind it and would work with any door oriented any way.

In vector 1, we store the normal of the door. Basically, that's just the direction in 3D space that the door is facing. Next, in vector 2, we store the vector from the door to the player by subtracting the door's position from the player's. After that, we normalize it, which just scales it down so that the length of the vector equals one (read on for why).

Next, a little math lesson. The dot product of vector A and B gives us: |A|*|B|*cos(Angle)
That can be read as: The length of the first vector times the length of the second vector times the cosine of the angle between them. Now, since both of our vectors have a length of 1, this is just the cosine of the angle between them. Using what we know about the cosine function, we can tell that when 0<Angle<90, 1>cos(Angle)>0, and when 90<Angle<180, 0<cos(Angle)<-1.

In summary, when the dot product of our two vectors is greater than zero, we know that the player is in front of the door, and when it's negative, we know that the player is behind it.

Whew! And so concludes the math lesson for the day!

Edit: It just occurred to me that we don't even have to normalize the second vector because even if we don't, the dot product will still be positive or negative. It just won't necessarily be the cosine of the angle between the vectors.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 22nd Mar 2010 14:43
...or position an invisible 'dummy' object on the correct side of the door and check your player's distance from it? Once you get close enough you know you are on the right side.

...or better still, when you model the door just offset it from the side you want the door to open from (see image attached)... that way when you check for the distance from the door you will only ever be close enough to the door when you are on the right side.



Attachments

Login to view attachments
C0wbox
17
Years of Service
User Offline
Joined: 6th Jun 2006
Location: 0,50,-150
Posted: 22nd Mar 2010 16:52
@ baxslash
Wow, I completely hadn't thought of the 2nd idea there. xD

But na I have an aversion to using dummy objects because it just makes my games messy and complicated when they don't really need to be. xD

Nevertheless, I now have working doors thanks to Neuro Fuzzy.

Login to post a reply

Server time is: 2024-04-25 01:46:04
Your offset time is: 2024-04-25 01:46:04