# 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?

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

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.

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

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.

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

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