Jump to content
Sign in to follow this  
tcann

Discrepencies with getDir and setDir with attachTo

Recommended Posts

I have a problem with a script I have been working on all day. The script allows a player to more or less freely move around in a vehicle, such as a helicopter, while it is moving. The script works in general, as long as the helicopter is at or near heading 0. The getDir on the player that fires when left, right, forward, or backwards is pressed gets the compass heading, as far as I can tell, but setDir only works relative to the helicopters direction, since I am using attachTo to keep the player inside. Are there any workarounds for this? I was inspired to do this script after I messed around with the ShackTak MH-6 enhancement, and that seems to work independently of the chopper's heading, so I'm sure it must be at least possible.

I have searched everywhere for an explanation to this, so I made an account to ask here. It seems odd it hasn't come up before. Normally I would have moved on at this point, but this is the best script I have ever done, and I'm in way too deep now.

Share this post


Link to post
Share on other sites

There is some explanation of the behaviour of setDir when used with attachTo here:

http://community.bistudio.com/wiki/attachTo

You can get the direction with this:

while {true} do {
hintSilent format ["dir= %1", round(getdir objectName)];
sleep 0.1;
};

Not sure what you are trying to achieve, can you explain it a little more clearly? Why do you need a different reference than the parent objects heading? It might be possible to use one of the BIS functions to get the directions you want or it may require a bit of maths as there is a relationship between the vehicle heading and compass directions.

eg:

BIS_fnc_relPos Returns a position that is a specified distance and compass direction from the passed position or object.

Parameters: [object or position, distance, direction]

Example: [player, 5, 100] call BIS_fnc_relPos

http://community.bistudio.com/wiki/BIS_fnc_dirTo

see http://community.bistudio.com/wiki/BIS_fnc_help for more.

Edited by PELHAM

Share this post


Link to post
Share on other sites

To explain a littel better, if a player is attached to a helicopter that is facing east (90) and the player is facing south (180), player setDir 180 will make the player face west (270). At this point, one would assume that getDir on the player should return 180, since that is where setDir put him, however, it returns west, 270. This means when I press the "A" key that I set to rotate left, it subtracts 20 from 270 then setDir puts the guy at 250. I am not at home now so I can't try your other suggestions there, but I think I recognize them as things I've tried. If you need more information let me know and I'll post it when I get home and I'm actually looking at the script.

Share this post


Link to post
Share on other sites

Yes simples - when attached setDir gets a new reference, the vehicle heading not the compass - setDir 180 will make him face the rear of the vehicle, in you case that is 270. To make him face south you need a setDir 90. Forget my other post, wasn't sure what you were after. See the notes in the 1st link.

Share this post


Link to post
Share on other sites

My question here is how do I calculate what I need to setDir to? I can't wrap my head around the math. I want to get the player's heading inside the vehicle with a command, then add or subtract 20 degrees depending on what key is pressed, and finally setDir. I just can't figure out how to determine the direction since setDir and getDir work differently with attachTo.

Share this post


Link to post
Share on other sites

Something like.....

player setdir ((getdir veh1)-(getdir player)) + _angle_from_your_keypress;

Edited by twirly
Clarity

Share this post


Link to post
Share on other sites

That makes me move to a seemingly random 20 degree increment. To clarify, pressing D for right from 0 degrees moves me to 20 as desired, but, a second press moves me back to zero. Left does the same.

Edited by TCann
More information

Share this post


Link to post
Share on other sites

How about don't use setDir - does setFormDir work ? the perameters are different so you have to use:

(group player) setFormDir 90;

Share this post


Link to post
Share on other sites
That makes me move to a seemingly random 20 degree increment. To clarify, pressing D for right from 0 degrees moves me to 20 as desired, but, a second press moves me back to zero. Left does the same.

Will have a look for you later if it's not sorted by then..... a bit busy at the moment. I'm pretty sure I can work it out.

Share this post


Link to post
Share on other sites

Unfortunately setFormDir does nothing, and the .rpt file has no errors, so I don't think it will work in this situation. Could the fact that setDir and getDir do different things with attachTo be a bug? I can't think of any situation where there is a benefit to them acting differently.

Share this post


Link to post
Share on other sites

Well me and mikie boy have spent a few hours scratching our heads on this one and came to a decision:

setDir is the wrong thing to use. The Pros from Dover at BIS probably use Vectors for this situation:

vectorDir

vectorUp

setVectorDir

setVectorDirAndUp

This function file looks promising - just substitute your values and place a functions module on the map. Possibly called with:

waituntil {!isnil "bis_fnc_init"};
_vec1 = vectorDir player;
_vec2 = [_vec1, 20] call BIS_fnc_rotateVector2D;
player setVectorDir _vec;

/************************************************************
Rotate 2D Vector
By Andrew Barron
Parameters: [[vector], angle]
Returns: [vector]
     This function returns a 2D vector rotated a specified number
     of degrees around the origin.
************************************************************/

private ["_v","_d","_x","_y"];

//extract parameters
_v = +(_this select 0); //we don't want to modify the originally passed vector
_d = _this select 1;

//extract old x/y values
_x = _v select 0;
_y = _v select 1;

//if vector is 3d, we don't want to mess up the last element
_v set [0, (cos _d)*_x - (sin _d)*_y];
_v set [1, (sin _d)*_x + (cos _d)*_y];

//return new vector
_v

Not sure if you know about the function viewer?

BIS_fnc_help

We also came up with a funny looking script that manually corrects setDir but the above is probably the correct way to go - I will PM that to you if you want.

Edited by PELHAM

Share this post


Link to post
Share on other sites

Unfortunately, setVectorDir only seems to change where the player object's head is pointing, but will not rotate the body, so I may need that other script.

Share this post


Link to post
Share on other sites

/****************************************************************
by Mikie boy and PELHAM
call compile preprocessFile "dirfunc.sqf";
_dir = [vehicleName,unitName] call fn_dir_func;
eg nul = [ch1,man1] call fn_dir_func;

funtion file to give correct setDir from getDir when vehicle attachTo other vehicle/object
see note 1: [url]http://community.bistudio.com/wiki/attachTo[/url]
*****************************************************************/
fn_dir_func = {
private ["_veh","_unit","_v","_p","_c","_dir"];
_veh = _this select 0;
_unit = _this select 1;

_v = getDir _veh;
_p = getDir _unit;
_c = 360;

_dir = _c-((_c-_p)-(_c-_v));

hintSilent ["%1",_dir]; //for debug, remove
_dir
};

You could preprocess that and call it and add the value to whatever you do on "keyDown".

Unit doesn't change direction which means _dir == getDir _unit (relative to _v)

To test it put this in a radio trigger, everytime you trigger he rotates 20 degrees clockwise:

direc = [ch1,man1] call fn_dir_func; man1 setdir direc+20;

AntiClockwise

direc = [ch1,man1] call fn_dir_func; man1 setdir direc-20;

Edited by PELHAM

Share this post


Link to post
Share on other sites

Is there a function to get dir where player is looking? if so can then set body to the same? - man this script better be good lol! Pelham had to dust off the olde scientific calculator lol. GOOD LUCK :).

Edited by Mikie boy

Share this post


Link to post
Share on other sites

Sweet guys, thanks a lot. I'm on my iPad right now but I'll implement it as soon as I get home. As far as the script being good... it will still have some things I need to work out once this is fixed, like how to prevent the player from simply walking outside the helicopter and float around while still attached! But after all this is done, I think it will be a very useful script for singleplayer and low player count coop missions.

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
Sign in to follow this  

×