Irrlicht and Newton

BlitzMax Forums/BlitzMax Programming/Irrlicht and Newton

Haramanai(Posted 2005) [#1]
Today I was trying to use gg.irrBMax mod by gman with Newton mod by thierry-myke and I came to acomplish the first tutorial of newton(I hope so).
just copy paste the code and be sure to have properly installed gg.irrBMax and Newton.PUB




TMyke(Posted 2005) [#2]
Super, I'like it. it's so good
I hope that that will give a speed to the fan of Irrlicht


Haramanai(Posted 2005) [#3]
Myke if you have some more examples with newton and eliza
and if it is not trouble for you to post them pls post them cos
I never programmed c++. and I cannot find my way in newton.


Haramanai(Posted 2005) [#4]
the two modules can make and tutorial two.


TMyke(Posted 2005) [#5]
several things, initially download the SDK Newton, you will have Doc,
with the explanation for each function what helps one can nevertheless.
In continuation you can go to see on:
http://irrlicht.sourceforge.net/tutorials.html
it's C++, but you should nevertheless find the broad outline to progress.
For others tutotials I will try, but I am testing integer Newtow directly in Eliza, and is not easy.
if I arrive there, I would make Doc, with tutorials.
But that takes time to me. Goes on TMyke.fr from time to time, I will make an update very soon
:)


Haramanai(Posted 2005) [#6]
good to know that you still work on your two projects.


Haramanai(Posted 2005) [#7]
Ok the second tutorial its almost ready.
Here is the code.


the proble that i have here and not working the tutorial right is that collisions are not working if not all Irrlicht Nodes have inique names.
plus the pointer PhysicsSetTransform function if the nodes have the same name(like the cubes in the tutorial) allways call the last added node.

If someone finds out what going wrong pls say.


gman(Posted 2005) [#8]
greetings Haramanai :) i will not be able to look at this until this evening (im -6 GMT) but i will see if i can see anything.


Haramanai(Posted 2005) [#9]
Thanks . I am going to wait for your answer


gman(Posted 2005) [#10]
went down with the flu sunday afternoon :( i am feeling a bit better and will try to take a look at this tonight.


gman(Posted 2005) [#11]
sorry it took me so long. my girls have the flu now too so we've been pretty busy at home. i found your objects not moving issue. essentially you were storing a reference to the BMAX box object in the newton userdata but this does not increment the BMAX reference counter and those variables were falling out of scope. the better way to do it, would be to store the reference to the C++ object handle (remember the Irrlicht mod is a wrapper and the C++ object handle is what the Irrlicht scene graph keeps a reference to) and rebuild the scenenode from the handle. for some reason i had to tweak the mass calculation, but here is a modified version of your sample thats working. all my changes have comments starting with "GG:".
Strict
Framework PUB.Newton
Import GG.IrrBMax
Import BRL.PNGLoader

Local device:T_irrIrrlichtDevice = T_irrIrrlichtDevice.create(EDT_OPENGL , T_irrDimension2d_s32.create(640 , 480) , 16 , False , False , False , 0)
Global driver:T_irrIVideoDriver = device.getVideoDriver()
Global smgr:T_IrrISceneManager = device.getSceneManager() 
'Local Tex:T_irrITexture = driver.getTexture("brlLogo.png")
Local camera:T_irrICameraSceneNode = smgr.addCameraSceneNodeFPS()
camera.setPosition(T_irrVector3df.createFromVals(-20,0,0))
camera.setRotation(T_irrVector3df.createFromVals(0 , 90 , 0))

Global nWorld:Byte Ptr = newtonCreate(PhysicsAlloc , PhysicsFree)

initScene()
Local Timer:Int = MilliSecs()
While (device.run())
	Timer:+MilliSecs()
	newtonUpdate(nWorld , Timer)
	Timer = MilliSecs()
	'NewtonBodyGetMatrix(box , Varptr Mat[0])
	'T_irrSetMatrix(node , Mat)
	driver.beginScene(True , True , T_irrSColor.createFromVals(0,200 , 200 , 200))
	smgr.drawAll()
	
	driver.endScene()
	FlushMem
Wend

Rem '''''''''''''''''''''''''''''''''''/////////////////////////////////////   The Convertion Matrix Functions
		T_irrGetMatrix is the function to get the matrix from the Irrlecht SceneNode And pass it
	to an array to be used by Newton.
		T_irrSetMatrix is the function to pass the Matrix from Newton to the Irrlicht SceneNode
EndRem
Function T_irrGetMatrix(NSceneNode:T_irrISceneNode , NewtonMat:Float Ptr)
	Local Euler:Float[3]
	Euler[0] = NSceneNode.getRotation().getX() * (Pi / 180.0)
	Euler[1] = NSceneNode.getRotation().getY() * (Pi / 180.0)
	Euler[2] = NSceneNode.getRotation().getZ() * (Pi / 180.0)
	NewtonSetEulerAngle(Varptr Euler[0] , Varptr newtonMat[0])
	Newtonmat[12] = NSceneNode.getPosition().getX()
	newtonMat[13] = NSceneNode.getPosition().getY()
	NewtonMat[14] = NSceneNode.getPosition().getZ()	
End Function
Function T_irrSetMatrix(NSceneNode:T_irrISceneNode , NewtonMat:Float Ptr)
	Local Euler:Float[3]
	NewtonGetEulerAngle(Varptr NewtonMat[0] , Varptr Euler[0])
	NSceneNode.setPosition(T_irrVector3df.createFromVals(NewtonMat[12] , NewtonMat[13] , NewtonMat[14]))
	NSceneNode.setRotation(T_irrVector3df.createfromVals(Euler[0] * (180 / Pi) , Euler[1] * (180 / Pi) , Euler[2] * (180 / Pi)))
End Function
Rem 
					THE FUNCTIONS FROM TUTORIAL 2
End Rem
Function	PhysicsAlloc:Byte Ptr(sizeInBytes:Int)
	Return MemAlloc(sizeInBytes)
End Function
Function    PhysicsFree( memptr:Byte Ptr, sizeInBytes:Int)
	MemFree (memptr, sizeInBytes)
End Function
' set the tranformation of a rigid body
Function  PhysicsApplyForceAndTorque (Body:Byte Ptr)
	Local mass:Float
	Local Ixx:Float
	Local Iyy:Float
	Local Izz:Float
	NewtonBodyGetMassMatrix (body,Varptr mass, Varptr Ixx, Varptr Iyy, Varptr Izz);
	'Local force:Float[] =  [0.0, -mass * 9.8 , 0.0]
	' GG: messed with the mass multiplier to make it look like the tutorial.  have no idea why this is different.
	Local force:Float[] =  [0.0, -mass * 410 , 0.0]
	NewtonBodySetForce (body, Varptr force[0])
End Function
Function  PhysicsSetTransform (body:Byte Ptr, NewtonMat:Float Ptr)
	Local primitive:T_irrISceneNode
	'// get the graphic Object form the rigid body
	' GG: changed to recreate the scenenode based on the C++ object handle
	primitive = T_irrISceneNode.CreateFromHandle(Int(newtonBodyGetUserData(body)),False)
	
	'// set the transformation matrix For this rigid body
	newtonBodyGetMatrix(body , Varptr NewtonMat[0])
	T_irrSetMatrix(Primitive , Varptr NewtonMat[0])
	
End Function
Function PhysicsBodyDestructor(body:Byte Ptr)
	Local primitive:T_irrISceneNode
	'// get the graphic Object form the rigid body
	' GG: changed to recreate the scenenode based on the C++ object handle
	primitive = T_irrISceneNode.CreateFromHandle(Int(newtonBodyGetUserData(body)),False)
	'// destroy the graphic Object
	primitive.remove()
	
End Function
Function CleanUp ()
	'// destroy the Newton world
	NewtonDestroy (nWorld);
	
End Function
Function InitScene()
	Global Texture:T_irrITexture = Driver.getTexture("brllogo.png") 
	Global box:T_irrISceneNode
	Global IFloor:T_irrISceneNode
	Global boxBody:Byte Ptr
	Global floorBody:Byte Ptr
	Global collision:Byte Ptr
	Global Mat:Float[16]
	'// create the newton world
	
	'// set the linear solver model For faster speed 
	NewtonSetSolverModel(nWorld, 8)
	'// set the adpative friction model For faster speed 
	NewtonSetFrictionModel(nWorld, 1)
	
	'// Set the termination Function
	'atexit(CleanUp);   '//// I Haven't Found The Apropriaty Function for Irrlicht
	
	'// create the the Floor graphic objects
	IFloor = smgr.addTestSceneNode(100 )
	IFloor.setPosition(T_irrVector3df.createFromVals(0,-100 , 0))
	collision = newtonCreateBox(nWorld , 100.0 , 100.0 , 100.0 , Null)
	FloorBody = newtonCreateBody(nWorld , collision)
	
	'// set the transformation For this rigid body
	T_irrGetMatrix(IFloor ,Varptr Mat[0])
	NewtonBodySetMatrix(floorBody,Varptr Mat[0])
	
	'// save the pointer To the graphic Object with the body.
	' GG: changed to store the scenenode C++ object handle instead of a reference to the BMAX object with falls out of scope
	NewtonBodySetUserData(floorBody,Byte Ptr(IFloor.handle))
	
	'// set a destrutor For this rigid body
	NewtonBodySetDestructorCallback(floorBody, PhysicsBodyDestructor);
	
	'// set the initial size
	'size = dVector(0.5f, 0.5f, 0.5f);    Not really nesesery(I think so)
	'// create the collision 
	collision = NewtonCreateBox(nWorld, 0.5 , 0.5, 0.5 , Null) 
	Local location:Float[] = [0.0 , 0.0 , 0.0]
	location[0] = -10.0   'FOR  X
	For Local k:Int = 0 To 9
		location[2] = 0.0	'For Z
		For Local J:Int = 0 To 9
			location[1] = 2.0	'For Y
			For Local i:Int = 0  To 9
				box = smgr.addTestSceneNode(0.5)
				box.SetMaterialTexture(0 , Texture)
				box.setPosition(T_irrVector3df.createFromVals(location[0] ,location[1] , location[2]))
				boxBody = newtonCreateBody(nWorld , collision)				
				' GG: changed to store the scenenode C++ object handle instead of a reference to the BMAX object with falls out of scope
				newtonBodySetUserData(boxBody ,Byte Ptr(box.handle))		
				'// set a destrutor For this rigid body
				NewtonBodySetDestructorCallback(boxBody, PhysicsBodyDestructor)
				'// set the tranform call back Function
				NewtonBodySetTransformCallback(boxBody, PhysicsSetTransform)
				'// set the force And torque call back funtion
				NewtonBodySetForceAndTorqueCallback(boxBody, PhysicsApplyForceAndTorque)
				'// set the mass matrix
				'//NewtonBodySetMassMatrix (boxBody, 1.0f, 1.0f / 6.0f, 1.0f / 6.0f, 1.0f  / 6.0f);
				NewtonBodySetMassMatrix(boxBody, 1.0, 1.0, 1.0, 1.0)				
				'// set the matrix For tboth the rigid nody And the graphic body
				T_irrGetMatrix(box , Varptr Mat[0])
				NewtonBodySetMatrix(boxBody,Varptr Mat[0])
				PhysicsSetTransform(boxBody, Varptr Mat[0])
				Location[1] :+ 0.5 * 2.0
			Next
			Location[2] :- 0.5 * 4.0 	
		Next
		location[0] :- 0.5 * 4.0 
	Next
	'// Release the collsion geometry when Not need it
	NewtonReleaseCollision(nWorld, collision)

EndFunction



Haramanai(Posted 2005) [#12]
I wise the best for your family.(sorry English it's not my first language and I don't know what to say in this situatuions)

Thanks for the clue.


Haramanai(Posted 2005) [#13]
Ok here is the working Tutorial two with some changes so it will be more visual and with the forgoten line for collision.




gman(Posted 2005) [#14]

I wise the best for your family


correct would be "I wish the best for your family" and thank you :)


Haramanai(Posted 2005) [#15]
Thanks to gman all moves.
I have sniped the code from the tutorial of the Irrlicht web site and translated to fit gg.ibmax and newton.mod and created a function so to be able to use Maps with newton.
Here is the function:


beware that if you move the map in irrlicht you lose all the compartibility.


DirtBikeDude(Posted 2006) [#16]
Haramanai, I was experimenting with you code. I noticed if the frame rate drops the physics does'nt match the ammount of time elapsed. The physics syncronizes with the frames per second instead of the timescale. Is this how newton works or could the example be changed?


Haramanai(Posted 2006) [#17]
I don't know DirtBikeDude. My expirience with Newton was only this. After that I haven't touched newton with Irrlicht. I think that you will not have any problem by posting to Newton's or Irrlicht's forums about any problems you may have or search fo answers as the wrappers are very well done.


ninjarat(Posted 2007) [#18]
Hey, this thread could use some reviving. So here's my newer irrlicht compatible version of the mesh to tree function. And guess what? I doesn't work. Could someone tell me what I did wrong?




ninjarat(Posted 2007) [#19]


This one Returns the Body. It still doesn't work, but at least it doesn't crash. Still don't know why...


ninjarat(Posted 2007) [#20]
Could someone give me a hand with this?


gman(Posted 2007) [#21]
greetings :) i will try to take a look at this this evening.


René(Posted 2007) [#22]
Hi there. Do you have any working links to a Newton wrapper for BMax?


ninjarat(Posted 2007) [#23]
It's quite a minimal wrapper actually. It's why I like it. Gets the job done without a lot of extra bullshit. I'll just post it.



Just install as a module and you're ready to go! I think it's by Leadwerks, but I could be mistaken.


ninjarat(Posted 2007) [#24]
Oh, hey, I forgot, you need the DLL/LIB. :P You can find 'em at http://www.newtondynamics.com/ in the SDK download. Glad to see someone else is showing an interest. Have a good one. :)


ninjarat(Posted 2007) [#25]
While I'm at it I'll just show you the module I'm working on; it gives what I showed you guys some context.




klepto2(Posted 2007) [#26]
Yes, this one is by leadwerks and really nice. I currently use this in addition with minib3d and I have also added some functions to it which I have needed. I have also have made it crossplattform.

Get the crossplattform module here: http://www.blitzbasic.com/Community/posts.php?topic=67327


ninjarat(Posted 2007) [#27]
Here's my Irrlicht.Extended btw. Nothing impressive. Just a pair of convenience functions.



EDIT:Added another function.


ninjarat(Posted 2007) [#28]
Oh, and GMan, thanks for looking at it. I sincerely appreciate it.

Incedentally, does the idea for your screen name come from Half Life? :P


René(Posted 2007) [#29]
Thank you ninjarat for the wrapper code!


ninjarat(Posted 2007) [#30]
You're welcome :)


ninjarat(Posted 2007) [#31]
Anyone gotten anywhere with it? I still can't do the level mesh collision tree properly.


gman(Posted 2007) [#32]
@ninjarat - unfortunately i didnt get the time last night. i will look as soon as i can.

Incedentally, does the idea for your screen name come from Half Life? :P


nope :) gman comes from my initials... GG. tends to be what everyone starts calling me wherever i go.


ninjarat(Posted 2007) [#33]
Oh. My initials are BW, or WW. Either way it's bad. Billium, Willard, Wet Willy...


gman(Posted 2007) [#34]
how are you calling the AnimatedMeshToNewtonCollisionTree function? what are the parameters?


ninjarat(Posted 2007) [#35]
Syntax:
AnimatedMeshToNewtonCollisionTree:Byte Ptr(nWorld:Byte Ptr,g_map:IAnimatedMesh,g_newtonMap:Byte Ptr)


Arguments:
- nWorld is the current Newton world.
- g_map is the animated mesh to create the collision body from.
- g_newtonMap is the collision tree object pointer.

Returns: Pointer to the Newton collision body generated.


gman(Posted 2007) [#36]
sorry should have been more clear :) what model/type are you using for the IAnimatedMesh instance?


ninjarat(Posted 2007) [#37]
Also, I think I may be taking an entirely wrong approach to this. Any chance you could show me how to do this? You're the Irrlicht expert. And I'm fairly certain you would know something about Newton.


ninjarat(Posted 2007) [#38]
oh, yeah, I'm using a B3D mesh. But I want it to work with all mesh formats.


gman(Posted 2007) [#39]
here is the example using the sydney model with the AnimatedMeshToCollisionTree function. you should be able to change the node at the top right before the initScene() function to test out other meshes. i did comment out the set the world size part of the AnimatedMeshToCollisionTree function. please note that different vertex types will cause problems with the function. i will see if i can whip up a more compatible function sometime tomorrow.
SuperStrict
Framework PUB.Newton
Import Irrlicht.Core
Import BRL.PNGLoader
StartNewton()

Local device:IrrlichtDevice = IrrlichtDevice.create(EDT_OPENGL , Dimension2di.create(640 , 480) , 16 , False , False , False , Null)
Global driver:IVideoDriver = device.getVideoDriver()
Global smgr:ISceneManager = device.getSceneManager() 
'Local Tex:ITexture = driver.getTexture("brlLogo.png")
Local camera:ICameraSceneNode = smgr.addCameraSceneNodeFPS()
camera.setPosition(Vector3df.createFromVals(-19,-49,-30))
camera.setRotation(Vector3df.createFromVals(0 , 0 , 0))
Local light:ILightSceneNode = smgr.addLightSceneNode( Null , Vector3df.createfromVals(-20 , -40 , -30))

Global nWorld:Byte Ptr = NewtonCreate(PhysicsAlloc , PhysicsFree)

' ------------------------------------
' new stuff
' ------------------------------------

' ------------------------------------
' TODO: edit this section for different meshes/nodes
' ------------------------------------
Global mesh:IAnimatedMesh=smgr.getMesh("sydney.md2")
Global node:IAnimatedMeshSceneNode=smgr.addAnimatedMeshSceneNode(mesh)
node.setAnimationSpeed(0)
node.setPosition(_VECTOR3DF(-25,-40,0))

' ------------------------------------
' setup node in newton
' ------------------------------------
Local meshcollision:Byte Ptr = NewtonCreateTreeCollision(nWorld, Null)
Local meshBody:Byte Ptr = AnimatedMeshToNewtonCollisionTree(nWorld, mesh, meshcollision)
Local MeshMat:Float[16]

NewtonBodySetUserData(meshBody,Byte Ptr(node.handle))
'// set a destrutor For this rigid body
NewtonBodySetDestructorCallback(meshBody, PhysicsBodyDestructor)
'// set the transformation For this rigid body
GetMatrix(node ,Varptr MeshMat[0])
NewtonBodySetMatrix(meshBody,Varptr MeshMat[0])
newtonReleaseCollision(nWorld , meshcollision)
' ------------------------------------
	
initScene()

' ------------------------------------

Local Timer:Int = MilliSecs()
While (device.run())
	Timer:+MilliSecs()
	NewtonUpdate(nWorld , 0.001)
	Timer = MilliSecs()
	'NewtonBodyGetMatrix(box , Varptr Mat[0])
	'SetMatrix(node , Mat)
	driver.beginScene(True , True , SColor.createFromVals(0,200 , 200 , 200))
	smgr.drawAll()
	
	driver.endScene()
Wend
StopNewton()

Rem '''''''''''''''''''''''''''''''''''/////////////////////////////////////   The Convertion Matrix Functions
		GetMatrix is the function to get the matrix from the Irrlecht SceneNode And pass it
	to an array to be used by Newton.
		SetMatrix is the function to pass the Matrix from Newton to the Irrlicht SceneNode
EndRem
Function GetMatrix(NSceneNode:ISceneNode , NewtonMat:Float Ptr)
	Local Euler:Float[3]
	Euler[0] = NSceneNode.getRotation().getX() * (Pi / 180.0)
	Euler[1] = NSceneNode.getRotation().getY() * (Pi / 180.0)
	Euler[2] = NSceneNode.getRotation().getZ() * (Pi / 180.0)
	NewtonSetEulerAngle(Varptr Euler[0] , Varptr newtonMat[0])
	Newtonmat[12] = NSceneNode.getPosition().getX()
	newtonMat[13] = NSceneNode.getPosition().getY()
	NewtonMat[14] = NSceneNode.getPosition().getZ()	
End Function
Function SetMatrix(NSceneNode:ISceneNode , NewtonMat:Float Ptr)
	Local Euler:Float[3]
	NewtonGetEulerAngle(Varptr NewtonMat[0] , Varptr Euler[0])
	NSceneNode.setPosition(Vector3df.createFromVals(NewtonMat[12] , NewtonMat[13] , NewtonMat[14]))
	NSceneNode.setRotation(Vector3df.createfromVals(Euler[0] * (180 / Pi) , Euler[1] * (180 / Pi) , Euler[2] * (180 / Pi)))
End Function
Rem 
					THE FUNCTIONS FROM TUTORIAL 2
End Rem
Function	PhysicsAlloc:Byte Ptr(sizeInBytes:Int)
	Return MemAlloc(sizeInBytes)
End Function
Function    PhysicsFree( memptr:Byte Ptr, sizeInBytes:Int)
	MemFree (memptr)
End Function
' set the tranformation of a rigid body
Function  PhysicsApplyForceAndTorque (Body:Byte Ptr)
	Local mass:Float
	Local Ixx:Float
	Local Iyy:Float
	Local Izz:Float
	NewtonBodyGetMassMatrix (body,Varptr mass, Varptr Ixx, Varptr Iyy, Varptr Izz);
	'Local force:Float[] =  [0.0, -mass * 9.8 , 0.0]
	' GG: messed with the mass multiplier to make it look like the tutorial.  have no idea why this is different.
	Local force:Float[] =  [0.0, -mass * 410 , 0.0]
	NewtonBodySetForce (body, Varptr force[0])
End Function
Function  PhysicsSetTransform (body:Byte Ptr, NewtonMat:Float Ptr)
	Local primitive:ISceneNode
	'// get the graphic Object form the rigid body
	' GG: changed to recreate the scenenode based on the C++ object handle
	primitive = ISceneNode.CreateFromHandle(Int(newtonBodyGetUserData(body)),False)
		
	'// set the transformation matrix For this rigid body
	newtonBodyGetMatrix(body , Varptr NewtonMat[0])
	SetMatrix(Primitive , Varptr NewtonMat[0])	
End Function
Function PhysicsBodyDestructor(body:Byte Ptr)
	Local primitive:ISceneNode
	'// get the graphic Object form the rigid body
	' GG: changed to recreate the scenenode based on the C++ object handle
	primitive = ISceneNode.CreateFromHandle(Int(newtonBodyGetUserData(body)),False)
	'// destroy the graphic Object
	primitive.remove()
	
End Function
Function CleanUp ()
	'// destroy the Newton world
	NewtonDestroy (nWorld);
	
End Function

Function InitScene()
	Global Texture:ITexture = Driver.getTexture("doc/brllogo.png") 
	Global box:ISceneNode
	Global IFloor:ISceneNode
	Global boxBody:Byte Ptr
	Global floorBody:Byte Ptr
	Global collision:Byte Ptr
	Global Mat:Float[16]
	'// create the newton world
	
	'// set the linear solver model For faster speed 
	NewtonSetSolverModel(nWorld, 8)
	'// set the adpative friction model For faster speed 
	NewtonSetFrictionModel(nWorld, 1)
	
	'// Set the termination Function
	'atexit(CleanUp);   '//// I Haven't Found The Apropriaty Function for Irrlicht
	
	' ------------------------------------
	' floor
	' ------------------------------------
	'// create the the Floor graphic objects
	IFloor = smgr.addCubeSceneNode(100 )
	'IFloor.setMaterialFlag(EMF_LIGHTING,True)
	IFloor.setMaterialTexture(0 , texture)
	IFloor.setPosition(Vector3df.createFromVals(0,-100 , 0))
	collision = newtonCreateBox(nWorld , 100.0 , 100.0 , 100.0 , Null)
	FloorBody = newtonCreateBody(nWorld , collision)
	
	'// set the transformation For this rigid body
	GetMatrix(IFloor ,Varptr Mat[0])
	NewtonBodySetMatrix(floorBody,Varptr Mat[0])
	
	'// save the pointer To the graphic Object with the body.
	' GG: changed to store the scenenode C++ object handle instead of a reference to the BMAX object with falls out of scope
	NewtonBodySetUserData(floorBody,Byte Ptr(IFloor.handle))
	
	'// set a destrutor For this rigid body
	NewtonBodySetDestructorCallback(floorBody, PhysicsBodyDestructor);
	' ------------------------------------
			
	' ------------------------------------
	' boxes
	' ------------------------------------
	'// set the initial size
	'size = dVector(0.5f, 0.5f, 0.5f);    Not really nesesery(I think so)
	'// create the collision 
	newtonReleaseCollision(nWorld , collision)
	collision = NewtonCreateBox(nWorld, 0.5 , 0.5, 0.5 , Null) 
	Local location:Float[] = [0.0 , 0.0 , 0.0]
	location[0] = -10.0   'FOR  X
	For Local k:Int = 0 To 9
		location[2] = 0.0	'For Z
		For Local J:Int = 0 To 9
			location[1] = 2.0	'For Y
			For Local i:Int = 0  To 9
				box = smgr.addCubeSceneNode(0.5)
				box.setMaterialFlag(EMF_LIGHTING,True)
				box.SetMaterialTexture(0 , Texture)
				box.setPosition(Vector3df.createFromVals(location[0] ,location[1] , location[2]))
				boxBody = newtonCreateBody(nWorld , collision)				
				' GG: changed to store the scenenode C++ object handle instead of a reference to the BMAX object with falls out of scope
				newtonBodySetUserData(boxBody ,Byte Ptr(box.handle))		
				'// set a destrutor For this rigid body
				NewtonBodySetDestructorCallback(boxBody, PhysicsBodyDestructor)
				'// set the tranform call back Function
				NewtonBodySetTransformCallback(boxBody, PhysicsSetTransform)
				'// set the force And torque call back funtion
				NewtonBodySetForceAndTorqueCallback(boxBody, PhysicsApplyForceAndTorque)
				'// set the mass matrix
				'//NewtonBodySetMassMatrix (boxBody, 1.0f, 1.0f / 6.0f, 1.0f / 6.0f, 1.0f  / 6.0f);
				NewtonBodySetMassMatrix(boxBody, 1.0, 1.0, 1.0, 1.0)				
				'// set the matrix For tboth the rigid nody And the graphic body
				GetMatrix(box , Varptr Mat[0])
				NewtonBodySetMatrix(boxBody,Varptr Mat[0])
				PhysicsSetTransform(boxBody, Varptr Mat[0])
				Location[1] :+ 0.5 * 2.0
			Next
			Location[2] :- 0.5 * 4.0 	
		Next
		location[0] :- 0.5 * 4.0 
	Next
	'// Release the collsion geometry when Not need it
	NewtonReleaseCollision(nWorld, collision)
	' ------------------------------------	
EndFunction

Function AnimatedMeshToNewtonCollisionTree:Byte Ptr(nWorld:Byte Ptr,g_map:IAnimatedMesh,g_newtonMap:Byte Ptr)
	NewtonTreeCollisionBeginBuild(g_newtonmap)
	Local cMeshBuffer:Int,j:Int
	Local v1i:Int,v2i:Int,v3i:Int
	Local mb:IMeshBuffer
	
	Local vArray:Float[9]	'vertex array (3*3 floats)
	
	Local tmpCount:Int = 0
	
	For cMeshBuffer=0 To g_map.getMesh(0).getMeshBufferCount() - 1
		
		mb = g_map.getMesh(0).getMeshBuffer(cMeshBuffer)
		
		Local mb_vertices:Array_S3DVertex=mb.getVertices()
		Local mb_indices:Array_u16=mb.getIndices()
		
		'add each triangle from the mesh
		For j=0 To mb.getIndexCount() - 1 Step 3
			v1i=mb_indices.elementAt(j)[0]
			v2i=mb_indices.elementAt(j+1)[0]
			v3i=mb_indices.elementAt(j+2)[0]
			
			vArray[0]=mb_vertices.elementAt(v1i).getPos().getX()
			vArray[1]=mb_vertices.elementAt(v1i).getPos().getY()
			vArray[2]=mb_vertices.elementAt(v1i).getPos().getZ()
			vArray[3]=mb_vertices.elementAt(v2i).getPos().getX()
			vArray[4]=mb_vertices.elementAt(v2i).getPos().getY()
			vArray[5]=mb_vertices.elementAt(v2i).getPos().getZ()
			vArray[6]=mb_vertices.elementAt(v3i).getPos().getX()
			vArray[7]=mb_vertices.elementAt(v3i).getPos().getY()
			vArray[8]=mb_vertices.elementAt(v3i).getPos().getZ()
			
			NewtonTreeCollisionAddFace(g_newtonmap, 3,Varptr vArray[0], 12, j)
		Next
		
	Next
	NewtonTreeCollisionEndBuild(g_newtonmap,0)
	Local g_newtonmapbody:Byte Ptr=NewtonCreateBody(nWorld,g_newtonmap)
	
Rem
After loading the bsp mesh in irrlicht we must create a newton collision tree consisting of all the triangles
in the bsp mesh. To do this we loop through the indices list And add vertices 3 at a time into newton. I should
mention here that I was lazy when writing this And just used a straight cast To video::S3DVertex2TCoords - this is because
bsp meshes have lightmap textures on them. If you are using a mesh with only 1 set of texture co-ordinates (like
a landscape Or .x level) Then use (video::S3DVertex).
Newton requires 2 pointers For every Object - a NewtonBody* For the rigid body, And a NewtonCollision* that
describes the objects geometry. After creating the tree collision geometry, we create a body For the map And
assign it To the collision. Bodys which have a tree collision For their geometry are static And their mass
will be ignored by newton - so you can only use meshes For your scenery!
End Rem
	Rem
	'set the newton world size based on the bsp size
	Local boxP0:Float[3] 
	Local boxP1:Float[3] 
	Local matrix:Float[16] 
	NewtonBodyGetMatrix g_newtonmapbody,Varptr matrix[0]
	NewtonCollisionCalculateAABB g_newtonmap,Varptr matrix[0],Varptr boxP0[0],Varptr boxP1[0]
	'pad the box
	boxP0[0]:-10
	boxP1[0]:+10
	boxP0[1]:-10
	boxP1[1]:+10
	boxP0[2]:-10
	boxP1[2]:+10
	NewtonSetWorldSize nWorld,Varptr boxP0[0],Varptr boxP1[0]
	EndRem
Rem
After loading the map, we can calculate the bounding box surrounding it And set the newton world size To
match. Newton should automatically disable anything which falls out of the map this way.
End Rem
	Return g_newtonmapbody
End Function



ninjarat(Posted 2007) [#40]
THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!! :D :D :D :D


Sorry. Excited. Right. Thanks, G. (is it all right if I just call you G?)


ninjarat(Posted 2007) [#41]
Oh, for genral interest, here's my first Newton test using primitives. All the textures may be found in the Irrlicht Examples media directory except for the skybox which is from Lugaru.

Ever played Lugaru? It's by a guy called David, and it is WICKED for a freelance game. It has the most innovative fighting system I've ever controlled in gameplay. Lugaru 1 is pretty minimal otherwise, but he is working on Lugaru 2, and is definitely on to something big. http://www.wolfire.com And for the record, he didn't pay me to say that. I just think it's cool.




ninjarat(Posted 2007) [#42]
I'm having trouble with this... .x and .b3d don't work, but .md2 works fine. Different vert formats, right? How do I solve this?


gman(Posted 2007) [#43]
this one should be compatible with any mesh type:
Function AnimatedMeshToNewtonCollisionTree:Byte Ptr(nWorld:Byte Ptr,g_map:IAnimatedMesh,g_newtonMap:Byte Ptr)
	NewtonTreeCollisionBeginBuild(g_newtonmap)
	Local cMeshBuffer:Int,j:Int
	Local v1i:Int,v2i:Int,v3i:Int
	Local mb:IMeshBuffer
	
	Local vArray:Float[9]	'vertex array (3*3 floats)
	
	Local tmpCount:Int = 0
	
	For cMeshBuffer=0 To g_map.getMesh(0).getMeshBufferCount() - 1
		
		mb = g_map.getMesh(0).getMeshBuffer(cMeshBuffer)

		Local vertType:Int=mb.getVertexType()
		
		Local arr_standardVerts:Array_S3DVertex=Null
		Local arr_2TCoordVerts:Array_S3DVertex2TCoords=Null
		Local arr_TangentVerts:Array_S3DVertexTangents=Null

		If vertType=EVT_STANDARD
			arr_standardVerts=mb.getVertices()
		Else If vertType=EVT_2TCOORDS
			arr_2TCoordVerts=mb.getVertices2TCoords()
		Else If vertType=EVT_TANGENTS
			arr_TangentVerts=mb.getVerticesTangents()
		EndIf
		
		Local mb_indices:Array_u16=mb.getIndices()
		
		'add each triangle from the mesh
		For j=0 To mb.getIndexCount() - 1 Step 3
			v1i=mb_indices.elementAt(j)[0]
			v2i=mb_indices.elementAt(j+1)[0]
			v3i=mb_indices.elementAt(j+2)[0]

			Local v1:Vertex=Null
			Local v2:Vertex=Null
			Local v3:Vertex=Null
			
			If vertType=EVT_STANDARD
				v1=Vertex(arr_standardVerts.elementAt(v1i))
				v2=Vertex(arr_standardVerts.elementAt(v2i))
				v3=Vertex(arr_standardVerts.elementAt(v3i))
			Else If vertType=EVT_2TCOORDS
				v1=Vertex(arr_2TCoordVerts.elementAt(v1i))
				v2=Vertex(arr_2TCoordVerts.elementAt(v2i))
				v3=Vertex(arr_2TCoordVerts.elementAt(v3i))
			Else If vertType=EVT_TANGENTS
				v1=Vertex(arr_tangentVerts.elementAt(v1i))
				v2=Vertex(arr_tangentVerts.elementAt(v2i))
				v3=Vertex(arr_tangentVerts.elementAt(v3i))
			EndIf
			
			vArray[0]=v1.getPos().getX()
			vArray[1]=v1.getPos().getY()
			vArray[2]=v1.getPos().getZ()
			vArray[3]=v2.getPos().getX()
			vArray[4]=v2.getPos().getY()
			vArray[5]=v2.getPos().getZ()
			vArray[6]=v3.getPos().getX()
			vArray[7]=v3.getPos().getY()
			vArray[8]=v3.getPos().getZ()
			
			NewtonTreeCollisionAddFace(g_newtonmap, 3,Varptr vArray[0], 12, j)
		Next
		
	Next
	NewtonTreeCollisionEndBuild(g_newtonmap,0)
	Local g_newtonmapbody:Byte Ptr=NewtonCreateBody(nWorld,g_newtonmap)
	
Rem
After loading the bsp mesh in irrlicht we must create a newton collision tree consisting of all the triangles
in the bsp mesh. To do this we loop through the indices list And add vertices 3 at a time into newton. I should
mention here that I was lazy when writing this And just used a straight cast To video::S3DVertex2TCoords - this is because
bsp meshes have lightmap textures on them. If you are using a mesh with only 1 set of texture co-ordinates (like
a landscape Or .x level) Then use (video::S3DVertex).
Newton requires 2 pointers For every Object - a NewtonBody* For the rigid body, And a NewtonCollision* that
describes the objects geometry. After creating the tree collision geometry, we create a body For the map And
assign it To the collision. Bodys which have a tree collision For their geometry are static And their mass
will be ignored by newton - so you can only use meshes For your scenery!
End Rem
	Rem
	'set the newton world size based on the bsp size
	Local boxP0:Float[3] 
	Local boxP1:Float[3] 
	Local matrix:Float[16] 
	NewtonBodyGetMatrix g_newtonmapbody,Varptr matrix[0]
	NewtonCollisionCalculateAABB g_newtonmap,Varptr matrix[0],Varptr boxP0[0],Varptr boxP1[0]
	'pad the box
	boxP0[0]:-10
	boxP1[0]:+10
	boxP0[1]:-10
	boxP1[1]:+10
	boxP0[2]:-10
	boxP1[2]:+10
	NewtonSetWorldSize nWorld,Varptr boxP0[0],Varptr boxP1[0]
	EndRem
Rem
After loading the map, we can calculate the bounding box surrounding it And set the newton world size To
match. Newton should automatically disable anything which falls out of the map this way.
End Rem
	Return g_newtonmapbody	
End Function



ninjarat(Posted 2007) [#44]
Also, transforming the mesh (even before I create the tree) doesn't seem to change anything (in terms of collision.)


ninjarat(Posted 2007) [#45]
thx, G.


ninjarat(Posted 2007) [#46]
Hey, here's mine! It's strongly based on yours but it has an add-on. :D Kinda proud!



Added a still mesh function.

The way my test code works is that it takes an animated mesh, gets the first (and only) frame of it, and runs it through a mesh manipulator scale operation to fix the scale (needs to be about ten times bigger.)

Then creates a scene node for the still mesh, and also creates a collision tree. Then drops the primitives just like in the first demo.

Just as soon as I get this working well, I'll show everyone the source.

Have a good one, and you'll DEFINITELY be in the credits of my next game, (that uses this) G! :)


ninjarat(Posted 2007) [#47]
Hey, G. I'd like some help with ragdolls. As in, how do you make one out of an Irrlicht skinned mesh (of any format?)


ninjarat(Posted 2007) [#48]
Pretty neat, eh?



Pub.NewtonAndIrrlicht:


Irrlicht.Extended:



gman(Posted 2007) [#49]
greetings :) lots of stuff going on in here... glad i could help :) do have have the media for download for your latest sample?

as for ragdoll, ive never done it before. im probably way off but here are the steps i would think you need to take:

a) set animation speed to 0 to stop the animation at the current frame

b) for each joint, call the getJointNode() method for the model and use those nodes to build your newton ragdoll. these are specific to the model type and are getXJointNode(), getMS3DJointNode(), getB3DJointNode().

c) run the ragdoll simulation

good luck :) also you can search the Irrlicht forum for rag and doll and see if there is anything that can help you out in there.


ninjarat(Posted 2007) [#50]
I'll upload the media when I get some time on my Linux machine, the only (and very awkward) setup I have for uploading things.

Oh, and I'm also having trouble getting a collision tree to run callbacks. Is there some reason that doesn't work?


ninjarat(Posted 2007) [#51]
Uploaded full example (improved from the code given here) with binaries included. Get it at HTTP://www.rattuscanisdev.g0dsoft.com/irrlicht/

TAR and BZIP2 unpacking tools required. I reccomend ArK or 7zip


ninjarat(Posted 2007) [#52]
Currently uploading the same example in a .tar.gz format archive. I do this because I know alot of people probably won't want to dig for a .bz2 compatible archiver (I just found out 7zip won't do .bz2)


Chroma(Posted 2007) [#53]
I see the newton wrapper code but how do I build it into a module?


ninjarat(Posted 2007) [#54]
Here's how to build modules:

1) Save the the code in a file called: [bmax install dir]/mod/pub.mod/newton.mod/newton.bmx

2) Open a console window and navigate to [bmax install dir]/bin/

3) Type: bmk makemods pub.newton

Hope this helps! :)


Chroma(Posted 2007) [#55]
The newton.mod, is that just a regular folder I make and then rename it to that?

EDIT: nm I got it thanks.


ninjarat(Posted 2007) [#56]
How's the physics stuff going, Chroma?


verfum(Posted 2007) [#57]
Is this the only thread on this subject, it's been a while before anyone has posted here, just curious if this continued elsewhere.

Is it possible yet to animate the character and use the collisions, it only works on frame 1.


ninjarat(Posted 2007) [#58]
hmm. I suppose if you tweaked it to run collisions on every frame, for now it uses a still mesh, so maybe just try to run it on every frame?