Irrlicht and Newton

BlitzMax Forums/BlitzMax Programming/Irrlicht and Newton

Hey, here's mine! It's strongly based on yours but it has an add-on. :D Kinda proud!

'Fixed by gman.  That guy's showing up in the credits for sure.  Thank you G!
Function AnimatedMeshToNewtonCollisionTree:Byte Ptr(world:Byte Ptr,map:IAnimatedMesh,newtonMap:Byte Ptr,createbox=True)
	NewtonTreeCollisionBeginBuild(newtonmap)
	Local cMeshBuffer,j
	Local mb:IMeshBuffer
	
	Local vArray#[9]	'vertex array (3*3 floats)
	For cMeshBuffer=0 To map.getMesh(0).getMeshBufferCount() - 1
		mb = map.getMesh(0).getMeshBuffer(cMeshBuffer)
		Local vertType=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 Then
				v1=Vertex(arr_standardVerts.elementAt(v1i))
				v2=Vertex(arr_standardVerts.elementAt(v2i))
				v3=Vertex(arr_standardVerts.elementAt(v3i))
			Else If vertType=EVT_2TCOORDS Then
				v1=Vertex(arr_2TCoordVerts.elementAt(v1i))
				v2=Vertex(arr_2TCoordVerts.elementAt(v2i))
				v3=Vertex(arr_2TCoordVerts.elementAt(v3i))
			Else If vertType=EVT_TANGENTS Then
				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(newtonmap, 3,Varptr vArray[0], 12, j)
		Next
	Next
	NewtonTreeCollisionEndBuild(newtonmap,0)
	Local newtonmapbody:Byte Ptr=NewtonCreateBody(world,newtonmap)
	
	If createbox Then
		'set the newton world size based on the mesh size
		Local boxP0#[3] 
		Local boxP1#[3] 
		NewtonBodyGetMatrix newtonmapbody,Varptr mat[0]
		NewtonCollisionCalculateAABB newtonmap,Varptr mat[0],Varptr boxP0[0],Varptr boxP1[0]
		'pad the box
		boxP0[0]:-60
		boxP1[0]:+60
		boxP0[1]:-60
		boxP1[1]:+60
		boxP0[2]:-60
		boxP1[2]:+60
		NewtonSetWorldSize world,Varptr boxP0[0],Varptr boxP1[0]
	End If
	
	Return newtonmapbody
End Function
Function MeshToNewtonCollisionTree:Byte Ptr(world:Byte Ptr,map:IMesh,newtonMap:Byte Ptr,createbox=True)
	NewtonTreeCollisionBeginBuild(newtonmap)
	Local cMeshBuffer,j
	Local mb:IMeshBuffer
	
	Local vArray#[9]	'vertex array (3*3 floats)
	For cMeshBuffer=0 To map.getMeshBufferCount() - 1
		mb = map.getMeshBuffer(cMeshBuffer)
		Local vertType=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 Then
				v1=Vertex(arr_standardVerts.elementAt(v1i))
				v2=Vertex(arr_standardVerts.elementAt(v2i))
				v3=Vertex(arr_standardVerts.elementAt(v3i))
			Else If vertType=EVT_2TCOORDS Then
				v1=Vertex(arr_2TCoordVerts.elementAt(v1i))
				v2=Vertex(arr_2TCoordVerts.elementAt(v2i))
				v3=Vertex(arr_2TCoordVerts.elementAt(v3i))
			Else If vertType=EVT_TANGENTS Then
				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(newtonmap, 3,Varptr vArray[0], 12, j)
		Next
	Next
	NewtonTreeCollisionEndBuild(newtonmap,0)
	Local newtonmapbody:Byte Ptr=NewtonCreateBody(world,newtonmap)
	
	If createbox Then
		'set the newton world size based on the mesh size
		Local boxP0#[3] 
		Local boxP1#[3] 
		NewtonBodyGetMatrix newtonmapbody,Varptr mat[0]
		NewtonCollisionCalculateAABB newtonmap,Varptr mat[0],Varptr boxP0[0],Varptr boxP1[0]
		'pad the box
		boxP0[0]:-60
		boxP1[0]:+60
		boxP0[1]:-60
		boxP1[1]:+60
		boxP0[2]:-60
		boxP1[2]:+60
		NewtonSetWorldSize world,Varptr boxP0[0],Varptr boxP1[0]
	End If
	
	Return newtonmapbody
End Function


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! :)