Cnewton Class:
- Code: Select all
Public Class cNewton
Public Shared newtonworld As intptr
Public Shared newtonmap As intptr
Public Shared lasttick As Integer
public shared deltrans As new newtonapi.NewtonSetTransform(AddressOf NewtonSetTransform)
public shared ttrans As new newtonapi.NewtonApplyForceAndTorque(addressof ApplyForceAndTorqueEvent)
Const newton_gravity As Single = -800.0f
Public Shared Sub InitPhysics
newtonworld = newtonapi.NewtonCreate(nothing, Nothing)
Dim i As Integer = newtonapi.NewtonMaterialGetDefaultGroupID(newtonworld)
newtonapi.NewtonMaterialSetDefaultFriction(newtonworld, i, i, 0.8f, 0.4f)
newtonapi.NewtonMaterialSetDefaultElasticity(newtonworld, i, i, 0.3f)
newtonapi.NewtonMaterialSetDefaultSoftness(newtonworld, i, i, 0.05f)
newtonapi.NewtonMaterialSetCollisionCallback(newtonworld, i, i, Nothing, Nothing, Nothing)
End Sub
Public Shared Sub NewtonSetTransform(byval body as IntPtr, byref matrix as Matrix, byval threadindex as Integer)
'copy the XNA Matrix into an Irrlicht Matrix4
Dim mat As new Matrix4
mat = cNewtonHelp.XNAMatrix2IrrMatrix4(matrix)
'retrieve the user data attached to the newton body
Dim alloc As GCHandle
Dim tmp As scenenode
alloc = gchandle.FromIntPtr(newtonapi.NewtonBodyGetUserData(body))
tmp = alloc.target
alloc.free
If tmp is nothing Then
Exit Sub
Else
'retrieve the user data
tmp.Position = mat.Translation
tmp.rotation = mat.rotationdegrees
End If
End Sub
Public Shared Sub ApplyForceAndTorqueEvent(byval body as intptr, byval timestep as Single, byval threadindex as integer)
Dim mass As Single
Dim Izz As Single
Dim Iyy As Single
Dim Ixx as single
newtonapi.NewtonBodyGetMassMatrix(body, mass, Ixx, Iyy, Izz)
dim force as vector3d
force.x = 0.0f
force.y = newton_gravity * mass
force.z = 0.0f
dim torque as vector3d
torque.x = 0.0f
torque.y = 0.0f
torque.z = 0.0f
newtonapi.NewtonBodyAddForce(body, cNewtonHelp.IrrVec3d2XNAVec3(force))
newtonapi.NewtonBodyAddTorque(body, cNewtonHelp.IrrVec3d2XNAVec3(torque))
End Sub
Public Shared Sub LoadBSP()
newtonmap = newtonapi.NewtonCreateTreeCollision(newtonworld)
newtonapi.NewtonTreeCollisionBeginBuild(newtonmap)
Dim cmeshbuffer As Integer
Dim v1i, v2i, v3i As Integer
Dim mb As MeshBuffer
Dim varray(9) As Single
Dim tmpCount As Integer = 0
Dim m As Mesh = program.mesh.GetMesh(0, 0, 0, 0)
For cmeshbuffer = 0 To m.MeshBufferCount - 1
mb = m.GetMeshBuffer(cmeshbuffer)
Dim mbvertices As list(of Vertex3DT2) = cnewtonhelp.getverticest2(mb)
Dim indices as List(of ushort) = cnewtonhelp.GetIndices(mb)
'add each triangle from the mesh
For q As Integer = 0 To mb.IndexCount - 1 step 3
v1i = indices(q)
v2i = indices(q + 1)
v3i = indices(q + 2)
varray(0) = mbvertices(v1i).Position.X
varray(1) = mbvertices(v1i).Position.Y
varray(2) = mbvertices(v1i).Position.Z
varray(3) = mbvertices(v2i).Position.X
varray(4) = mbvertices(v2i).Position.Y
varray(5) = mbvertices(v2i).Position.Z
varray(6) = mbvertices(v3i).Position.X
varray(7) = mbvertices(v3i).Position.Y
varray(8) = mbvertices(v3i).Position.Z
newtonapi.NewtonTreeCollisionAddFace(newtonmap, 3, varray, 12, 1)
Next
Next
newtonapi.NewtonTreeCollisionEndBuild(newtonmap, False)
dim newtonmapbody as IntPtr = newtonapi.NewtonCreateBody(newtonworld, newtonmap)
' 'set the newton world size based on the BSP size
Dim boxP0 as new vector3(0, 0, 0)
Dim boxP1 As vector3
dim matrix as matrix
newtonapi.NewtonBodyGetMatrix(newtonmapbody, matrix)
newtonapi.NewtonCollisionCalculateAABB(newtonmap, matrix, boxP0, boxP1)
newtonapi.NewtonSetWorldSize(newtonworld, boxP0, boxP1)
End Sub
Public Structure NewtonCube
Public mesh As AnimatedMesh
Public node As AnimatedMeshSceneNode
Public body As intptr
public collision as intptr
End Structure
Public Shared Function MakeCube(ByVal loc As vector3d) As newtoncube
Dim tmp As New NewtonCube
tmp.mesh = program.smgr.GetMesh("maps/smallcube.3ds")
tmp.node = smgr.AddAnimatedMeshSceneNode(tmp.mesh)
tmp.node.setMaterialTexture(0, program.driver.getTexture("textures\warehouse\crate.jpg"))
'create the box primitive
tmp.collision = newtonapi.NewtonCreateBox(newtonworld, 38, 38, 38, nothing)
tmp.body = newtonapi.NewtonCreateBody(newtonworld, tmp.collision)
'Set user data pointer to the scene node
Dim alloc As GCHandle
alloc = gchandle.Alloc(tmp.node)
newtonapi.NewtonBodySetUserData(tmp.body, gchandle.ToIntPtr(alloc))
alloc.free
'set the mass and inertia
newtonapi.NewtonBodySetMassMatrix(tmp.body, 10f, 150f, 150f, 150f)
' 'Set callback functions for the body
newtonapi.NewtonBodySetTransformCallback(tmp.body, deltrans)
newtonapi.NewtonBodySetForceAndTorqueCallback(tmp.body, ttrans)
'Set the position of the body
dim mat as new matrix4
mat.Translation = loc
newtonapi.NewtonBodySetMatrix(tmp.body, cnewtonhelp.IrrMatrix42XnaMatrix(mat))
return tmp
End Function
End Class
cNewtonHelp Class
- Code: Select all
Public Class cNewtonHelp
''' <summary>
''' Get all vertsT2 from a meshbuffer
''' </summary>
''' <param name="mb"></param>
''' <returns></returns>
Public Shared Function GetVerticesT2(byval mb as meshbuffer) As List(Of vertex3dT2)
dim l as new List(of vertex3dT2)
For z As Integer = 0 To mb.VertexCount - 1
l.Add(mb.GetVertexT2(z))
Next
return l
End Function
''' <summary>
''' Get all indices of a meshbuffer
''' </summary>
''' <param name="mb"></param>
''' <returns></returns>
Public Shared Function GetIndices(ByVal mb As MeshBuffer) As List(Of ushort)
Dim l As new List(Of UShort)
For r As Integer = 0 To mb.IndexCount - 1
l.Add(mb.GetIndex(r))
Next
return l
End Function
''' <summary>
''' Converts and XNA Vector3 to and Irrlicht Vector3d
''' </summary>
''' <param name="vec"></param>
''' <returns></returns>
Public Shared Function XnaVec32IrrVec3d(ByVal vec As Vector3) as vector3d
Dim retvec As Vector3D
retvec.X = vec.X
retvec.Y = vec.Y
retvec.Z = vec.Z
return retvec
End Function
''' <summary>
''' Converts Irrlicht Vector3d to XNA Vector3
''' </summary>
''' <param name="vec"></param>
''' <returns></returns>
Public Shared Function IrrVec3d2XNAVec3(ByVal vec As Vector3D) As Vector3
Dim retvec As Vector3
retvec.x = vec.X
retvec.Y = vec.Y
retvec.Z = vec.Z
return retvec
End Function
''' <summary>
''' Converts and XNA Matrix to and Irrlicht Matrix4
''' </summary>
''' <param name="mat"></param>
''' <returns></returns>
Public Shared Function XNAMatrix2IrrMatrix4(ByVal mat As Matrix) As Matrix4
Dim irrmat As new Matrix4
irrmat.SetM(0, 0, mat.M11)
irrmat.SetM(0, 1, mat.M12)
irrmat.SetM(0, 2, mat.M13)
irrmat.SetM(0, 3, mat.M14)
irrmat.SetM(1, 0, mat.M21)
irrmat.SetM(1, 1, mat.M22)
irrmat.SetM(1, 2, mat.M23)
irrmat.SetM(1, 3, mat.M24)
irrmat.SetM(2, 0, mat.M31)
irrmat.SetM(2, 1, mat.M32)
irrmat.SetM(2, 2, mat.M33)
irrmat.SetM(2, 3, mat.M34)
irrmat.SetM(3, 0, mat.M41)
irrmat.SetM(3, 1, mat.M42)
irrmat.SetM(3, 2, mat.M43)
irrmat.SetM(3, 3, mat.M44)
return irrmat
End Function
Public Shared Function IrrMatrix42XnaMatrix(ByVal mat As Matrix4) As Matrix
Dim xnamat As New Matrix
xnamat.m11 = mat.GetM(0, 0)
xnamat.m12 = mat.GetM(0, 1)
xnamat.m13 = mat.GetM(0, 2)
xnamat.m14 = mat.GetM(0, 3)
xnamat.m21 = mat.GetM(1, 0)
xnamat.m22 = mat.GetM(1, 1)
xnamat.m23 = mat.GetM(1, 2)
xnamat.m24 = mat.GetM(1, 3)
xnamat.m31 = mat.GetM(2, 0)
xnamat.m32 = mat.GetM(2, 1)
xnamat.m33 = mat.GetM(2, 2)
xnamat.m34 = mat.GetM(2, 3)
xnamat.m41 = mat.GetM(3, 0)
xnamat.m42 = mat.GetM(3, 1)
xnamat.m43 = mat.GetM(3, 2)
xnamat.m44 = mat.GetM(3, 3)
return xnamat
End Function
End Class
anybody see anything wrong with my classes?
