dynamics.bmx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. ' Copyright (c) 2015-2016 Bruce A Henderson
  2. '
  3. ' This software is provided 'as-is', without any express or implied
  4. ' warranty. In no event will the authors be held liable for any damages
  5. ' arising from the use of this software.
  6. '
  7. ' Permission is granted to anyone to use this software for any purpose,
  8. ' including commercial applications, and to alter it and redistribute it
  9. ' freely, subject to the following restrictions:
  10. '
  11. ' 1. The origin of this software must not be misrepresented; you must not
  12. ' claim that you wrote the original software. If you use this software
  13. ' in a product, an acknowledgment in the product documentation would be
  14. ' appreciated but is not required.
  15. '
  16. ' 2. Altered source versions must be plainly marked as such, and must not be
  17. ' misrepresented as being the original software.
  18. '
  19. ' 3. This notice may not be removed or altered from any source
  20. ' distribution.
  21. '
  22. SuperStrict
  23. Rem
  24. bbdoc: Newton Dynamics
  25. End Rem
  26. Module Newton.Dynamics
  27. ModuleInfo "Version: 1.00"
  28. ModuleInfo "License: zlib"
  29. ModuleInfo "Copyright: Newton Dynamics - 2003-2011 Julio Jerez and Alain Suero"
  30. ModuleInfo "Copyright: Wrapper - 2015-2016 Bruce A Henderson"
  31. ModuleInfo "History: 1.00"
  32. ModuleInfo "History: Initial Release."
  33. ModuleInfo "CC_OPTS: -D_NEWTON_STATIC_LIB"
  34. ?win32x86
  35. ModuleInfo "CC_OPTS: -DPTW32_BUILD -DPTW32_STATIC_LIB -D_MINGW_32_VER"
  36. ModuleInfo "CC_OPTS: -msse -msse3 -mfpmath=sse -ffloat-store -ffast-math -freciprocal-math -funsafe-math-optimizations -fsingle-precision-constant"
  37. ?win32x64
  38. ModuleInfo "CC_OPTS: -DPTW32_BUILD -DPTW32_STATIC_LIB -D_MINGW_64_VER"
  39. ModuleInfo "CC_OPTS: -msse -msse3 -mfpmath=sse -ffloat-store -ffast-math -freciprocal-math -funsafe-math-optimizations -fsingle-precision-constant"
  40. ?macos
  41. ModuleInfo "CC_OPTS: -D_MACOSX_VER -msse4.1"
  42. ?linuxx86
  43. ModuleInfo "CC_OPTS: -O2 -msse -msse3"
  44. ModuleInfo "CC_OPTS: -D_POSIX_VER -mfpmath=sse -ffloat-store -ffast-math -freciprocal-math -funsafe-math-optimizations -fsingle-precision-constant"
  45. ?linuxx64
  46. ModuleInfo "CC_OPTS: -O2 -msse -msse3"
  47. ModuleInfo "CC_OPTS: -D_POSIX_VER_64 -mfpmath=sse -ffloat-store -ffast-math -freciprocal-math -funsafe-math-optimizations -fsingle-precision-constant"
  48. ?raspberrypi
  49. ModuleInfo "CC_OPTS: -D_POSIX_VER -ffloat-store -ffast-math -freciprocal-math -funsafe-math-optimizations -fsingle-precision-constant"
  50. ?
  51. Import "common.bmx"
  52. Rem
  53. bbdoc:
  54. End Rem
  55. Type TNWorld
  56. Field worldPtr:Byte Ptr
  57. Rem
  58. bbdoc: Creates an instance of the Newton world.
  59. End Rem
  60. Function Create:TNWorld()
  61. Local this:TNWorld = New TNWorld
  62. this.worldPtr = bmx_newtondynamics_NewtonCreate(this)
  63. Return this
  64. End Function
  65. Rem
  66. bbdoc: Creates a rigid body.
  67. about: Creates a Newton rigid body and assigns a the @collision geometry representing the rigid body.
  68. Optionally pass a subclassed TNBody object for direct access to force/torque callbacks.
  69. End Rem
  70. Method CreateDynamicBody:TNBody(collision:TNCollision, matrix:TNMatrix, custom:TNBody = Null)
  71. Local body:TNBody
  72. If Not custom Then
  73. body = New TNBody
  74. Else
  75. body = custom
  76. End If
  77. body.bodyPtr = bmx_newtondynamics_NewtonCreateDynamicBody(body, worldPtr, collision.collisionPtr, Varptr matrix.frontX)
  78. NewtonBodySetForceAndTorqueCallback(body.bodyPtr, TNBody._defaultForceAndTorqueCallback)
  79. NewtonBodySetTransformCallback(body.bodyPtr, TNBody._defaultTransformCallback)
  80. Return body
  81. End Method
  82. Rem
  83. bbdoc: Creates an empty complex collision geometry tree.
  84. End Rem
  85. Method CreateTreeCollision:TNTreeCollision(shapeID:Int = 0, custom:TNTreeCollision = Null)
  86. Local tree:TNTreeCollision
  87. If Not custom Then
  88. tree = New TNTreeCollision
  89. Else
  90. tree = custom
  91. End If
  92. tree.collisionPtr = bmx_newtondynamics_NewtonCreateTreeCollision(tree, worldPtr, shapeID)
  93. Return tree
  94. End Method
  95. Rem
  96. bbdoc: Creates a generalized ellipsoid primitive.
  97. about: Sphere collision are generalized ellipsoids, the application can create many different kind of objects by just playing with dimensions of the radius.
  98. for example to make a sphere set all tree radius to the same value, to make a ellipse of revolution just set two of the tree radius to the same value.
  99. End Rem
  100. Method CreateSphere:TNCollision(radius:Float, shapeID:Int = 0, offsetMatrix:TNMatrix = Null, custom:TNCollision = Null)
  101. Local sphere:TNCollision
  102. If Not custom Then
  103. sphere = New TNCollision
  104. Else
  105. sphere = custom
  106. End If
  107. If Not offsetMatrix Then
  108. sphere.collisionPtr = bmx_newtondynamics_NewtonCreateSphere(sphere, worldPtr, radius, shapeID, Null)
  109. Else
  110. sphere.collisionPtr = bmx_newtondynamics_NewtonCreateSphere(sphere, worldPtr, radius, shapeID, Varptr offsetMatrix.frontX)
  111. End If
  112. Return sphere
  113. End Method
  114. Rem
  115. bbdoc: Creates a box primitive for collision.
  116. End Rem
  117. Method CreateBox:TNCollision(dx:Float, dy:Float, dz:Float, shapeID:Int = 0, offsetMatrix:TNMatrix = Null, custom:TNCollision = Null)
  118. Local box:TNCollision
  119. If Not custom Then
  120. box = New TNCollision
  121. Else
  122. box = custom
  123. End If
  124. If Not offsetMatrix Then
  125. box.collisionPtr = bmx_newtondynamics_NewtonCreateBox(box, worldPtr, dx, dy, dz, shapeID, Null)
  126. Else
  127. box.collisionPtr = bmx_newtondynamics_NewtonCreateBox(box, worldPtr, dx, dy, dz, shapeID, Varptr offsetMatrix.frontX)
  128. End If
  129. Return box
  130. End Method
  131. Rem
  132. bbdoc: Creates a cylinder primitive for collision.
  133. End Rem
  134. Method CreateCylinder:TNCollision(radius:Float, height:Float, shapeID:Int = 0, offsetMatrix:TNMatrix = Null, custom:TNCollision = Null)
  135. Local cylinder:TNCollision
  136. If Not custom Then
  137. cylinder = New TNCollision
  138. Else
  139. cylinder = custom
  140. End If
  141. If Not offsetMatrix Then
  142. cylinder.collisionPtr = bmx_newtondynamics_NewtonCreateCylinder(cylinder, worldPtr, radius, height, shapeID, Null)
  143. Else
  144. cylinder.collisionPtr = bmx_newtondynamics_NewtonCreateCylinder(cylinder, worldPtr, radius, height, shapeID, Varptr offsetMatrix.frontX)
  145. End If
  146. Return cylinder
  147. End Method
  148. Rem
  149. bbdoc:
  150. End Rem
  151. Method CreateMesh:TNMesh()
  152. Local mesh:TNMesh = New TNMesh
  153. mesh.meshPtr = NewtonMeshCreate(worldPtr)
  154. Return mesh
  155. End Method
  156. Rem
  157. bbdoc: Sets coulomb model of friction.
  158. about: Allows the application to chose between and exact or an adaptive coulomb friction model.
  159. 0: Is the exact model. Friction forces are calculated in each frame.
  160. This model is good for applications where precision is more important than speed, ex: realistic simulation.
  161. 1: Is the adaptive model. Here values from previous frames are used to determine the maximum friction values of the current frame.
  162. This is about 10% faster than the exact model however it may introduce strange friction behaviors. For example a
  163. bouncing object tumbling down a ramp will act as a friction less object because the contacts do not have continuity.
  164. In general each time a new contact is generated the friction value is zero, only if the contact persist a non zero
  165. friction values is used. The second effect is that if a normal force is very strong, and if the contact is suddenly
  166. destroyed, a very strong friction force will be generated at the contact point making the object react in a non-familiar way.
  167. End Rem
  168. Method SetFrictionModel(model:Int)
  169. NewtonSetFrictionModel(worldPtr, model)
  170. End Method
  171. Rem
  172. bbdoc: Sets the minimum frame rate at which the simulation can run.
  173. about: The default minimum frame rate of the simulation is 60 frame per second.
  174. When the simulation falls below the specified minimum frame, Newton will perform sub steps in order to meet the desired minimum FPS.
  175. End Rem
  176. Method SetMinimumFrameRate(framerate:Float)
  177. NewtonSetMinimumFrameRate(worldPtr, framerate)
  178. End Method
  179. Rem
  180. bbdoc: Sets the contact merge tolerance.
  181. End Rem
  182. Method SetContactMergeTolerance(tolerance:Float)
  183. NewtonSetContactMergeTolerance(worldPtr, tolerance)
  184. End Method
  185. Rem
  186. bbdoc: Resets all internal states of the engine.
  187. about: When an application wants to reset the state of all the objects in the world to a predefined initial condition,
  188. just setting the initial position and velocity is not sufficient to reproduce equal runs since the engine maintain
  189. there are internal states that in order to take advantage of frame to frame coherence.
  190. In this cases this method will reset all of the internal states.
  191. End Rem
  192. Method InvalidateCache()
  193. NewtonInvalidateCache(worldPtr)
  194. End Method
  195. Rem
  196. bbdoc: Advances the simulation by an amount of time.
  197. about: The Newton Engine does not perform sub-steps, and does not need tuning parameters. It is the responsibility of the application to
  198. ensure that @timestep is small enough to guarantee physics stability.
  199. End Rem
  200. Method Update(timestep:Float)
  201. NewtonUpdate(worldPtr, timestep)
  202. End Method
  203. Rem
  204. bbdoc: Returns the total number of rigid bodies in the world.
  205. End Rem
  206. Method GetBodyCount:Int()
  207. Return NewtonWorldGetBodyCount(worldPtr)
  208. End Method
  209. Rem
  210. bbdoc: Returns the total number of constraints in the world.
  211. End Rem
  212. Method GetConstraintCount:Int()
  213. Return NewtonWorldGetConstraintCount(worldPtr)
  214. End Method
  215. Rem
  216. bbdoc: Shoots a ray from p0 to p1 and calling the delegate's OnFilter/OnPreFilter with each ray intersection.
  217. about: The ray cast will call the application with each body intersecting the line segment.
  218. By writing the OnFilter() in different ways the application can implement different flavors of ray casting.
  219. For example an all body ray cast can be easily implemented by having the OnFilter() always returning 1.0, and copying each
  220. rigid body into an array/list; a closest hit ray cast can be implemented by saving the body with the smaller intersection
  221. parameter and returning the parameter t; and a report the first body hit can be implemented by having the filter returning
  222. zero after the first call and saving the rigid body.
  223. End Rem
  224. Method RayCast(p0x:Float, p0y:Float, p0z:Float, p1x:Float, p1y:Float, p1z:Float, delegate:TNRayCastDelegate, threadIndex:Int = 0)
  225. If delegate.prefilter Then
  226. bmx_newtondynamics_NewtonWorldRayCast(worldPtr, p0x, p0y, p0z, p1x, p1y, p1z, TNRayCastDelegate._filterCallback, delegate, TNRayCastDelegate._prefilterCallback, threadIndex)
  227. Else
  228. bmx_newtondynamics_NewtonWorldRayCast(worldPtr, p0x, p0y, p0z, p1x, p1y, p1z, TNRayCastDelegate._filterCallback, delegate, Null, threadIndex)
  229. End If
  230. End Method
  231. Rem
  232. bbdoc:
  233. End Rem
  234. Method Destroy()
  235. If worldPtr Then
  236. NewtonDestroy(worldPtr)
  237. worldPtr = Null
  238. End If
  239. End Method
  240. End Type
  241. Rem
  242. bbdoc: A helper delegate for handling ray casts.
  243. End Rem
  244. Type TNRayCastDelegate
  245. Rem
  246. bbdoc: Whether or not to enable the prefilter delegation.
  247. about: If @prefilter is True, Newton will call OnPreFilter() right before executing the intersections between the ray and the primitive.
  248. If the method returns zero, Newton will not ray cast the primitive.
  249. The application can use this implement faster or smarter filters when implementing complex logic, otherwise for normal all ray cast
  250. this setting can be False.
  251. End Rem
  252. Field prefilter:Int = False
  253. Rem
  254. bbdoc:
  255. End Rem
  256. Method OnPreFilter:Int(body:TNBody, collision:TNCollision)
  257. End Method
  258. Rem
  259. bbdoc:
  260. about: The most common use for the ray cast function is the closest body hit. In this case it is important, for performance reasons,
  261. that the OnFilter() method returns the intersection parameter. If the OnFilter() method returns a value of zero the ray cast will terminate
  262. immediately.
  263. End Rem
  264. Method OnFilter:Float(body:TNBody, shapeHit:TNCollision, hitContact:Float Ptr, hitNormal:Float Ptr, intersectParam:Float)
  265. End Method
  266. Function _prefilterCallback:Int(bodyPtr:Byte Ptr, collPtr:Byte Ptr, delPtr:Byte Ptr)
  267. Local delegate:TNRayCastDelegate = bmx_newtondynamics_RayCastDelegateFromPtr(delPtr)
  268. Return delegate.OnPreFilter(NewtonBodyGetUserData(bodyPtr), NewtonCollisionGetUserData(collPtr))
  269. End Function
  270. Function _filterCallback:Float(bodyPtr:Byte Ptr, collPtr:Byte Ptr, hitContact:Float Ptr, hitNormal:Float Ptr, delPtr:Byte Ptr, intersectParam:Float)
  271. Local delegate:TNRayCastDelegate = bmx_newtondynamics_RayCastDelegateFromPtr(delPtr)
  272. Return delegate.OnFilter(NewtonBodyGetUserData(bodyPtr), NewtonCollisionGetUserData(collPtr), hitContact, hitNormal, intersectParam)
  273. End Function
  274. End Type
  275. Rem
  276. bbdoc:
  277. End Rem
  278. Type TNCollision
  279. Field collisionPtr:Byte Ptr
  280. Method Destroy()
  281. If collisionPtr Then
  282. NewtonDestroyCollision(collisionPtr)
  283. collisionPtr = Null
  284. End If
  285. End Method
  286. End Type
  287. Rem
  288. bbdoc: A complex collision geometry tree.
  289. about: TNTreeCollision is the preferred method within Newton for collision with polygonal meshes of arbitrary complexity.
  290. The mesh must be made of flat non-intersecting polygons, but they do not explicitly need to be triangles.
  291. End Rem
  292. Type TNTreeCollision Extends TNCollision
  293. Method BeginBuild()
  294. NewtonTreeCollisionBeginBuild(collisionPtr)
  295. End Method
  296. Rem
  297. bbdoc: Finalizes the construction of the polygonal mesh.
  298. about: After the application has finished adding polygons to the TNTreeCollision, it must call this method to finalize the construction of the collision mesh.
  299. If concave polygons are added to the *TreeCollision*, the application must call this function with the parameter *optimize* set to 1.
  300. With the *optimize* parameter set to 1, Newton will optimize the collision mesh by removing non essential edges from adjacent flat polygons.
  301. Newton will not change the topology of the mesh but significantly reduces the number of polygons in the mesh. The reduction factor of the number of polygons in the mesh depends upon the irregularity of the mesh topology.
  302. A reduction factor of 1.5 to 2.0 is common.
  303. Calling this method with the parameter *optimize* set to zero, will leave the mesh geometry unaltered.
  304. End Rem
  305. Method EndBuild(optimize:Int)
  306. NewtonTreeCollisionEndBuild(collisionPtr, optimize)
  307. End Method
  308. Rem
  309. bbdoc: Adds an individual polygon to the TNTreeCollision.
  310. about:
  311. End Rem
  312. Method AddFace(vertexCount:Int, vertexPtr:Float Ptr, strideInBytes:Int, faceAttribute:Int)
  313. NewtonTreeCollisionAddFace(collisionPtr, vertexCount, vertexPtr, strideInBytes, faceAttribute)
  314. End Method
  315. End Type
  316. Rem
  317. bbdoc: Creates a rigid body.
  318. about: Can be subclassed in order to override callbacks such as OnForceAndTorque().
  319. End Rem
  320. Type TNBody
  321. Field bodyPtr:Byte Ptr
  322. ' the user callback function
  323. Field _fatCallback(body:TNBody, timestamp:Float, threadIndex:Int)
  324. Field _transCallback(body:TNBody, matrix:Float Ptr, threadIndex:Int)
  325. Rem
  326. bbdoc:
  327. End Rem
  328. Method OnForceAndTorque(timestamp:Float, threadIndex:Int)
  329. End Method
  330. Method OnTransform(matrix:Float Ptr, threadIndex:Int)
  331. End Method
  332. Rem
  333. bbdoc: Sets the transformation matrix of a rigid body.
  334. about: The application should make sure the transformation matrix has not scale, otherwise unpredictable result will occur.
  335. End Rem
  336. Method SetMatrix(matrix:TNMatrix)
  337. NewtonBodySetMatrix(bodyPtr, Varptr matrix.frontX)
  338. End Method
  339. Rem
  340. bbdoc: Gets the transformation matrix of a rigid body, populating @matrix.
  341. End Rem
  342. Method GetMatrix(matrix:TNMatrix)
  343. NewtonBodyGetMatrix(bodyPtr, Varptr matrix.frontX)
  344. End Method
  345. Rem
  346. bbdoc: Gets the mass matrix of a rigid body.
  347. End Rem
  348. Method GetMassMatrix(mass:Float Var, Ixx:Float Var, Iyy:Float Var, Izz:Float Var)
  349. NewtonBodyGetMassMatrix(bodyPtr, Varptr mass, Varptr Ixx, Varptr Iyy, Varptr Izz)
  350. End Method
  351. Rem
  352. bbdoc: Gets the rotation part of the transformation matrix of a body, in form of a unit quaternion.
  353. about: The rotation quaternion is the same as what the application would get by using at function to extract a quaternion form a matrix.
  354. however since the rigid body already contained the rotation in it, it is more efficient to just call this method avoiding expensive conversion.
  355. End Rem
  356. Method GetRotation(q0:Float Var, q1:Float Var, q2:Float Var, q3:Float Var)
  357. bmx_newtondynamics_NewtonBodyGetRotation(bodyPtr, Varptr q0, Varptr q1, Varptr q2, Varptr q3)
  358. End Method
  359. Rem
  360. bbdoc:
  361. End Rem
  362. Method GetInvMass(mass:Float Var, Ixx:Float Var, Iyy:Float Var, Izz:Float Var)
  363. NewtonBodyGetInvMass(bodyPtr, Varptr mass, Varptr Ixx, Varptr Iyy, Varptr Izz)
  364. End Method
  365. Rem
  366. bbdoc:
  367. End Rem
  368. Method GetInertiaMatrix(matrix:TNMatrix)
  369. NewtonBodyGetInertiaMatrix(bodyPtr, Varptr matrix.frontX)
  370. End Method
  371. Rem
  372. bbdoc:
  373. End Rem
  374. Method GetInvInertiaMatrix(matrix:TNMatrix)
  375. NewtonBodyGetInvInertiaMatrix(bodyPtr, Varptr matrix.frontX)
  376. End Method
  377. Rem
  378. bbdoc: Gets the global angular velocity of the body.
  379. End Rem
  380. Method GetOmega(ox:Float Var, oy:Float Var, oz:Float Var)
  381. bmx_newtondynamics_NewtonBodyGetOmega(bodyPtr, Varptr ox, Varptr oy, Varptr oz)
  382. End Method
  383. Rem
  384. bbdoc: Gets the global linear velocity of the body.
  385. End Rem
  386. Method GetVelocity(vx:Float Var, vy:Float Var, vz:Float Var)
  387. bmx_newtondynamics_NewtonBodyGetVelocity(bodyPtr, Varptr vx, Varptr vy, Varptr vz)
  388. End Method
  389. Rem
  390. bbdoc: Gets the net force applied to a rigid body after the last Newton Update.
  391. End Rem
  392. Method GetForce(fx:Float Var, fy:Float Var, fz:Float Var)
  393. bmx_newtondynamics_NewtonBodyGetForce(bodyPtr, Varptr fx, Varptr fy, Varptr fz)
  394. End Method
  395. Rem
  396. bbdoc: Gets the net torque applied to a rigid body after the last Newton Update.
  397. End Rem
  398. Method GetTorque(tx:Float Var, ty:Float Var, tz:Float Var)
  399. bmx_newtondynamics_NewtonBodyGetTorque(bodyPtr, Varptr tx, Varptr ty, Varptr tz)
  400. End Method
  401. Rem
  402. bbdoc: Gets the force applied on the last call to OnForceAndTorque/apply force and torque callback.
  403. about: This method can be useful to modify force from joint callback
  404. End Rem
  405. Method GetForceAcc(fx:Float Var, fy:Float Var, fz:Float Var)
  406. bmx_newtondynamics_NewtonBodyGetForceAcc(bodyPtr, Varptr fx, Varptr fy, Varptr fz)
  407. End Method
  408. Rem
  409. bbdoc: Gets the torque applied on the last call to OnForceAndTorque/apply force and torque callback.
  410. about: This method can be useful to modify torque from joint callback
  411. End Rem
  412. Method GetTorqueAcc(tx:Float Var, ty:Float Var, tz:Float Var)
  413. bmx_newtondynamics_NewtonBodyGetTorqueAcc(bodyPtr, Varptr tx, Varptr ty, Varptr tz)
  414. End Method
  415. Rem
  416. bbdoc: Gets the relative position of the center of mass of a rigid body.
  417. about: This method can be used to set the relative offset of the center of mass of a rigid body.
  418. when a rigid body is created the center of mass is set the the point c(0, 0, 0), and normally this is
  419. the best setting for a rigid body. However there are situations in which and object does not have symmetry or
  420. simple some kind of special effect is desired, and this origin needs to be changed.
  421. End Rem
  422. Method GetCentreOfMass(cx:Float Var, cy:Float Var, cz:Float Var)
  423. bmx_newtondynamics_NewtonBodyGetCentreOfMass(bodyPtr, Varptr cx, Varptr cy, Varptr cz)
  424. End Method
  425. Rem
  426. bbdoc: Gets the angular viscous damping of the body.
  427. End Rem
  428. Method GetAngularDamping(aX:Float Var, aY:Float Var, aZ:Float Var)
  429. bmx_newtondynamics_NewtonBodyGetAngularDamping(bodyPtr, Varptr aX, Varptr aY, Varptr aZ)
  430. End Method
  431. Rem
  432. bbdoc: Gets the world axis aligned bounding box (AABB) of the body.
  433. End Rem
  434. Method NewtonBodyGetAABB(p0x:Float Var, p0y:Float Var, p0z:Float Var, p1x:Float Var, p1y:Float Var, p1z:Float Var)
  435. bmx_newtondynamics_NewtonBodyGetAABB(bodyPtr, Varptr p0x, Varptr p0y, Varptr p0z, Varptr p1x, Varptr p1y, Varptr p1z)
  436. End Method
  437. Rem
  438. bbdoc: Applies the angular viscous damping coefficient to the body.
  439. about: The default value of *angularDamp* is clamped to a value between 0.0 and 1.0; the default value is 0.1,
  440. There is a non zero implicit attenuation value of 0.0001 assumed by the integrator.
  441. The dampening viscous friction torque is added to the external torque applied to the body every frame before going to the solver-integrator.
  442. This torque is proportional to the square of the magnitude of the angular velocity to the body in the opposite direction of the angular velocity of the body.
  443. An application can set *angularDamp* to zero when the to take control of the external forces and torque applied to the body, should the application
  444. desire to have absolute control of the forces over that body. However, it is recommended that the *linearDamp* coefficient be set to a non-zero
  445. value for the majority of background bodies. This saves the application from needing to control these forces and also prevents the integrator from
  446. adding very large velocities to a body.
  447. End Rem
  448. Method SetAngularDamping(aX:Float, aY:Float, aZ:Float)
  449. bmx_newtondynamics_NewtonBodySetAngularDamping(bodyPtr, aX, aY, aZ)
  450. End Method
  451. Rem
  452. bbdoc: Assigns an event function for applying external force and torque to a rigid body.
  453. about: The default is to call the OnForceAndTorque() method, which can be overriden by subclassing TNBody and
  454. re-implementing the method.
  455. The callback is called by the Newton Engine every time an active body is going to be simulated.
  456. The Newton Engine does not call the callback for bodies that are inactive or have reached a state of stable equilibrium.
  457. End Rem
  458. Method SetForceAndTorqueCallback(callback(body:TNBody, timestamp:Float, threadIndex:Int))
  459. _fatCallback = callback
  460. NewtonBodySetForceAndTorqueCallback(bodyPtr, _forceAndTorqueCallback)
  461. End Method
  462. Rem
  463. bbdoc: Assign a transformation event function to the body.
  464. about: The default is to call the OnTransform() method, which can be overriden by subclassing TNBody and
  465. re-implementing the method.
  466. The callback is called by the Newton engine every time a visual object that represents the rigid body has changed.
  467. The Newton engine does not call the callback for bodies that are inactive or have reached a state of stable equilibrium.
  468. End Rem
  469. Method SetTransformCallback(callback(body:TNBody, matrix:Float Ptr, threadIndex:Int))
  470. _transCallback = callback
  471. NewtonBodySetTransformCallback(bodyPtr, _transformCallback)
  472. End Method
  473. Rem
  474. bbdoc: Sets the global angular velocity of the body.
  475. End Rem
  476. Method SetOmega(ox:Float, oy:Float, oz:Float, ow:Float)
  477. bmx_newtondynamics_NewtonBodySetOmega(bodyPtr, ox, oy, oz, ow)
  478. End Method
  479. Rem
  480. bbdoc: Sets the net force applied to a rigid body.
  481. about: This method is only effective when called from OnForceAndTorque/apply force and torque callback.
  482. End Rem
  483. Method SetForce(fx:Float, fy:Float, fz:Float, fw:Float)
  484. bmx_newtondynamics_NewtonBodySetForce(bodyPtr, fx, fy, fz, fw)
  485. End Method
  486. Rem
  487. bbdoc: Sets the global linear velocity of the body.
  488. End Rem
  489. Method SetVelocity(vx:Float, vy:Float, vz:Float, vw:Float)
  490. bmx_newtondynamics_NewtonBodySetVelocity(bodyPtr, vx, vy, vz, vw)
  491. End Method
  492. Rem
  493. bbdoc: Sets the net torque applied to a rigid body.
  494. about: This method is only effective when called from OnForceAndTorque/apply force and torque callback.
  495. End Rem
  496. Method SetTorque(tx:Float, ty:Float, tz:Float, tw:Float)
  497. bmx_newtondynamics_NewtonBodySetTorque(bodyPtr, tx, ty, tz, tw)
  498. End Method
  499. Rem
  500. bbdoc:
  501. End Rem
  502. Method SetMassProperties(mass:Float, collision:TNCollision)
  503. NewtonBodySetMassProperties(bodyPtr, mass, collision.collisionPtr)
  504. End Method
  505. Rem
  506. bbdoc: Applies the linear viscous damping coefficient to the body.
  507. about: The default value of @linearDamp is clamped to a value between 0.0 and 1.0; the default value is 0.1,
  508. There is a non zero implicit attenuation value of 0.0001 assume by the integrator.
  509. End Rem
  510. Method SetLinearDamping(linearDamp:Float)
  511. NewtonBodySetLinearDamping(bodyPtr, linearDamp)
  512. End Method
  513. Rem
  514. bbdoc: Returns the body type.
  515. about: Returns one of NEWTON_DYNAMIC_BODY, NEWTON_KINEMATIC_BODY or NEWTON_DEFORMABLE_BODY.
  516. End Rem
  517. Method GetType:Int()
  518. Return NewtonBodyGetType(bodyPtr)
  519. End Method
  520. Rem
  521. bbdoc: Returns true if the body is collidable.
  522. End Rem
  523. Method GetCollidable:Int()
  524. Return NewtonBodyGetCollidable(bodyPtr)
  525. End Method
  526. Rem
  527. bbdoc: Sets the collidable state for the body.
  528. End Rem
  529. Method SetCollidable(collidableState:Int)
  530. NewtonBodySetCollidable(bodyPtr, collidableState)
  531. End Method
  532. Rem
  533. bbdoc: Adds the net force applied to a rigid body.
  534. about: This method is only effective when called from OnForceAndTorque/apply force and torque callback.
  535. End Rem
  536. Method AddForce(fx:Float, fy:Float, fz:Float)
  537. bmx_newtondynamics_NewtonBodyAddForce(bodyPtr, fx, fy, fz)
  538. End Method
  539. Rem
  540. bbdoc: Adds the net torque applied to a rigid body.
  541. about: This method is only effective when called from OnForceAndTorque/apply force and torque callback.
  542. End Rem
  543. Method AddTorque(tx:Float, ty:Float, tz:Float)
  544. bmx_newtondynamics_NewtonBodyAddTorque(bodyPtr, tx, ty, tz)
  545. End Method
  546. Rem
  547. bbdoc: Calculates the next force that net to be applied to the body to archive the desired velocity in the current time step.
  548. about: Can be useful when creating object for game play.
  549. This treats the body as a point mass and is uses the solver to calculates the net force that need to be applied to the body
  550. such that it reaches the desired velocity in the net time step.
  551. End Rem
  552. Method CalculateInverseDynamicsForce(timestep:Float, vx:Float, vy:Float, vz:Float, fx:Float Var, fy:Float Var, fz:Float Var)
  553. bmx_newtondynamics_NewtonBodyCalculateInverseDynamicsForce(bodyPtr, timestep, vx, vy, vz, Varptr fx, Varptr fy, Varptr fz)
  554. End Method
  555. ' internal
  556. Function _forceAndTorqueCallback(bodyPtr:Byte Ptr, timestamp:Float, threadIndex:Int)
  557. Local body:TNBody = NewtonBodyGetUserData(bodyPtr)
  558. body._fatCallback(body, timestamp, threadIndex)
  559. End Function
  560. Function _defaultForceAndTorqueCallback(bodyPtr:Byte Ptr, timestamp:Float, threadIndex:Int)
  561. Local body:TNBody = NewtonBodyGetUserData(bodyPtr)
  562. body.OnForceAndTorque(timestamp, threadIndex)
  563. End Function
  564. Function _transformCallback(bodyPtr:Byte Ptr, matrix:Float Ptr, threadIndex:Int)
  565. Local body:TNBody = NewtonBodyGetUserData(bodyPtr)
  566. body._transCallback(body, matrix, threadIndex)
  567. End Function
  568. Function _defaultTransformCallback(bodyPtr:Byte Ptr, matrix:Float Ptr, threadIndex:Int)
  569. Local body:TNBody = NewtonBodyGetUserData(bodyPtr)
  570. body.OnTransform(matrix, threadIndex)
  571. End Function
  572. Rem
  573. bbdoc:
  574. End Rem
  575. Method Destroy()
  576. If bodyPtr Then
  577. NewtonDestroyBody(bodyPtr)
  578. bodyPtr = Null
  579. End If
  580. End Method
  581. End Type
  582. Rem
  583. bbdoc:
  584. End Rem
  585. Type TNMesh
  586. Field meshPtr:Byte Ptr
  587. Rem
  588. bbdoc:
  589. End Rem
  590. Method ApplyTransform(matrix:TNMatrix)
  591. NewtonMeshApplyTransform(meshPtr, Varptr matrix.frontX)
  592. End Method
  593. Rem
  594. bbdoc:
  595. End Rem
  596. Method CalculateOOBB(matrix:TNMatrix, x:Float Var, y:Float Var, z:Float Var)
  597. NewtonMeshCalculateOOBB(meshPtr, Varptr matrix.frontX, Varptr x, Varptr y, Varptr z)
  598. End Method
  599. Rem
  600. bbdoc:
  601. End Rem
  602. Method CalculateVertexNormals(angle:Float)
  603. NewtonMeshCalculateVertexNormals(meshPtr, angle * 0.01745329252)
  604. End Method
  605. Rem
  606. bbdoc:
  607. End Rem
  608. Method ApplySphericalMapping(material:Int)
  609. NewtonMeshApplySphericalMapping(meshPtr, material)
  610. End Method
  611. Rem
  612. bbdoc:
  613. End Rem
  614. Method ApplyCylindricalMapping(cylinderMaterial:Int, capMaterial:Int)
  615. NewtonMeshApplyCylindricalMapping(meshPtr, cylinderMaterial, capMaterial)
  616. End Method
  617. Rem
  618. bbdoc:
  619. End Rem
  620. Method ApplyBoxMapping(front:Int, side:Int, top:Int)
  621. NewtonMeshApplyBoxMapping(meshPtr, front, side, top)
  622. End Method
  623. Rem
  624. bbdoc:
  625. End Rem
  626. Method IsOpenMesh:Int()
  627. Return NewtonMeshIsOpenMesh(meshPtr)
  628. End Method
  629. Rem
  630. bbdoc:
  631. End Rem
  632. Method FixTJoints()
  633. NewtonMeshFixTJoints(meshPtr)
  634. End Method
  635. Rem
  636. bbdoc:
  637. End Rem
  638. Method Polygonize()
  639. NewtonMeshPolygonize(meshPtr)
  640. End Method
  641. Rem
  642. bbdoc:
  643. End Rem
  644. Method Triangulate()
  645. NewtonMeshTriangulate(meshPtr)
  646. End Method
  647. Rem
  648. bbdoc:
  649. End Rem
  650. Method BeginFace()
  651. NewtonMeshBeginFace(meshPtr)
  652. End Method
  653. Rem
  654. bbdoc:
  655. End Rem
  656. Method AddFace(vertexCount:Int, vertex:Float Ptr, strideInBytes:Int, materialIndex:Int)
  657. NewtonMeshAddFace(meshPtr, vertexCount, vertex, strideInBytes, materialIndex)
  658. End Method
  659. Rem
  660. bbdoc:
  661. End Rem
  662. Method EndFace()
  663. NewtonMeshEndFace(meshPtr)
  664. End Method
  665. Rem
  666. bbdoc:
  667. End Rem
  668. Method GetTotalFaceCount:Int()
  669. Return NewtonMeshGetTotalFaceCount(meshPtr)
  670. End Method
  671. Rem
  672. bbdoc:
  673. End Rem
  674. Method GetTotalIndexCount:Int()
  675. Return NewtonMeshGetTotalIndexCount(meshPtr)
  676. End Method
  677. Rem
  678. bbdoc:
  679. End Rem
  680. Method GetPointCount:Int()
  681. Return NewtonMeshGetPointCount(meshPtr)
  682. End Method
  683. Rem
  684. bbdoc:
  685. End Rem
  686. Method GetPointStrideInByte:Int()
  687. Return NewtonMeshGetPointStrideInByte(meshPtr)
  688. End Method
  689. Rem
  690. bbdoc:
  691. End Rem
  692. Method GetPointArray:Double Ptr()
  693. Return NewtonMeshGetPointArray(meshPtr)
  694. End Method
  695. Rem
  696. bbdoc:
  697. End Rem
  698. Method GetNormalArray:Double Ptr()
  699. Return NewtonMeshGetNormalArray(meshPtr)
  700. End Method
  701. Rem
  702. bbdoc:
  703. End Rem
  704. Method GetUV0Array:Double Ptr()
  705. Return NewtonMeshGetUV0Array(meshPtr)
  706. End Method
  707. Rem
  708. bbdoc:
  709. End Rem
  710. Method GetUV1Array:Double Ptr()
  711. Return NewtonMeshGetUV1Array(meshPtr)
  712. End Method
  713. Rem
  714. bbdoc:
  715. End Rem
  716. Method GetVertexCount:Int()
  717. Return NewtonMeshGetVertexCount(meshPtr)
  718. End Method
  719. Rem
  720. bbdoc:
  721. End Rem
  722. Method GetVertexStrideInByte:Int()
  723. Return NewtonMeshGetVertexStrideInByte(meshPtr)
  724. End Method
  725. Rem
  726. bbdoc:
  727. End Rem
  728. Method MeshGetVertexArray:Double Ptr()
  729. Return NewtonMeshGetVertexArray(meshPtr)
  730. End Method
  731. Rem
  732. bbdoc:
  733. End Rem
  734. Method Destroy()
  735. If meshPtr Then
  736. NewtonMeshDestroy(meshPtr)
  737. meshPtr = Null
  738. End If
  739. End Method
  740. End Type
  741. Rem
  742. bbdoc:
  743. End Rem
  744. Type TNMatrix
  745. Field frontX:Float
  746. Field frontY:Float
  747. Field frontZ:Float
  748. Field frontW:Float
  749. Field upX:Float
  750. Field upY:Float
  751. Field upZ:Float
  752. Field upW:Float
  753. Field rightX:Float
  754. Field rightY:Float
  755. Field rightZ:Float
  756. Field rightW:Float
  757. Field positX:Float
  758. Field positY:Float
  759. Field positZ:Float
  760. Field positW:Float
  761. Rem
  762. bbdoc:
  763. End Rem
  764. Function GetIdentityMatrix:TNMatrix()
  765. Local matrix:TNMatrix = New TNMatrix
  766. matrix.frontX = 1
  767. matrix.upY = 1
  768. matrix.rightZ = 1
  769. matrix.positW = 1
  770. Return matrix
  771. End Function
  772. Rem
  773. bbdoc:
  774. End Rem
  775. Method GetEulerAngles(pitch0:Float Var, yaw0:Float Var, roll0:Float Var, pitch1:Float Var, yaw1:Float Var, roll1:Float Var)
  776. bmx_newtondynamics_matrix_GetEulerAngles(Varptr frontX, Varptr pitch0, Varptr yaw0, Varptr roll0, Varptr pitch1, Varptr yaw1, Varptr roll1)
  777. End Method
  778. End Type
  779. Extern
  780. Function NewtonBodyGetUserData:TNBody(body:Byte Ptr)
  781. Function NewtonCollisionGetUserData:TNCollision(coll:Byte Ptr)
  782. Function bmx_newtondynamics_RayCastDelegateFromPtr:TNRayCastDelegate(del:Byte Ptr)
  783. End Extern