瀏覽代碼

Added TNMesh.

woollybah 9 年之前
父節點
當前提交
ce1005736d
共有 3 個文件被更改,包括 304 次插入0 次删除
  1. 35 0
      dynamics.mod/common.bmx
  2. 247 0
      dynamics.mod/dynamics.bmx
  3. 22 0
      dynamics.mod/glue.cpp

+ 35 - 0
dynamics.mod/common.bmx

@@ -42,6 +42,7 @@ Extern
 	Function bmx_newtondynamics_NewtonCreateBox:Byte Ptr(obj:Object, world:Byte Ptr, dx:Float, dy:Float, dz:Float, shapeID:Int, offsetMatrix:Float Ptr)
 	Function bmx_newtondynamics_NewtonCreateTreeCollision:Byte Ptr(obj:Object, world:Byte Ptr, shapeID:Int)
 	Function bmx_newtondynamics_NewtonCreateCylinder:Byte Ptr(obj:Object, world:Byte Ptr, radius:Float, height:Float, shapeID:Int, offsetMatrix:Float Ptr)
+	Function NewtonMeshCreate:Byte Ptr(world:Byte Ptr)
 
 	Function bmx_newtondynamics_NewtonWorldRayCast(world:Byte Ptr, p0x:Float, p0y:Float, p0z:Float, p1x:Float, p1y:Float, p1z:Float, ..
 		fcb:Float(bodyPtr:Byte Ptr, collPtr:Byte Ptr, hitContact:Float Ptr, hitNormal:Float Ptr, delPtr:Byte Ptr, intersectParam:Float), ..
@@ -88,7 +89,41 @@ Extern
 	Function bmx_newtondynamics_NewtonBodySetForce(body:Byte Ptr, fx:Float, fy:Float, yz:Float, fw:Float)
 	Function bmx_newtondynamics_NewtonBodySetTorque(body:Byte Ptr, tx:Float, ty:Float, tz:Float, tw:Float)
 	Function bmx_newtondynamics_NewtonBodyGetAABB(body:Byte Ptr, p0x:Float Ptr, p0y:Float Ptr, p0z:Float Ptr, p1x:Float Ptr, p1y:Float Ptr, p1z:Float Ptr)
+	Function bmx_newtondynamics_NewtonBodyAddForce(body:Byte Ptr, fx:Float, fy:Float, yz:Float)
+	Function bmx_newtondynamics_NewtonBodyAddTorque(body:Byte Ptr, tx:Float, ty:Float, tz:Float)
+	Function bmx_newtondynamics_NewtonBodyCalculateInverseDynamicsForce(body:Byte Ptr, timestep:Float, vx:Float, vy:Float, vz:Float, fx:Float Ptr, fy:Float Ptr, fz:Float Ptr)
 
 	Function bmx_newtondynamics_matrix_GetEulerAngles(frontX:Float Ptr, pitch0:Float Ptr, yaw0:Float Ptr, roll0:Float Ptr, pitch1:Float Ptr, yaw1:Float Ptr, roll1:Float Ptr)
 
+	Function NewtonMeshDestroy(mesh:Byte Ptr)
+	Function NewtonMeshApplyTransform(mesh:Byte Ptr, matrix:Float Ptr)
+	Function NewtonMeshCalculateOOBB(mesh:Byte Ptr, matrix:Float Ptr, x:Float Ptr, y:Float Ptr, z:Float Ptr)
+	Function NewtonMeshCalculateVertexNormals(mesh:Byte Ptr, angle:Float)
+	Function NewtonMeshApplySphericalMapping(mesh:Byte Ptr, material:Int)
+	Function NewtonMeshApplyCylindricalMapping(mesh:Byte Ptr, cylinderMaterial:Int, capMaterial:Int)
+	Function NewtonMeshApplyBoxMapping(mesh:Byte Ptr, front:Int, side:Int, top:Int)
+	Function NewtonMeshIsOpenMesh:Int(mesh:Byte Ptr)
+	Function NewtonMeshFixTJoints(mesh:Byte Ptr)
+	Function NewtonMeshPolygonize(mesh:Byte Ptr)
+	Function NewtonMeshTriangulate(mesh:Byte Ptr)
+	Function NewtonMeshBeginFace(mesh:Byte Ptr)
+	Function NewtonMeshAddFace(mesh:Byte Ptr, vertexCount:Int, vertex:Float Ptr, strideInBytes:Int, materialIndex:Int)
+	Function NewtonMeshEndFace(mesh:Byte Ptr)
+	Function NewtonMeshGetVertexCount:Int(mesh:Byte Ptr)
+	Function NewtonMeshGetVertexStrideInByte:Int(mesh:Byte Ptr)
+	Function NewtonMeshGetVertexArray:Double Ptr(mesh:Byte Ptr)
+	Function NewtonMeshGetTotalFaceCount:Int(mesh:Byte Ptr)
+	Function NewtonMeshGetTotalIndexCount:Int(mesh:Byte Ptr)
+	Function NewtonMeshGetPointCount:Int(mesh:Byte Ptr)
+	Function NewtonMeshGetPointStrideInByte:Int(mesh:Byte Ptr)
+	Function NewtonMeshGetPointArray:Double Ptr(mesh:Byte Ptr)
+	Function NewtonMeshGetNormalArray:Double Ptr(mesh:Byte Ptr)
+	Function NewtonMeshGetUV0Array:Double Ptr(mesh:Byte Ptr)
+	Function NewtonMeshGetUV1Array:Double Ptr(mesh:Byte Ptr)
+	
 End Extern
+
+
+Const NEWTON_DYNAMIC_BODY:Int = 0
+Const NEWTON_KINEMATIC_BODY	:Int = 1
+Const NEWTON_DEFORMABLE_BODY:Int = 2

+ 247 - 0
dynamics.mod/dynamics.bmx

@@ -160,6 +160,15 @@ Type TNWorld
 		Return cylinder
 	End Method
 	
+	Rem
+	bbdoc: 
+	End Rem
+	Method CreateMesh:TNMesh()
+		Local mesh:TNMesh = New TNMesh
+		mesh.meshPtr = NewtonMeshCreate(worldPtr)
+		Return mesh
+	End Method
+	
 	Rem
 	bbdoc: Sets coulomb model of friction.
 	about: Allows the application to chose between and exact or an adaptive coulomb friction model.
@@ -571,6 +580,54 @@ Type TNBody
 		NewtonBodySetLinearDamping(bodyPtr, linearDamp)
 	End Method
 	
+	Rem
+	bbdoc: Returns the body type.
+	about: Returns one of NEWTON_DYNAMIC_BODY, NEWTON_KINEMATIC_BODY or NEWTON_DEFORMABLE_BODY.
+	End Rem
+	Method GetType:Int()
+		Return NewtonBodyGetType(bodyPtr)
+	End Method
+	
+	Rem
+	bbdoc: Returns true if the body is collidable.
+	End Rem
+	Method GetCollidable:Int()
+		Return NewtonBodyGetCollidable(bodyPtr)
+	End Method
+	
+	Rem
+	bbdoc: Sets the collidable state for the body.
+	End Rem
+	Method SetCollidable(collidableState:Int)
+		NewtonBodySetCollidable(bodyPtr, collidableState)
+	End Method
+	
+	Rem
+	bbdoc: Adds the net force applied to a rigid body.
+	about: This method is only effective when called from OnForceAndTorque/apply force and torque callback.
+	End Rem
+	Method AddForce(fx:Float, fy:Float, fz:Float)
+		bmx_newtondynamics_NewtonBodyAddForce(bodyPtr, fx, fy, fz)
+	End Method
+	
+	Rem
+	bbdoc: Adds the net torque applied to a rigid body.
+	about: This method is only effective when called from OnForceAndTorque/apply force and torque callback.
+	End Rem
+	Method AddTorque(tx:Float, ty:Float, tz:Float)
+		bmx_newtondynamics_NewtonBodyAddTorque(bodyPtr, tx, ty, tz)
+	End Method
+	
+	Rem
+	bbdoc: Calculates the next force that net to be applied to the body to archive the desired velocity in the current time step.
+	about: Can be useful when creating object for game play.
+	This treats the body as a point mass and is uses the solver to calculates the net force that need to be applied to the body
+	such that it reaches the desired velocity in the net time step.
+	End Rem
+	Method CalculateInverseDynamicsForce(timestep:Float, vx:Float, vy:Float, vz:Float, fx:Float Var, fy:Float Var, fz:Float Var)
+		bmx_newtondynamics_NewtonBodyCalculateInverseDynamicsForce(bodyPtr, timestep, vx, vy, vz, Varptr fx, Varptr fy, Varptr fz)
+	End Method
+	
 	' internal
 	Function _forceAndTorqueCallback(bodyPtr:Byte Ptr, timestamp:Float, threadIndex:Int)
 		Local body:TNBody = NewtonBodyGetUserData(bodyPtr)
@@ -604,6 +661,196 @@ Type TNBody
 
 End Type
 
+Rem
+bbdoc: 
+End Rem
+Type TNMesh
+
+	Field meshPtr:Byte Ptr
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method ApplyTransform(matrix:TNMatrix)
+		NewtonMeshApplyTransform(meshPtr, Varptr matrix.frontX)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method CalculateOOBB(matrix:TNMatrix, x:Float Var, y:Float Var, z:Float Var)
+		NewtonMeshCalculateOOBB(meshPtr, Varptr matrix.frontX, Varptr x, Varptr y, Varptr z)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method CalculateVertexNormals(angle:Float)
+		NewtonMeshCalculateVertexNormals(meshPtr, angle * 0.01745329252)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method ApplySphericalMapping(material:Int)
+		NewtonMeshApplySphericalMapping(meshPtr, material)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method ApplyCylindricalMapping(cylinderMaterial:Int, capMaterial:Int)
+		NewtonMeshApplyCylindricalMapping(meshPtr, cylinderMaterial, capMaterial)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method ApplyBoxMapping(front:Int, side:Int, top:Int)
+		NewtonMeshApplyBoxMapping(meshPtr, front, side, top)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method IsOpenMesh:Int()
+		Return NewtonMeshIsOpenMesh(meshPtr)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method FixTJoints()
+		NewtonMeshFixTJoints(meshPtr)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method Polygonize()
+		NewtonMeshPolygonize(meshPtr)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method Triangulate()
+		NewtonMeshTriangulate(meshPtr)
+	End Method
+	
+	
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method BeginFace()
+		NewtonMeshBeginFace(meshPtr)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method AddFace(vertexCount:Int, vertex:Float Ptr, strideInBytes:Int, materialIndex:Int)
+		NewtonMeshAddFace(meshPtr, vertexCount, vertex, strideInBytes, materialIndex)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method EndFace()
+		NewtonMeshEndFace(meshPtr)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method GetTotalFaceCount:Int()
+		Return NewtonMeshGetTotalFaceCount(meshPtr)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method GetTotalIndexCount:Int()
+		Return NewtonMeshGetTotalIndexCount(meshPtr)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method GetPointCount:Int()
+		Return NewtonMeshGetPointCount(meshPtr)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method GetPointStrideInByte:Int()
+		Return NewtonMeshGetPointStrideInByte(meshPtr)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method GetPointArray:Double Ptr()
+		Return NewtonMeshGetPointArray(meshPtr)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method GetNormalArray:Double Ptr()
+		Return NewtonMeshGetNormalArray(meshPtr)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method GetUV0Array:Double Ptr()
+		Return NewtonMeshGetUV0Array(meshPtr)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method GetUV1Array:Double Ptr()
+		Return NewtonMeshGetUV1Array(meshPtr)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method GetVertexCount:Int()
+		Return NewtonMeshGetVertexCount(meshPtr)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method GetVertexStrideInByte:Int()
+		Return NewtonMeshGetVertexStrideInByte(meshPtr)
+	End Method
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method MeshGetVertexArray:Double Ptr()
+		Return NewtonMeshGetVertexArray(meshPtr)
+	End Method
+	
+	
+	Rem
+	bbdoc: 
+	End Rem
+	Method Destroy()
+		If meshPtr Then
+			NewtonMeshDestroy(meshPtr)
+			meshPtr = Null
+		End If
+	End Method
+
+End Type
+
 Rem
 bbdoc: 
 End Rem

+ 22 - 0
dynamics.mod/glue.cpp

@@ -46,6 +46,9 @@ extern "C" {
 	void bmx_newtondynamics_NewtonBodyGetTorqueAcc(const NewtonBody* const, float *, float *, float *);
 	void bmx_newtondynamics_NewtonBodyGetCentreOfMass(const NewtonBody* const, float *, float *, float *);
 	void bmx_newtondynamics_NewtonBodyGetAABB(const NewtonBody* const, float *, float *, float *, float *, float *, float *);
+	void bmx_newtondynamics_NewtonBodyAddForce(const NewtonBody* const, float, float, float);
+	void bmx_newtondynamics_NewtonBodyAddTorque(const NewtonBody* const, float, float, float);
+	void bmx_newtondynamics_NewtonBodyCalculateInverseDynamicsForce(const NewtonBody* const, float, float, float, float, float *, float *, float *);
 
 	void bmx_newtondynamics_config_body(BBObject *, NewtonBody *);
 	void bmx_newtondynamics_body_destroycallback(const NewtonBody* const);
@@ -229,6 +232,25 @@ void bmx_newtondynamics_NewtonBodyGetAABB(const NewtonBody* const body, float *
 	*p1z = v1.m_z;
 }
 
+void bmx_newtondynamics_NewtonBodyAddForce(const NewtonBody* const body, float x, float y, float z) {
+	dVector v(x, y, z, 0.0f);
+	NewtonBodyAddForce(body, &v[0]);
+}
+
+void bmx_newtondynamics_NewtonBodyAddTorque(const NewtonBody* const body, float x, float y, float z) {
+	dVector v(x, y, z, 0.0f);
+	NewtonBodyAddTorque(body, &v[0]);
+}
+
+void bmx_newtondynamics_NewtonBodyCalculateInverseDynamicsForce(const NewtonBody* const body, float timestep, float x, float y, float z, float * fx, float * fy, float * fz) {
+	dVector v(x, y, z, 0.0f);
+	dVector f;
+	NewtonBodyCalculateInverseDynamicsForce(body, timestep, &v[0], &f[0]);
+	*fx = f.m_x;
+	*fy = f.m_y;
+	*fz = f.m_z;
+}
+
 // ********************************************************
 
 NewtonCollision * bmx_newtondynamics_NewtonCreateSphere(BBObject * obj, const NewtonWorld* const world, float radius, int shapeID, float * offsetMatrix) {