November 27, 2020, 05:18:47 PM

Author Topic: [bb] Newton physics by JaviCervera [ 1+ years ago ]  (Read 1114 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Newton physics by JaviCervera [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Newton physics
Author : JaviCervera
Posted : 1+ years ago

Description : I haven't tested it yet, but I think it is ok. Some of the functions don't have much sense in Blitz, cos they use function callbacks. Anyway, you would use these functions by creating custom dlls with functions which return pointers to the callbacks. Other functions return pointers to data, so you need to peek from that memory. I woyuld write a PeekMemByte(), PeekMemShort() etc library if someone requests it :) Other functions require a pointer to some data as parameter (for example, a vector with three floats). You can reate a bank and pass it as parameter.

Try it and tell me if something is wrong. Please create cool demos for all of us to see! :D

You must download the Newton SDK from <a href="http://www.physicsengine.com/" target="_blank">http://www.physicsengine.com[/url] which contains the DLL you need and the documentation of the API.

Enjoy :)


Code :
Code: BlitzBasic
  1. .lib "Newton.dll"
  2.  
  3. ;World interface
  4. NewtonCreate%(malloc, free)
  5. NewtonDestroy(world)
  6. NewtonUpdate(world, timestep#)
  7. NewtonDestroyAllBodies(world)
  8. NewtonSetWorldSize(world, min_ptr*, max_ptr*)
  9. NewtonSetBodyLeaveWorldEvent(world, callback)
  10. NewtonWorldFreezeBody(world, body)
  11. NewtonWorldUnfreezeBody(world, body)
  12.  
  13. ;GroupID interface
  14. NewtonMaterialGetDefaultGroupID%(world)
  15. NewtonMaterialCreateGroupID%(world)
  16. NewtonMaterialDestroyAllGroupID(world)
  17. NewtonMaterialSetDefaultCollidable(world, id0, id1, state)
  18. NewtonMaterialSetDefaultFriction(world, id0, id1, static_friction#, kinetic_friction#)
  19. NewtonMaterialSetDefaultElasticity(world, id0, id1, elastic_coef#)
  20. NewtonMaterialSetDefaultSoftness(world, id0, id1, softness_coef#)
  21. NewtonMaterialSetCollisionCallback(world, id0, id1, user_data*, begin_callback, process_callback, end_callback)
  22.  
  23. ;Contact behaviour control interface
  24. NewtonMaterialDisableContact(material)
  25. NewtonMaterialGetContactUserData%(material)
  26. NewtonMaterialGetContactNormalSpeed#(material, contact_handle)
  27. NewtonMaterialGetContactTangentSpeed#(material, contact_handle, index)
  28. NewtonMaterialGetContactPositionAndNormal(material, posit_ptr*, normal_ptr*)
  29. NewtonMaterialGetContactTangentDirections(material, dir0*, dir1*)
  30. NewtonMaterialSetContactSoftness(material, softness#)
  31. NewtonMaterialSetContactElasticity(material, restitution#)
  32. NewtonMaterialSetContactFrictionState(material, state, index)
  33. NewtonMaterialSetContactStaticFrictionCoef(material, coef#, index)
  34. NewtonMaterialSetContactKineticFrictionCoef(material, coef#, index)
  35. NewtonMaterialSetContactTangentAcceleration(material, accel#, index)
  36. NewtonMaterialContactRotateTangentDirections(material,align_vector*)
  37.  
  38. ;Convex collision primitives interface
  39. NewtonCreateSphere%(world, radius#, offset_matrix*)
  40. NewtonCreateBox%(world, dx#, dy#, dz#, offset_matrix*)
  41.  
  42. ;Complex collision primitives interface
  43. NewtonCreateCompoundCollision%(world, count, collision_primitive_array*)
  44. NewtonCreateUserMeshCollision%(world, min_box*, max_box*, user_data*, collide_callback, rayhit_callback, destroy_callback)
  45. NewtonCreateTreeCollision%(world, user_callback)
  46. NewtonTreeCollisionBeginBuild(tree_collision)
  47. NewtonTreeCollisionAddFace(tree_collision, vertex_count, vertex_ptr, stride_in_bytes, face_attribute)
  48. NewtonTreeCollisionEndBuild(tree_collision, optimize)
  49. NewtonTreeCollisionSerialize(tree_collision, callback, serialize_handle*)
  50. NewtonCreateTreeCollisionFromSerialization%(world, user_callback, deserialize_function, serialize_handle)
  51. NewtonTreeCollisionGetFaceAtribute%(tree_collision, face_index_array*)
  52. NewtonTreeCollisionSetFaceAtribute(tree_collision, face_index_array*, attribute)
  53.  
  54. ;Collision miscelanios interface
  55. NewtonReleaseCollision(world, collision_ptr)
  56. NewtonCollisionCalculateAABB(collision_ptr, offset_matrix*, p0*, p1*)
  57.  
  58. ;Transforms utility functions
  59. NewtonGetEulerAngle(matrix*, angles*)
  60. NewtonSetEulerAngle(matrix*, angles*)
  61.  
  62. ;Rigid Body interface
  63. NewtonCreateBody%(world, collision_ptr)
  64. NewtonDestroyBody(world, body)
  65. NewtonBodySetUserData(body, user_data*)
  66. NewtonBodyGetUserData%(body)
  67. NewtonBodySetTransformCallback(body, callback)
  68. NewtonBodySetForceAndTorqueCallback(body, callback)
  69. NewtonBodySetDestructorCallback(body, callback)
  70. NewtonBodySetMassMatrix(body, mass#, Ixx#, Iyy#, Izz#)
  71. NewtonBodyGetMassMatrix(body, mass*, Ixx*, Iyy*, Izz*)
  72. NewtonBodyGetInvMass(body, inv_mass*, inv_Ixx*, inv_Iyy*, inv_Izz*)
  73. NewtonBodySetMatrix(body, matrix_ptr*)
  74. NewtonBodySetMatrixRecursive(world, body, matrix_ptr*)
  75. NewtonBodyGetMatrix(body, matrix_ptr*)
  76. NewtonBodySetForce(body, force_ptr*)
  77. NewtonBodyAddForce(body, force_ptr*)
  78. NewtonBodyGetForce(body, vector*)
  79. NewtonBodySetTorque(body, torque_ptr*)
  80. NewtonBodyAddTorque(body, torque_ptr*)
  81. NewtonBodyGetTorque(body, vector*)
  82. NewtonBodyAddBuoyancyForce(body, fluid_density#, fluid_linear_viscosity#, fluid_angular_viscosity#, gravity_vector*, buoyancy_plane)
  83. NewtonBodySetCollision(body, collision)
  84. NewtonBodyGetCollision%(body)
  85. NewtonBodySetMaterialGroupID(body, id)
  86. NewtonBodyGetMaterialGroupID%(body)
  87. NewtonBodySetAutoFreeze(body, state)
  88. NewtonBodyGetAutoFreeze%(body)
  89. NewtonBodySetFreezeTreshold(body, freeze_accel_mag#, freeze_alpha_mag#, freeze_speed_mag#, freeze_omega_mag#)
  90. NewtonBodyGetFreezeTreshold(body, freeze_accel_mag*, freeze_alpha_mag*, freeze_speed_mag*, freeze_omega_mag*)
  91. NewtonBodyGetAABB(body, p0*, p1*)
  92. NewtonBodySetVelocity(body, velocity*)
  93. NewtonBodyGetVelocity(body, velocity*)
  94. NewtonBodySetOmega(body, omega*)
  95. NewtonBodyGetOmega(body, omega*)
  96. NewtonBodySetLinearDamping(body, linear_damp#)
  97. NewtonBodyGetLinearDamping(body, linear_damp*)
  98. NewtonBodySetAngularDamping(body, angular_damp*)
  99. NewtonBodyGetAngularDamping(body, angular_damp*)
  100. NewtonBodyRayIntersect#(body, p0*, p1*, normal_ptr*)
  101. NewtonAddBodyImpulse(world, body, point_delta_veloc*, point_posit*)


Comments :


Bot Builder(Posted 1+ years ago)

 Interesting. Is this the full command set?  No joints, static meshs, or breakage apparently... I'm gonna try to get it working and have newton dll run a previously made toko demo, so we can compare.  I'm gonna bet that newton has better performance but 4x the time. This is all completly unbased though. the download hasn't finished :D[edit] nevermind - Checked the docs. there are tons of commands....


JaviCervera(Posted 1+ years ago)

 <div class="quote"> Checked the docs. there are tons of commands.... </div>Hmmm... I think I have wrapped the full command set of Newton. Maybe I don't have the latest version? I have to check that. About joints... I have seen a demo with ragdolls, so I assume it has. Static meshes: if they're used for collision detection, you can use these commands to create a collidable mesh:
Code: [Select]
NewtonCreateTreeCollision%(world, user_callback)
NewtonTreeCollisionBeginBuild(tree_collision)
NewtonTreeCollisionAddFace(tree_collision, vertex_count, vertex_ptr, stride_in_bytes, face_attribute)
NewtonTreeCollisionEndBuild(tree_collision, optimize)
About breakage, I am not sure.


Bot Builder(Posted 1+ years ago)

 <div class="quote"> NewtonCreateNewtonWorld* NewtonCreate(NewtonAllocMemory mallocFnt,NewtonFreeMemory mfreeFnt)Create an instance of the Newton world.ParametersNewtonAllocMemory mallocFnt    -    is a pointer to the memory allocator callback function. If this parameter is NULL the standard malloc function is used.NewtonFreeMemory mfreeFnt    -    is a pointer to the memory release callback function. If this parameter is NULL the standard free function is used.Returna pointer to an instance of the Newton world.Remarksthis function must be called before any of the other API function. </div>Uh-oh. Trouble on the first command. I'm not sure if blitz can pass a null pointer, but I definitly know blitz doesn't support function pointers. This is probably why tokamak had to be wrapped.[edit] doh. I just realised you stated that in the description. To bad. I can't write dlls (I've tried) even though I'm semi-familiar with C/C++ syntax (heheh).


JaviCervera(Posted 1+ years ago)

 You must call it this way: NewtonCreate(0, 0)


Bot Builder(Posted 1+ years ago)

 hmmm. Doh!
Code: [Select]
Graphics3D 640,480

wrld=NewtonCreate(0, 0)

matrix=CreateBank(4*4*4)

vec=CreateBank(12)

PokeFloat vec,0,5
PokeFloat vec,4,5
PokeFloat vec,8,5

sph=NewtonCreateBody(wrld,NewtonCreateSphere(wrld,1,matrix))
NewtonBodySetMassMatrix sph,1,1,1,1
;cam=CreateCamera()

While Not KeyHit(1)
NewtonUpdate wrld,.01
NewtonAddBodyImpulse wrld,sph,vec,vec
NewtonBodyGetMatrix sph,matrix

For a=0 To 3
DebugLog PeekFloat(matrix,a*12)+Chr$(9)+PeekFloat(matrix,a*12+4)+Chr$(9)+PeekFloat(matrix,a*12+8)+Chr$(9)+PeekFloat(matrix,a*16+12)
Next
DebugLog "****"
;RenderWorld
;Flip
Wend

NewtonDestroy wrld
Not sure what I'm doing wrong... the body's matrix should store transformation and rotation info... The matrix should at least be changing.  Any ideas? at least this doesn't give a MAV...


Lyon(Posted 1+ years ago)

 How can i use matrix data ???


Zethrax(Posted 1+ years ago)

 Just thought I'd give you a heads up on this post from the Newton forum, Jedive. The Newton guy mentions that you're missing some of the Newton physics engine functions in your declarations file.<a href="http://www.physicsengine.com/forum/viewtopic.php?t=287" target="_blank">http://www.physicsengine.com/forum/viewtopic.php?t=287[/url]


JaviCervera(Posted 1+ years ago)

 Thx Axeman. I haven't played much with this yet, because i am busy with other things. It seems that I was wrapping an old version, and that Newton has evolved since then. I'll add the rest of the commands soon, and I hope I can find the time to make a small demo.


Bot Builder(Posted 1+ years ago)

 see. toldja :P Well, sorta...<div class="quote">  Is this the full command set? No joints, static meshs, or breakage apparently... </div>Anyway, Andris - Complex topic indeed. My comment here has somewhat of an explanation as well as some links.I was going to suggest the usage of the undocumented command SetMatElement, but it doesn't highlight in the editer. Am I insane or did this used to exist? Regardless of sanity, it'd be very handy in this situation and no cray-zay matrix math would need to be done.


Chroma(Posted 1+ years ago)

 Is this working yet guys? [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal