entityexts.monkey2 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. Namespace mojo3d
  2. Private
  3. Const DegreesToRadians:=Pi/180.0
  4. Const RadiansToDegrees:=180.0/Pi
  5. Public
  6. #rem monkeydoc Utility extension methods for entities.
  7. #end
  8. Class Entity Extension
  9. #rem monkeydoc The rigid body attached to the entity.
  10. #end
  11. Property RigidBody:RigidBody()
  12. Return GetComponent<RigidBody>()
  13. End
  14. #rem monkeydoc The collider attached to the entity.
  15. #end
  16. Property Collider:Collider()
  17. Return GetComponent<Collider>()
  18. End
  19. #rem monkeydoc The joint attached to the entity.
  20. #end
  21. Property Joint:Joint()
  22. Return GetComponent<Joint>()
  23. End
  24. #rem monkeydoc The animator attached to the entity.
  25. #end
  26. Property Animator:Animator()
  27. Return GetComponent<Animator>()
  28. End
  29. #rem monkeydoc World space rotation in degrees.
  30. #end
  31. Property Rotation:Vec3f()
  32. Return Basis.GetRotation() * RadiansToDegrees
  33. Setter( rotation:Vec3f )
  34. Basis=Mat3f.Rotation( rotation * DegreesToRadians )
  35. End
  36. #rem monkeydoc Local space rotation in degrees.
  37. #end
  38. Property LocalRotation:Vec3f()
  39. Return LocalBasis.GetRotation() * RadiansToDegrees
  40. Setter( rotation:Vec3f )
  41. LocalBasis=Mat3f.Rotation( rotation * DegreesToRadians )
  42. End
  43. #Rem monkeydoc World space rotation around the X axis in degrees.
  44. #End
  45. Property Rx:Float()
  46. Return Rotation.x
  47. Setter( rx:Float )
  48. Local r:=Rotation
  49. Rotation=New Vec3f( rx,r.y,r.z )
  50. End
  51. #Rem monkeydoc World space rotation around the Y axis in degrees.
  52. #End
  53. Property Ry:Float()
  54. Return Rotation.y
  55. Setter( ry:Float )
  56. Local r:=Rotation
  57. Rotation=New Vec3f( r.x,ry,r.z )
  58. End
  59. #Rem monkeydoc World space rotation around the Z axis in degrees.
  60. #End
  61. Property Rz:Float()
  62. Return Rotation.z
  63. Setter( rz:Float )
  64. Local r:=Rotation
  65. Rotation=New Vec3f( r.x,r.y,rz )
  66. End
  67. #Rem monkeydoc Local space rotation around the X axis in degrees.
  68. #End
  69. Property LocalRx:Float()
  70. Return LocalRotation.x
  71. Setter( rx:Float )
  72. Local r:=LocalRotation
  73. LocalRotation=New Vec3f( rx,r.y,r.z )
  74. End
  75. #Rem monkeydoc Local space rotation around the Y axis in degrees.
  76. #End
  77. Property LocalRy:Float()
  78. Return LocalRotation.y
  79. Setter( ry:Float )
  80. Local r:=LocalRotation
  81. LocalRotation=New Vec3f( r.x,ry,r.z )
  82. End
  83. #Rem monkeydoc Local space rotation around the Z axis in degrees.
  84. #End
  85. Property LocalRz:Float()
  86. Return LocalRotation.z
  87. Setter( rz:Float )
  88. Local r:=LocalRotation
  89. LocalRotation=New Vec3f( r.x,r.y,rz )
  90. End
  91. #rem monkeydoc World space X coordinate.
  92. #end
  93. Property X:Float()
  94. Return Position.x
  95. Setter( x:Float )
  96. Local v:=Position
  97. Position=New Vec3f( x,v.y,v.z )
  98. End
  99. #rem monkeydoc World space Y coordinate.
  100. #end
  101. Property Y:Float()
  102. Return Position.y
  103. Setter( y:Float )
  104. Local v:=Position
  105. Position=New Vec3f( v.x,y,v.z )
  106. End
  107. #rem monkeydoc World space Z coordinate.
  108. #end
  109. Property Z:Float()
  110. Return Position.z
  111. Setter( z:Float )
  112. Local v:=Position
  113. Position=New Vec3f( v.x,v.y,z )
  114. End
  115. #rem monkeydoc Local space X coordinate.
  116. #end
  117. Property LocalX:Float()
  118. Return LocalPosition.x
  119. Setter( x:Float )
  120. Local v:=LocalPosition
  121. LocalPosition=New Vec3f( x,v.y,v.z )
  122. End
  123. #rem monkeydoc Local space Y coordinate.
  124. #end
  125. Property LocalY:Float()
  126. Return LocalPosition.y
  127. Setter( y:Float )
  128. Local v:=LocalPosition
  129. LocalPosition=New Vec3f( v.x,y,v.z )
  130. End
  131. #rem monkeydoc Local space Z coordinate.
  132. #end
  133. Property LocalZ:Float()
  134. Return LocalPosition.z
  135. Setter( z:Float )
  136. Local v:=LocalPosition
  137. LocalPosition=New Vec3f( v.x,v.y,z )
  138. End
  139. #rem monkeydoc World space scale on the X axis.
  140. #end
  141. Property Sx:Float()
  142. Return Scale.x
  143. Setter( sx:Float )
  144. Local s:=Scale
  145. Scale=New Vec3f( sx,s.y,s.z )
  146. End
  147. #rem monkeydoc World space scale on the Y axis.
  148. #end
  149. Property Sy:Float()
  150. Return Scale.y
  151. Setter( sy:Float )
  152. Local s:=Scale
  153. Scale=New Vec3f( s.x,sy,s.z )
  154. End
  155. #rem monkeydoc World space scale on the Z axis.
  156. #end
  157. Property Sz:Float()
  158. Return Scale.z
  159. Setter( sz:Float )
  160. Local s:=Scale
  161. Scale=New Vec3f( s.x,s.y,sz )
  162. End
  163. #rem monkeydoc Local space scale on the X axis.
  164. #end
  165. Property LocalSx:Float()
  166. Return LocalScale.x
  167. Setter( sx:Float )
  168. Local s:=LocalScale
  169. LocalScale=New Vec3f( sx,s.y,s.z )
  170. End
  171. #rem monkeydoc Local space scale on the Y axis.
  172. #end
  173. Property LocalSy:Float()
  174. Return LocalScale.y
  175. Setter( sy:Float )
  176. Local s:=LocalScale
  177. LocalScale=New Vec3f( s.x,sy,s.z )
  178. End
  179. #rem monkeydoc Local space scale on the Z axis.
  180. #end
  181. Property LocalSz:Float()
  182. Return LocalScale.z
  183. Setter( sz:Float )
  184. Local s:=LocalScale
  185. LocalScale=New Vec3f( s.x,s.y,sz )
  186. End
  187. #rem monkeydoc Sets entity position in local or world space.
  188. #end
  189. Method SetPosition( position:Vec3f,localSpace:Bool=False )
  190. If localSpace LocalPosition=position Else Position=position
  191. End
  192. Method SetPosition( x:Float,y:Float,z:Float,localSpace:Bool=False )
  193. SetPosition( New Vec3f( x,y,z ),localSpace )
  194. End
  195. #rem monkeydoc Gets entity position in local or world space.
  196. #end
  197. Method GetPosition:Vec3f( localSpace:Bool=False )
  198. Return localSpace ? LocalPosition Else Position
  199. End
  200. #rem monkeydoc Sets entity basis matrix in local or world space.
  201. #end
  202. Method SetBasis( basis:Mat3f,localSpace:Bool=False )
  203. If localSpace LocalBasis=basis Else Basis=basis
  204. End
  205. #rem monkeydoc Gets entity basis matrix in local or world space.
  206. #end
  207. method GetBasis:Mat3f( localSpace:Bool=False )
  208. Return localSpace ? LocalBasis Else Basis
  209. End
  210. #rem monkeydoc Sets entity rotation in euler angles in local or world space.
  211. #end
  212. Method SetRotation( rotation:Vec3f,localSpace:Bool=False )
  213. Local basis:=Mat3f.Rotation( rotation * DegreesToRadians )
  214. If localSpace LocalBasis=basis Else Basis=basis
  215. End
  216. Method SetRotation( rx:Float,ry:Float,rz:Float,localSpace:Bool=False )
  217. SetRotation( New Vec3f( rx,ry,rz ),localSpace )
  218. End
  219. #rem monkeydoc Gets entity rotation in euler angles in local or world space.
  220. #end
  221. Method GetRotation:Vec3f( localSpace:Bool=False )
  222. Local basis:=localSpace ? LocalBasis Else Basis
  223. Return basis.GetRotation() * RadiansToDegrees
  224. End
  225. #rem monkeydoc Sets entity scale in local or world space.
  226. #end
  227. Method SetScale( scale:Vec3f,localSpace:Bool=False )
  228. If localSpace LocalScale=scale Else Scale=scale
  229. End
  230. Method SetScale( sx:Float,sy:Float,sz:Float,localSpace:Bool=False )
  231. SetScale( New Vec3f( sx,sy,sz ),localSpace )
  232. End
  233. #rem monkeydoc Gets entity scale in local or world space.
  234. #end
  235. Method GetScale:Vec3f( localSpace:Bool=False )
  236. Return localSpace ? LocalScale Else Scale
  237. End
  238. #rem monkeydoc Moves the entity.
  239. Moves the entity relative to its current orientation.
  240. #end
  241. Method Move( tv:Vec3f,localSpace:Bool=False )
  242. If localSpace LocalPosition+=tv Else Position+=Basis * tv
  243. End
  244. Method Move( tx:Float,ty:Float,tz:Float )
  245. Move( New Vec3f( tx,ty,tz ) )
  246. End
  247. #rem monkeydoc Moves the entity on the X axis.
  248. Moves the entity relative to its current orientation.
  249. #end
  250. Method MoveX( tx:Float,localSpace:Bool=False )
  251. If localSpace LocalX+=tx Else Position+=Basis.i * tx
  252. End
  253. #rem monkeydoc Moves the entity on the Y axis.
  254. Moves the entity relative to its current orientation.
  255. #end
  256. Method MoveY( ty:Float,localSpace:Bool=False )
  257. If localSpace LocalY+=ty Else Position+=Basis.j * ty
  258. End
  259. #rem monkeydoc Moves the entity on the Z axis.
  260. Moves the entity relative to its current orientation.
  261. #end
  262. Method MoveZ( tz:Float,localSpace:Bool=False )
  263. If localSpace LocalZ+=tz Else Position+=Basis.k * tz
  264. End
  265. #rem monkeydoc Rotates the entity.
  266. Rotates the entity.
  267. If `localSpace` is false, the rotation is applied after the entity's world rotation.
  268. If `localSpace` is true, the rotation is applied before the entity's local rotation.
  269. #end
  270. Method Rotate( rv:Vec3f,localSpace:Bool=False )
  271. Local basis:=Mat3f.Rotation( rv * DegreesToRadians )
  272. If localSpace LocalBasis*=basis Else Basis=basis*Basis
  273. End
  274. Method Rotate( rx:Float,ry:Float,rz:Float,localSpace:Bool=False )
  275. Rotate( New Vec3f( rx,ry,rz ),localSpace )
  276. End
  277. #rem monkeydoc Rotates the entity around the X axis.
  278. #end
  279. Method RotateX( rx:Float,localSpace:Bool=False )
  280. Local basis:=Mat3f.Pitch( rx * DegreesToRadians )
  281. If localSpace LocalBasis=basis*LocalBasis Else Basis*=basis
  282. End
  283. #rem monkeydoc Rotates the entity around the Y axis.
  284. #end
  285. Method RotateY( ry:Float,localSpace:Bool=False )
  286. Local basis:=Mat3f.Yaw( ry * DegreesToRadians )
  287. If localSpace LocalBasis=basis*LocalBasis Else Basis*=basis
  288. End
  289. #rem monkeydoc Rotates the entity around the Z axis.
  290. #end
  291. Method RotateZ( rz:Float,localSpace:Bool=False )
  292. Local basis:=Mat3f.Roll( rz * DegreesToRadians )
  293. If localSpace LocalBasis=basis*LocalBasis Else Basis*=basis
  294. End
  295. #rem monkeydoc Points the entity at a target.
  296. #end
  297. Method PointAt( target:Vec3f,up:Vec3f=New Vec3f( 0,1,0 ) )
  298. Local k:=(target-Position).Normalize()
  299. Local i:=up.Cross( k ).Normalize()
  300. Local j:=k.Cross( i )
  301. Basis=New Mat3f( i,j,k )
  302. End
  303. Method PointAt( target:Entity,up:Vec3f=New Vec3f( 0,1,0 ) )
  304. PointAt( target.LocalPosition )
  305. End
  306. End