## can you calculate the degree direction between two pieces?

Topics related to Vassal game modules.

Moderators: uckelman, Tim M

### can you calculate the degree direction between two pieces?

I need to be able to calculate the degree of angle between two x,y positions on a vassal map. Is that possible? If so how?
ChuckS

Posts: 120
Joined: August 16th, 2016, 4:37 pm

### Re: can you calculate the degree direction between two piece

I believe you could use Math.sin() and Math.cos() in a CalculatedProperty. Have a look at the new beta of Vassal 3.3.3, which has updated documentation for Expressions, which might be of some help.

Although too be clear this should work with earlier versions of Vassal too, but the documentation has been substantially improved in 3.3.3 Cattlesquat

Posts: 953
Joined: December 2nd, 2019, 4:57 pm
Location: Baltimore, Maryland, USA

### Re: can you calculate the degree direction between two piece

Actually, you would need Math.atan() and Math.toDegrees() for this application, but it should be possible. I believe Math.toDegrees(Math.atan((x2-x1)/(y2-y1))) would give you the number of degrees from vertical, if I'm getting my trigonometry right. The trick is how to get those coordinates into your formula....

Edit: And, of course, you'd have to worry about y2==y1, which would give you a divide-by-zero error....

So, probably do-able, but difficult.
jrwatts

Posts: 128
Joined: April 29th, 2020, 10:30 pm

### Re: can you calculate the degree direction between two piece

Oh yes, exactly - sorry - I'm used to knowing the angle and initial position, needing the new position, but you know both positions and need the angle.

Math.toDegrees(Math.atan2(y2 - y1, x2 - x1)) should do it without the divide-by-zero issue.

And then if it's negative add 360 Cattlesquat

Posts: 953
Joined: December 2nd, 2019, 4:57 pm
Location: Baltimore, Maryland, USA

### Re: can you calculate the degree direction between two piece

Thanks!! Looking to give a general degree location of the sub relative to the escort tracking it using the above and maybe build a randomized addition that reduces the closer the escort is to the submerged sub.
ChuckS

Posts: 120
Joined: August 16th, 2016, 4:37 pm

### Re: can you calculate the degree direction between two piece

So now the simple question, how do I reference the CurrentX/CurrentY of a piece not selected? Sorry just doesn't connect for me!
ChuckS

Posts: 120
Joined: August 16th, 2016, 4:37 pm

### Re: can you calculate the degree direction between two piece

There might be other ways depending on specific context, but a generic way would be:
(1) One piece writes its CurrentX and CurrentY to GlobalProperties (e.g. TargetCurrentX, TargetCurrentY, or something like that). To get it to write them, it may need a "Set Global Property" trait which you would then activate from your second piece (or somewhere) via a Global Key Command.
(2) Second piece then compares its CurrentX and CurrentY to TargetCurrentX and TargetCurrentY.

Brian Cattlesquat

Posts: 953
Joined: December 2nd, 2019, 4:57 pm
Location: Baltimore, Maryland, USA