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];
36  var x2 = x1 * Math.cos(inRads(theta)) -  y1 * Math.sin(inRads(theta));
37  var y2 = x1 * Math.sin(inRads(theta)) +  y1 * Math.cos(inRads(theta));
38
39  return [x2, y2];
40}
41
42// ///////////////////////////////////////////////////
43// Convert degrees to radians
44function inRads(degrees)
45{
46  return (Math.pi * degrees) / 180.0;
47}
48
49// ///////////////////////////////////////////////////
50// Returns the index of the row containing given value in given column
51// -1 if value was not found
52function arrayContains(array, column, value)
53{
54  if (array == null) return -1;
55  var i = 0;
56  for (i = 0; i < array.size; i++)
57  {
58    if (array.get(i) != null)
59      if (array.get(i).get(column) == value) return i;
60  }
61  return -1;
62}
Note: See TracBrowser for help on using the repository browser.