Jump to content
alpha993

Getting whether door opens inward or outward

Recommended Posts

Hey everyone,

 

I'm making a script that allows you to kick open a door to make breaching slightly faster. However, I do not want players to be able to kick open doors that open towards them, as that would be rather strange to say the least. In order to accomplish this, I need a way to determine if a door will open away from or towards a player, depending on which side they're standing on.

 

I have the following script which works fine for some doors. It gets the coordinates of the door in the player's model space when it's closed and open, and then checks if the difference is negative. However, some doors seem to be mirrored in-game with their selection coordinates not really matching up with the in-game model. So when a door opens away from the player, the coordinates show that it should actually open towards the player.

 

_building = cursorObject;

_doorSelection = ((selectionNames _building) select {((_building modelToWorldVisual (_building selectionPosition _x)) distance2D player < 1.5) AND (("door" in toLower _x)) AND !(("handle" in toLower _x))}) select 0;

_doorAnimation = format ["%1_rot", _doorSelection];

_doorAnimationPhase = _building animationPhase _doorAnimation;

_doorCenterPosStart = player worldToModel (_building modelToWorld (_building selectionPosition _doorSelection));

_building animate [_doorAnimation,1,true];

_doorCenterPosEnd = player worldToModel (_building modelToWorld (_building selectionPosition _doorSelection));

_building animate [_doorAnimation,_doorAnimationPhase,true];

_opensInwards = ((_doorCenterPosStart select 1) - (_doorCenterPosEnd select 1) < 0);

 

Is there some other way to figure this out? Because at the moment the alternative is increasingly looking like I'll have to brute force it and make a list of every door in every building 😕

 

Any help would be greatly appreciated!

  • Like 1

Share this post


Link to post
Share on other sites

Funny that you ask because I made similar script once. Link to my door angle solution: 

I hope  there's all the code you need, it's been awhile when I worked with that

 

 

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

Thanks for the reply! I'll work with it soon and let you know how it goes.

Share this post


Link to post
Share on other sites
9 hours ago, gc8 said:

Funny that you ask because I made similar script once. Link to my door angle solution: 

I hope  there's all the code you need, it's been awhile when I worked with that

 

 

 

I didn't end up using your code per se, but it did help me arrive at a different solution which suited my needs, so thank you for that! 🙂

 

I'm going to post here my solution for finding whether a door opens away from or towards the player using it in case anybody else runs into similar issues. First though, let me illustrate the issue I was encountering a little more clearly.

 

In this first picture, you can see what is a normal door with its selections in the correct place. The green one corresponds to the door's axis (_building selectionPosition "door_1_axis"), the red one to the door's center position (_building selectionPosition "door_1_trigger"), and the blue one to the standard position (_building selectionPosition "door_1"). Ideally, you'd be able to tell in which direction a door opens by instantly opening and closing it with the animate command (the player will not see this as it's too fast), and getting the relative coordinates of the standard blue position to the player in both the closed and opened states. The difference between them will tell you if the door opens towards the player or away from the player depending on whether the difference is negative or positive.

 

Spoiler

DDPHx6u.jpg?1

 

This is all fine and dandy except for the fact that some doors are mirrored such as the one in the picture below. In this case, the blue position stays on the axis when the door is moved, reversing the result from taking the difference of the relative coordinates. In simpler terms, the code will say the door opens towards the player, when in fact it does not.

 

Spoiler

juB8SW1.jpg

 

Initially I thought about comparing 2D distances between the axis and the blue point to determine if a door was mirrored, but this turned out to be inconsistent. As a result, I turned to finding the angle between two 2D vectors, those being the vector from the axis to the red center, and the vector from the axis to the blue point. After some testing, I found that mirrored doors had angles greater than 50 degrees (Note that there might be exceptions still!), so I made the code check for this angle and then reverse the calculation if it detected the larger angles. So far, I have found it to be reliable enough for my purposes.

 

Here is the code I used with annotations. Hopefully it's a useful starting point or even a solution for anyone who requires it.

 

params ["_building","_doorSelection"]; // Define building and desired door

// COLLECT DOOR INFO

_doorCenter = _building selectionPosition format ["%1_trigger", _doorSelection]; // Get door center coordinates in building's model space
_doorAxis = _building selectionPosition format ["%1_axis", _doorSelection]; // Get door axis coordinates in building's model space
_doorMain = _building selectionPosition _doorSelection; // Get door standard position coordinates in building's model space
_doorAnimation = format ["%1_rot", _doorSelection]; // Get door animation name

_vectorAxisToCenter = _doorAxis vectorFromTo _doorCenter; // Get vector from door axis to door center
_vectorAxisToCenter set [2, 0];				// Make 2D vector

_vectorAxisToMain = _doorAxis vectorFromTo _doorMain;	// Get vector from door axis to door standard pos
_vectorAxisToMain set [2, 0];				// Make 2D vector

_angle = acos (_vectorAxisToCenter vectorCos _vectorAxisToMain);	// Get angle between vectors

// ANIMATE TO FIND DIRECTION

_doorAnimationPhase = _building animationPhase _doorAnimation; // Get current animation phase

// Get door standard position's coordinates in player's model space before animating
_doorMainPosStart = player worldToModelVisual (_building modelToWorldVisual (_building selectionPosition [_doorSelection, "Geometry"]));

_building animate [_doorAnimation,1,true]; // Open the door instantly

// Get door standard position's coordinates in player's model space after animating
_doorMainPosEnd = player worldToModelVisual (_building modelToWorldVisual (_building selectionPosition [_doorSelection, "Geometry"]));

_building animate [_doorAnimation,_doorAnimationPhase,true];  // Return the door to its original animation phase instantly

// DEFINE RETURN VARIABLE

_opensAwayFromPlayer = if (_angle < 50) then // Check for angle (door is mirrored if it is greater than 50 degrees)
{
	((_doorMainPosStart select 1) - (_doorMainPosEnd select 1) < 0); // If true, use regular formula
} 
else
{
	((_doorMainPosEnd select 1) - (_doorMainPosStart select 1) < 0); // If false, use inverted formula
};

_opensAwayFromPlayer // Return true if the door opens away from the player

 

  • Like 1

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×