# source:experiments/frams/deathmatch/data/scripts/deathmatch-utils.inc@231

Last change on this file since 231 was 231, checked in by sz, 10 years ago

Deathmatch experiment - http://www.framsticks.com/deathmatch

File size: 1.6 KB
Line
1// ///////////////////////////////////////////////////
2// Computing angle between two vectors
3function vectorsAngle(v1, v2)
4{
5  var x1 = v1[0];
6  var y1 = v1[1];
7  var l1 = Math.sqrt(x1 * x1 + y1 * y1);
8
9  var x2 = v2[0];
10  var y2 = v2[1];
11  var l2 = Math.sqrt(x2 * x2 + y2 * y2);
12
13  var vProduct = x1 * y2 - y1 * x2;
14  var direction;
15
16  if (vProduct >= 0)
17    direction = 1.0;
18  else
19    direction = -1.0;
20
21  var cosinus = (x1 * x2 + y1 * y2) / (l1 * l2);
22  var angle = Math.acos(cosinus);
23
24  print("Vector1: [" + x1 + ", " + y1 + "] Vector2: " + x1 + ", " + y1 + "] " +
25        "Angle: " + (180.0 * angle / Math.pi) + " Direction: " + direction);
26
27  return direction * angle;
28}
29
30// ///////////////////////////////////////////////////
31// Computing rotated vector v1 by angle theta
32function rotateVector(v1, theta)
33{
34  var x1 = v1[0];
35  var y1 = v1[1];
38
39  return [x2, y2];
40}
41
42// ///////////////////////////////////////////////////
45{
46  return (Math.pi * degrees) / 180.0;
47}
48
49// ///////////////////////////////////////////////////
50// Returns the index of the row containing given value in given column