Jump to content
Sign in to follow this  
[aps]gnat

2d maths help needed

Recommended Posts

Do'in me head in! .... I'm getting rusty.

Sources of data;

Turret direction: -180 through to 180. 0deg is north

Ship direction: 0 through to 360. 0deg is north

Need:

If the turret is pointing left of ships heading, need direction in deg of 90deg left of ships heading, expressed in the range -180 to 180

If the turret is pointing right of ships heading, need direction in deg of 90deg right of ships heading, expressed in the range -180 to 180.

This is my attempt, but it doesnt work for all directions a ship can face :(

_array = _ship weaponDirection "GNTExocetLauncher";
_dir1 = ((_array select 0) atan2 (_array select 1));
_dir2 = (getdir _ship);
if (_dir1 > _dir2 ) then
{
	_dir = (getdir _ship) + 90;
	_missobj SetPos (_ship ModelToWorld (_ship selectionposition "exocet2"));
} else {
	_dir = (getdir _ship) - 90;
	_missobj SetPos (_ship ModelToWorld (_ship selectionposition "exocet1"));
};

Thanks !

Share this post


Link to post
Share on other sites

I think this should work:

 
_dir_turret = ... // -180 to 180
_dir_ship = ... // 0 to 360
If ((_dir_turret + 180) > _dir_ship) then {
 // pointing to the right
 _dir = ((_dir_ship + 90) mod 360) - 180
} else {
 // pointing to the left or same direction
 _dir = (((_dir_ship - 90) + 360) mod 360) - 180
}

You should get values between -180 and 180.

Share this post


Link to post
Share on other sites

Here's a slight variant for getting the relative bearing from an object to a point....

//returns the vector from pos2 to pos1

// ie pos1-pos2

subtractPos ={

private ["_pos1","_pos2"] ;

_pos1 = _this select 0;

_pos2 = _this select 1;

[((_pos1) select (0)) - ((_pos2) select (0)), ((_pos1) select (1))-((_pos2) select (1))] ;

} ;

//returns the absolute bearing from pos1 to pos2

bearing = {

private ["_pos1","_pos2","_v","_b"] ;

_pos1 = _this select 0;

_pos2 = _this select 1;

_v= [_pos2,_pos1] call subtractPos ;

_b = (((_v) select (0)) atan2 ((_v) select (1))) ;

_b = (360+_b) % 360 ;

_b ;

} ;

//returns the relative bearing from an object to a position

relativeBearing={

private ["_o","_b"] ;

_o = (_this select 0) ;

_b = [getPos _o,(_this select 1)] call bearing;

_b = _b - (getDir _o) ;

if (_b > 180) then { _b = 360 - _b ;} ;

if (_b < -180) then { _b = 360 + _b ;} ;

_b ;

} ;

Share this post


Link to post
Share on other sites

Thanks guys, but unfortunately neither helped.

@HeliJunkie

That code seemed to create either 0 or 180, not +90 or -90 (relative to ship heading)

Share this post


Link to post
Share on other sites

I dont quite understand what exactly you're trying to do. Are you just trying to get relative turret direction compared to the hull (ie ship) its attached to?

_shipDir = ...
_turretDir = ...

if (_shipDir > 180) then { _shipDir = (_shipDir - 360) }; // Express the ship dir in -179 through 180

_relativeDir = (_turretDir - _shipDir); // Calculate the difference of the turret vs the ship

if (_relativeDir > 0) then {
// Turret looking right of ship
}
else { 
// Turret looking left of ship
}

Example 1: If the ship is heading in a -60 degree angle (ie northwest) and the turret is pointing at -10 degrees (almost north), then the relative angle is +50 degrees to the right of the ship.

Example 2: If the ship is heading in a +160 degree angle and the turret is point at +90, then the relative angle is -70 degrees to the left of the ship.

I am probably way off in what you want though, lol.

Edited by Murklor

Share this post


Link to post
Share on other sites

Close, but I need the heading either 90deg left or 90deg right as the output.

And was hoping to do it with pure maths, without a bunch of if-thens, so it might run a little faster.

Share this post


Link to post
Share on other sites

First let's translate turretDir into normal compass dir:

_newTurretDir = _turretDir mod 360;

Now they use the same system. both 0-360.

if (_newTurretDir > _shipDir) then {
 _theDir = _shipDir + 90) mod 360;
} else {
 _theDir = _shipDir - 90) mod 360;
};

Don't think it can be done much simpler. Not sure how "mod" functions in Arma but in Python's IDLE: -90 % 360 = 270.

EDIT: Nah wait guess that creates border cases :S.

Share this post


Link to post
Share on other sites

You wont be able to do it without an if as you need to compare the two directions somewhere along the line since you want it to say 90deg left or right (you could get the relative difference without if), think you're going to have to live with that ;)

Share this post


Link to post
Share on other sites
...

Not sure how "mod" functions in Arma but in Python's IDLE: -90 % 360 = 270.

...

I tested it with:

Windows Calculator: -90 mod 360 = -90

Arma2: -90 mod 360 = -90

Share this post


Link to post
Share on other sites
First let's translate turretDir into normal compass dir:

_newTurretDir = _turretDir mod 360;

Now they use the same system. both 0-360.

Nope that didnt work.

I've just about given up.

I've coded it so I'm now firing missiles out the right hand side of the ship aways ......

_dir = (getdir _ship) + 90; // fire off right side
if (_dir > 180) then {_dir = -(-(_dir - 180) + 180);}; // -180 to 180

Very clunky :(

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  

×