TuioObject.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. TUIO C# Library - part of the reacTIVision project
  3. http://reactivision.sourceforge.net/
  4. Copyright (c) 2005-2009 Martin Kaltenbrunner <mkalten@iua.upf.edu>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. using System;
  18. using System.Collections.Generic;
  19. namespace TUIO
  20. {
  21. /**
  22. * The TuioObject class encapsulates /tuio/2Dobj TUIO objects.
  23. *
  24. * @author Martin Kaltenbrunner
  25. * @version 1.4
  26. */
  27. public class TuioObject : TuioContainer
  28. {
  29. /**
  30. * The individual symbol ID number that is assigned to each TuioObject.
  31. */
  32. protected int symbol_id;
  33. /**
  34. * The rotation angle value.
  35. */
  36. protected float angle;
  37. /**
  38. * The rotation speed value.
  39. */
  40. protected float rotation_speed;
  41. /**
  42. * The rotation acceleration value.
  43. */
  44. protected float rotation_accel;
  45. /**
  46. * Defines the ROTATING state.
  47. */
  48. public static readonly int TUIO_ROTATING = 5;
  49. /**
  50. * This constructor takes a TuioTime argument and assigns it along with the provided
  51. * Session ID, Symbol ID, X and Y coordinate and angle to the newly created TuioObject.
  52. *
  53. * @param ttime the TuioTime to assign
  54. * @param si the Session ID to assign
  55. * @param sym the Symbol ID to assign
  56. * @param xp the X coordinate to assign
  57. * @param yp the Y coordinate to assign
  58. * @param a the angle to assign
  59. */
  60. public TuioObject(TuioTime ttime, long si, int sym, float xp, float yp, float a)
  61. : base(ttime, si, xp, yp)
  62. {
  63. symbol_id = sym;
  64. angle = a;
  65. rotation_speed = 0.0f;
  66. rotation_accel = 0.0f;
  67. }
  68. /**
  69. * This constructor takes the provided Session ID, Symbol ID, X and Y coordinate
  70. * and angle, and assigs these values to the newly created TuioObject.
  71. *
  72. * @param si the Session ID to assign
  73. * @param sym the Symbol ID to assign
  74. * @param xp the X coordinate to assign
  75. * @param yp the Y coordinate to assign
  76. * @param a the angle to assign
  77. */
  78. public TuioObject(long si, int sym, float xp, float yp, float a)
  79. : base(si, xp, yp)
  80. {
  81. symbol_id = sym;
  82. angle = a;
  83. rotation_speed = 0.0f;
  84. rotation_accel = 0.0f;
  85. }
  86. /**
  87. * This constructor takes the atttibutes of the provided TuioObject
  88. * and assigs these values to the newly created TuioObject.
  89. *
  90. * @param tobj the TuioObject to assign
  91. */
  92. public TuioObject(TuioObject tobj)
  93. : base(tobj)
  94. {
  95. symbol_id = tobj.getSymbolID();
  96. angle = tobj.getAngle();
  97. rotation_speed = 0.0f;
  98. rotation_accel = 0.0f;
  99. }
  100. /**
  101. * Takes a TuioTime argument and assigns it along with the provided
  102. * X and Y coordinate, angle, X and Y velocity, motion acceleration,
  103. * rotation speed and rotation acceleration to the private TuioObject attributes.
  104. *
  105. * @param ttime the TuioTime to assign
  106. * @param xp the X coordinate to assign
  107. * @param yp the Y coordinate to assign
  108. * @param a the angle coordinate to assign
  109. * @param xs the X velocity to assign
  110. * @param ys the Y velocity to assign
  111. * @param rs the rotation velocity to assign
  112. * @param ma the motion acceleration to assign
  113. * @param ra the rotation acceleration to assign
  114. */
  115. public void update(TuioTime ttime, float xp, float yp, float a, float xs, float ys, float rs, float ma, float ra)
  116. {
  117. base.update(ttime, xp, yp, xs, ys, ma);
  118. angle = a;
  119. rotation_speed = rs;
  120. rotation_accel = ra;
  121. if ((rotation_accel != 0) && (state != TUIO_STOPPED)) state = TUIO_ROTATING;
  122. }
  123. /**
  124. * Assigns the provided X and Y coordinate, angle, X and Y velocity, motion acceleration
  125. * rotation velocity and rotation acceleration to the private TuioContainer attributes.
  126. * The TuioTime time stamp remains unchanged.
  127. *
  128. * @param xp the X coordinate to assign
  129. * @param yp the Y coordinate to assign
  130. * @param a the angle coordinate to assign
  131. * @param xs the X velocity to assign
  132. * @param ys the Y velocity to assign
  133. * @param rs the rotation velocity to assign
  134. * @param ma the motion acceleration to assign
  135. * @param ra the rotation acceleration to assign
  136. */
  137. public void update(float xp, float yp, float a, float xs, float ys, float rs, float ma, float ra)
  138. {
  139. base.update(xp, yp, xs, ys, ma);
  140. angle = a;
  141. rotation_speed = rs;
  142. rotation_accel = ra;
  143. if ((rotation_accel != 0) && (state != TUIO_STOPPED)) state = TUIO_ROTATING;
  144. }
  145. /**
  146. * Takes a TuioTime argument and assigns it along with the provided
  147. * X and Y coordinate and angle to the private TuioObject attributes.
  148. * The speed and accleration values are calculated accordingly.
  149. *
  150. * @param ttime the TuioTime to assign
  151. * @param xp the X coordinate to assign
  152. * @param yp the Y coordinate to assign
  153. * @param a the angle coordinate to assign
  154. */
  155. public void update(TuioTime ttime, float xp, float yp, float a)
  156. {
  157. TuioPoint lastPoint = path[path.Count - 1];
  158. base.update(ttime, xp, yp);
  159. TuioTime diffTime = currentTime - lastPoint.getTuioTime();
  160. float dt = diffTime.getTotalMilliseconds() / 1000.0f;
  161. float last_angle = angle;
  162. float last_rotation_speed = rotation_speed;
  163. angle = a;
  164. float da = (angle - last_angle) / (2.0f * (float)Math.PI);
  165. if (da > 0.75f) da -= 1.0f;
  166. else if (da < -0.75f) da += 1.0f;
  167. rotation_speed = da / dt;
  168. rotation_accel = (rotation_speed - last_rotation_speed) / dt;
  169. if ((rotation_accel != 0) && (state != TUIO_STOPPED)) state = TUIO_ROTATING;
  170. }
  171. /**
  172. * Takes the atttibutes of the provided TuioObject
  173. * and assigs these values to this TuioObject.
  174. * The TuioTime time stamp of this TuioContainer remains unchanged.
  175. *
  176. * @param tobj the TuioContainer to assign
  177. */
  178. public void update(TuioObject tobj)
  179. {
  180. base.update(tobj);
  181. angle = tobj.getAngle();
  182. rotation_speed = tobj.getRotationSpeed();
  183. rotation_accel = tobj.getRotationAccel();
  184. if ((rotation_accel != 0) && (state != TUIO_STOPPED)) state = TUIO_ROTATING;
  185. }
  186. /**
  187. * This method is used to calculate the speed and acceleration values of a
  188. * TuioObject with unchanged position and angle.
  189. */
  190. public new void stop(TuioTime ttime)
  191. {
  192. update(ttime, this.xpos, this.ypos, this.angle);
  193. }
  194. /**
  195. * Returns the symbol ID of this TuioObject.
  196. * @return the symbol ID of this TuioObject
  197. */
  198. public int getSymbolID()
  199. {
  200. return symbol_id;
  201. }
  202. /**
  203. * Returns the rotation angle of this TuioObject.
  204. * @return the rotation angle of this TuioObject
  205. */
  206. public float getAngle()
  207. {
  208. return angle;
  209. }
  210. /**
  211. * Returns the rotation angle in degrees of this TuioObject.
  212. * @return the rotation angle in degrees of this TuioObject
  213. */
  214. public float getAngleDegrees()
  215. {
  216. return angle / (float)Math.PI * 180.0f;
  217. }
  218. /**
  219. * Returns the rotation speed of this TuioObject.
  220. * @return the rotation speed of this TuioObject
  221. */
  222. public float getRotationSpeed()
  223. {
  224. return rotation_speed;
  225. }
  226. /**
  227. * Returns the rotation acceleration of this TuioObject.
  228. * @return the rotation acceleration of this TuioObject
  229. */
  230. public float getRotationAccel()
  231. {
  232. return rotation_accel;
  233. }
  234. /**
  235. * Returns true of this TuioObject is moving.
  236. * @return true of this TuioObject is moving
  237. */
  238. public new bool isMoving()
  239. {
  240. if ((state == TUIO_ACCELERATING) || (state == TUIO_DECELERATING) || (state == TUIO_ROTATING)) return true;
  241. else return false;
  242. }
  243. }
  244. }