Code archives/User Libs/Newton physics

This code has been declared by its author to be Public Domain code.

Download source code

Newton physics by JaviCervera2004
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 http://www.physicsengine.com which contains the DLL you need and the documentation of the API.

Enjoy :)
.lib "Newton.dll"

;World interface
NewtonCreate%(malloc, free)
NewtonDestroy(world)
NewtonUpdate(world, timestep#)
NewtonDestroyAllBodies(world)
NewtonSetWorldSize(world, min_ptr*, max_ptr*)
NewtonSetBodyLeaveWorldEvent(world, callback)
NewtonWorldFreezeBody(world, body)
NewtonWorldUnfreezeBody(world, body)

;GroupID interface
NewtonMaterialGetDefaultGroupID%(world)
NewtonMaterialCreateGroupID%(world)
NewtonMaterialDestroyAllGroupID(world)
NewtonMaterialSetDefaultCollidable(world, id0, id1, state)
NewtonMaterialSetDefaultFriction(world, id0, id1, static_friction#, kinetic_friction#)
NewtonMaterialSetDefaultElasticity(world, id0, id1, elastic_coef#)
NewtonMaterialSetDefaultSoftness(world, id0, id1, softness_coef#)
NewtonMaterialSetCollisionCallback(world, id0, id1, user_data*, begin_callback, process_callback, end_callback)

;Contact behaviour control interface
NewtonMaterialDisableContact(material)
NewtonMaterialGetContactUserData%(material)
NewtonMaterialGetContactNormalSpeed#(material, contact_handle)
NewtonMaterialGetContactTangentSpeed#(material, contact_handle, index)
NewtonMaterialGetContactPositionAndNormal(material, posit_ptr*, normal_ptr*)
NewtonMaterialGetContactTangentDirections(material, dir0*, dir1*)
NewtonMaterialSetContactSoftness(material, softness#)
NewtonMaterialSetContactElasticity(material, restitution#)
NewtonMaterialSetContactFrictionState(material, state, index)
NewtonMaterialSetContactStaticFrictionCoef(material, coef#, index)
NewtonMaterialSetContactKineticFrictionCoef(material, coef#, index)
NewtonMaterialSetContactTangentAcceleration(material, accel#, index)
NewtonMaterialContactRotateTangentDirections(material,align_vector*)

;Convex collision primitives interface
NewtonCreateSphere%(world, radius#, offset_matrix*)
NewtonCreateBox%(world, dx#, dy#, dz#, offset_matrix*)

;Complex collision primitives interface
NewtonCreateCompoundCollision%(world, count, collision_primitive_array*)
NewtonCreateUserMeshCollision%(world, min_box*, max_box*, user_data*, collide_callback, rayhit_callback, destroy_callback)
NewtonCreateTreeCollision%(world, user_callback)
NewtonTreeCollisionBeginBuild(tree_collision)
NewtonTreeCollisionAddFace(tree_collision, vertex_count, vertex_ptr, stride_in_bytes, face_attribute)
NewtonTreeCollisionEndBuild(tree_collision, optimize)
NewtonTreeCollisionSerialize(tree_collision, callback, serialize_handle*)
NewtonCreateTreeCollisionFromSerialization%(world, user_callback, deserialize_function, serialize_handle)
NewtonTreeCollisionGetFaceAtribute%(tree_collision, face_index_array*)
NewtonTreeCollisionSetFaceAtribute(tree_collision, face_index_array*, attribute)

;Collision miscelanios interface
NewtonReleaseCollision(world, collision_ptr)
NewtonCollisionCalculateAABB(collision_ptr, offset_matrix*, p0*, p1*)

;Transforms utility functions
NewtonGetEulerAngle(matrix*, angles*)
NewtonSetEulerAngle(matrix*, angles*)

;Rigid Body interface
NewtonCreateBody%(world, collision_ptr)
NewtonDestroyBody(world, body)
NewtonBodySetUserData(body, user_data*)
NewtonBodyGetUserData%(body)
NewtonBodySetTransformCallback(body, callback)
NewtonBodySetForceAndTorqueCallback(body, callback)
NewtonBodySetDestructorCallback(body, callback)
NewtonBodySetMassMatrix(body, mass#, Ixx#, Iyy#, Izz#)
NewtonBodyGetMassMatrix(body, mass*, Ixx*, Iyy*, Izz*)
NewtonBodyGetInvMass(body, inv_mass*, inv_Ixx*, inv_Iyy*, inv_Izz*)
NewtonBodySetMatrix(body, matrix_ptr*)
NewtonBodySetMatrixRecursive(world, body, matrix_ptr*)
NewtonBodyGetMatrix(body, matrix_ptr*)
NewtonBodySetForce(body, force_ptr*)
NewtonBodyAddForce(body, force_ptr*)
NewtonBodyGetForce(body, vector*)
NewtonBodySetTorque(body, torque_ptr*)
NewtonBodyAddTorque(body, torque_ptr*)
NewtonBodyGetTorque(body, vector*)
NewtonBodyAddBuoyancyForce(body, fluid_density#, fluid_linear_viscosity#, fluid_angular_viscosity#, gravity_vector*, buoyancy_plane)
NewtonBodySetCollision(body, collision)
NewtonBodyGetCollision%(body)
NewtonBodySetMaterialGroupID(body, id)
NewtonBodyGetMaterialGroupID%(body)
NewtonBodySetAutoFreeze(body, state)
NewtonBodyGetAutoFreeze%(body)
NewtonBodySetFreezeTreshold(body, freeze_accel_mag#, freeze_alpha_mag#, freeze_speed_mag#, freeze_omega_mag#)
NewtonBodyGetFreezeTreshold(body, freeze_accel_mag*, freeze_alpha_mag*, freeze_speed_mag*, freeze_omega_mag*)
NewtonBodyGetAABB(body, p0*, p1*)
NewtonBodySetVelocity(body, velocity*)
NewtonBodyGetVelocity(body, velocity*)
NewtonBodySetOmega(body, omega*)
NewtonBodyGetOmega(body, omega*)
NewtonBodySetLinearDamping(body, linear_damp#)
NewtonBodyGetLinearDamping(body, linear_damp*)
NewtonBodySetAngularDamping(body, angular_damp*)
NewtonBodyGetAngularDamping(body, angular_damp*)
NewtonBodyRayIntersect#(body, p0*, p1*, normal_ptr*)
NewtonAddBodyImpulse(world, body, point_delta_veloc*, point_posit*)

Comments

Bot Builder2004
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....


JaviCervera2004
Checked the docs. there are tons of commands....
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:
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 Builder2004

NewtonCreate
NewtonWorld* NewtonCreate(
NewtonAllocMemory mallocFnt,
NewtonFreeMemory mfreeFnt)
Create an instance of the Newton world.

Parameters

NewtonAllocMemory 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.

Return
a pointer to an instance of the Newton world.

Remarks
this function must be called before any of the other API function.



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).


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


Bot Builder2004
hmmm. Doh!
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...


Lyon2004
How can i use matrix data ???


Zethrax2004
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.

http://www.physicsengine.com/forum/viewtopic.php?t=287


JaviCervera2004
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 Builder2004
see. toldja :P Well, sorta...
Is this the full command set? No joints, static meshs, or breakage apparently...


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.


Chroma2004
Is this working yet guys?


Code Archives Forum