西昌.何雨锋
[mass 质量] [inertia 惯性] [rigid 刚性] [torque 扭距] [Impulse 推动力] [Simulate 模拟] [Angular 有角的]
[Gravity 重力] [joint 连接] [Interaction 交互] [damp阻尼] [Velocity 速度] [Ragdoll 洋娃娃] [Continious 连续的]
[Buoyancy 浮力] [cone 锥形] [Vehicle 小车] [Ellipsoid 椭圆体] [hinge 绞链] [slider 滑冰]
1、65第一次引入了newton引擎。
2、要使用这一引擎,必须先初始化:
#include "../../tv65dll/CTVPhysics.h"
CTVPhysics* phy1;
phy1=new CTVPhysics();
phy1->Initialize();
3、你可以先给物理世界一个地面。
phy1->CreateStaticTerrainBody(land1); //land1就是已经做好的land
4、可以给一点重力:
cTV_3DVECTOR zhong_pos1;
//而实际上点也可以直接以常量的方式列出:cTV_3DVECTOR(0.0001f, 0.0001f, 0.0001f)
赋指针可以这样: cTV_3DVECTOR* _damp =new cTV_3DVECTOR(0.0001f, 0.0001f, 0.0001f);
赋对象可以这样: cTV_3DVECTOR _damp =cTV_3DVECTOR(0.0001f, 0.0001f, 0.0001f);
zhong_pos1.x=0;
zhong_pos1.y=-0.2; //负数代表向下
zhong_pos1.z=0;
phy1->SetGlobalGravity(&zhong_pos1);
5、在物理世界中的核心是:body.
body代表这个物体只受到重力和强制力的影响,
但是其他的body以及地面对它无任何影响,因为它连形状都没有。
你必须先建立一个body:
int box_id=phy1->CreateBody(0); //这里的box_id代表了这个body在物理世界中唯一的序列号,以后对这个body的所有操作都将使用此序列号
//当然了,这个body得有重量,否则连重力也影响不到它。
phy1->SetBodyMass(box_id,0.1); //这里的重量是0.1kg,你还可以用其他函数来设置重心和权重
phy1->AddBox(box_id,&zhong_pos1,&tui_pos1); //将body增加一个box形状,其大小为zhong_pos1到tui_pos1的大小。
//也可以增加成一个大小为50的球体。
phy1->AddSphere(box_id,&zhong_pos1,50);
//还可以直接以mesh的形状来作为body的形状,但是也只有三种情况:box,球与圆拄,如果为none就是mesh的本身形状。
phy1->AddMesh(box_id,&box1,cTV_BODY_BOX ,false);
第二个参数常量列表如下:
cTV_BODY_BOX = 0,
cTV_BODY_SPHERE = 1,
cTV_BODY_CYLINDER = 2,
cTV_BODY_NONE = 3,
cTV_BODY_CONVEXHULL = 4
6、void SetBodyInertia(int rigidBodyIndex,float iX, float iY, float iZ);
//设置body惯性
void SetBodyPosition(int rigidBodyIndex,float fX, float fY, float fZ);
//设置body的位置,当然了,设置完以后,body将继续遵从物理世界的原则。
SetBodyRotation();
//设置body的旋转
void Unload();
7、不管怎么样,这个物理世界是不会自动运动的,除非你在循环中让其随时间的流逝而自动更新:
phy1->Simulate(engine1->TickCount());
8、有人可能会说,物理世界里的东西怎么运动,可并不会影响到(TV)渲染世界里的东西啊.是的,所以你得在循环中用下列
方法将物理世界里的盒子与渲染世界里的盒子联系起来,就OK了。
zhong_pos1=phy1->GetBodyPosition(box_id); //得到物理世界中box_id的位置
tui_pos1=phy1->GetBodyRotation(box_id); //得到物理世界中box_id的旋转
box1->SetPosition(zhong_pos1.x,zhong_pos1.y,zhong_pos1.z); //将渲染世界里的东西也这样做
box1->SetRotation(tui_pos1.x,tui_pos1.y,tui_pos1.z);
//好了,你看到盒子在陆地上翻滚了吗?
9、如果你想对一个body施加向某个方向的力,那么就可以用
void AddImpulse(int rigidBodyIndex, cTV_3DVECTOR* vImpulse, bool bLocalSpace = false);
//对body产生一个力,这个力的方向和大小放在第二个参数中,最后一个如果为true,那么就会有摩擦阻力,一般为false
这样这个物体就会被推动,最后一个参数如果选true,那么它在受到推力的情况下将不再和其他任何物体,包括地面发生碰撞了。
就象在真空中被推一样。
AddTorque(int rigidBodyIndex, cTV_3DVECTOR* vTorque, bool bLocalSpace = false);
//是对body进行扭矩力。
9、下面是对Actor的物理模拟。
int CreateAutoRagdoll(CTVActor* aActor);
//理论上是建立一个布娃娃,但是好象无法使用。
int CreateActorBody(float fMass, CTVActor* pActor, cCONST_TV_PHYSICSBODY_BOUNDING eBoundingType);
//建立出一个actor专用的body,但是在后面的参数中,还是只有那几项,所以出来的body只能还是box等。
void AddActorEx(int rigidBodyIndex, CTVActor* pActor, cCONST_TV_PHYSICSBODY_BOUNDING eBoundingType, float fTranslationOffsetX, float fTranslationOffsetY, float fTranslationOffsetZ, float fRotationOffsetX, float fRotationOffsetY, float fRotationOffsetZ);
void AddActor(int rigidBodyIndex, CTVActor* pActor, cCONST_TV_PHYSICSBODY_BOUNDING eBoundingType, bool bUseActorTransformationAsOffset);
void AddActorBone(int rigidBodyIndex, CTVActor* pActor,int iActorBone);
void SyncBodiesWithBones(CTVActor* pRagdollActor);
//同步骨骼对actor
10、小车的模拟:
[Associated 关联的] [wheel 轮子] [Chassis 底盘、滑动的] [steer 驾驶] [Brake 刹车] [Traction牵引] [Acceleration 加速度]
int CreateVehicle(int iChassisBody);
int AddVehicleWheel(int iVehicleIndex, float fMass, float fWidth, float fRadius, cTV_3DVECTOR* vRotationAxis, float fLocalPositionX, float fLocalPositionY, float fLocalPositionZ, CTVMesh* pAssociatedMesh = NULL);
int AddVehicleWheelEx(int iVehicleIndex, float fMass, float fWidth, float fRadius, cTV_3DVECTOR* vRotationAxis, float fLocalPositionX, float fLocalPositionY, float fLocalPositionZ, float fLocalRotationX = 0.0f, float fLocalRotationY = 0.0f, float fLocalRotationZ = 0.0f, float fSuspensionLength = 0.2f, float fSuspensionShock = 1.0f, float fSuspensionSpring = 1.0f, CTVMesh* pAssociatedMesh = NULL);
void DestroyVehicle(int iVehicleIndex);
void SetVehicleWheelSteering(int iVehicleIndex, int iWheelIndex, float fSteering, float fDamping = 0.0f);
void SetVehicleWheelTorque(int iVehicleIndex, int iWheelIndex, float fTorque, float fDamping = 0.0f);
void DestroyVehicleWheel(int iVehicleIndex, int iWheelIndex);
void SimulateFixedStep(float fDeltaTime, float fFixedStepTime);
float GetVehicleWheelLoad(int iVehicleIndex, int iWheelIndex);
float GetVehicleWheelAngularVelocity(int iVehicleIndex, int iWheelIndex);
void SetVehicleWheelParameters(int iVehicleIndex, int iWheelIndex, float fMaxSideSlip = 0.0f, float fSideSlipCoeff = 0.0f, float fMaxLongitudinalSlide = 0.0f, float fLongitudinalSlideCoeff = 0.0f);
void GetVehicleWheelSpeed(int iVehicle, int iWheelIndex, float* retLongitudinalSpeed, float* retLatitudeSpeed);
void VehicleWheelHandBrake(int iVehicle, int iWheelIndex, float fCoefficient = 1.0f, float fMaxBrakeTorque = 500.0f);
void VehicleWheelBrake(int iVehicle, int iWheelIndex, float fBrakeAcceleration, float fMaxBrakeTorque = 500.0f);
bool IsVehicleWheelColliding(int iVehicle, int iWheelIndex);
bool IsVehicleWheelLosingTraction(int iVehicle, int iWheelIndex);
bool IsVehicleWheelLosingSideGrip(int iVehicle, int iWheelIndex);
float VehicleWheelGetMaxBrakeAcceleration(int iVehicle, int iWheelIndex);
void VehicleReset(int iVehicle);
float GetVehicleWheelSteering(int iVehicle, int iWheel);
void SetVehicleWheelMaterialGroup(int iMaterialGroup, int iVehicle, int iWheel = -1);
6、其他函数:
void AddForce(int rigidBodyIndex, cTV_3DVECTOR* vForce, bool bLocalSpace = false);
//对body产生一个力并且body将不再对支撑物有效,这个力的方向和大小放在第二个参数中,最后一个如果为true,那么就会有摩擦阻力,一般为false
//用了之后,物体不但会滚动,而且会落下至地平线以下
//void AddTorque(int rigidBodyIndex, cTV_3DVECTOR* vTorque, cTV_3DVECTOR* vWorldPoint);
void AddImpulseToPoint(int rigidBodyIndex, cTV_3DVECTOR* vImpulse, cTV_3DVECTOR* vPoint, bool bLocalSpace = false);
void AddTorque(int rigidBodyIndex, cTV_3DVECTOR* vTorque, bool bLocalSpace = false);
void Simulate(float dt);
//模拟开始,参数为时间
void SetBodyMovable(int rigidBodyIndex, bool bMovable);
//设置body是否可动,默认是可动
void SetBodyAngularVelocity(int rigidBodyIndex,cTV_3DVECTOR* vAngularVelocity, bool bLocalSpace = false);
//?设置物体角顶点
void SetBodyLinearVelocity(int rigidBodyIndex,cTV_3DVECTOR* vLinearVelocity, bool bLocalSpace = false);
//?设置物体线顶点
int CreateStaticMeshBody(CTVMesh* pMesh, bool bGroupsAsFaceAttribute = false, bool bOptimize = true);
//建立一个静止的mesh的body
int GetActiveBodyCount();
//得到活动的body总数
int GetBodyCount();
//得到body总数
int GetMemoryUsed();
//得到内存用了多少
// ball joint
int CreateBallJoint(int iAttachedBody, int iParentBody, cTV_3DVECTOR* vAttachPointInWorldSpace);
void SetBallJointLimits(int iBallJoint, bool bLimitsEnable, cTV_3DVECTOR* vSocketAxis, float fMaxConeAngle, float fMaxTwistAngle );
// hinge joint
int CreateHingeJoint(int iAttachedBody, int iParentBody, cTV_3DVECTOR* vAttachPointInWorldSpace, cTV_3DVECTOR* vHingeAxis);
void SetHingeJointLimits(int iHingeJoint, bool bLimitsEnable, float fMaxAngle, float fMinAngle);
// slider joint
int CreateSliderJoint(int iSlidingBody, int iSupportBody, cTV_3DVECTOR* vAttachPointInWorldSpace, cTV_3DVECTOR* vSlidingAxis);
void SetSliderJointLimits(int iSliderJoint, bool bLimitsEnable, float fMaxPosition, float fMinPosition);
// up joint
int CreateUpJoint(int iBody, cTV_3DVECTOR* vDirectionInWorldSpace);
void SetUpJointDirection(int iUpJoint, cTV_3DVECTOR* vDirectionInWorldSpace);
cTV_3DVECTOR GetUpJointDirection(int iUpJoint);
void SetJointBreakage(int iJoint, bool bEnable, float fMaxForceOnTheJoint, bool bAutoRemoveJoint = true);
void DestroyJoint(int iJoint);
void DestroyBody(int iBody, bool bDisableAttachedMeshes = true);
//消毁body
int CreateMaterialGroup(char* sOptionalName = NULL);
int GetDefaultMaterialGroup();
void SetMaterialInteractionSoftness(int iMaterialGroup1 , int iMaterialGroup2 , float fSoftness);
void SetMaterialInteractionFriction(int iMaterialGroup1 , int iMaterialGroup2 , float fStaticFriction, float fKineticFriction);
void SetMaterialInteractionBounciness(int iMaterialGroup1, int iMaterialGroup2, float fBounciness);
void SetMaterialInteractionCollision(int iMaterialGroup1, int iMaterialGroup2, bool bCollisionEnable);
void SetMaterialInteractionEvents(int iMaterialGroup1, int iMaterialGroup2, bool bRaiseCollisionEvents= true , bool bOneEventPerBodyPairs = true, bool bDisableCollision = false);
void SetBodyMaterialGroup(int iBody, int iMaterialGroup);
int GetBodyMaterialGroup(int iBody);
void SetDamping(int iBody, float fLinearDamping, cTV_3DVECTOR* vAngularDamping );
void SetTorque(int iBody, cTV_3DVECTOR* vTorque, bool bLocalSpace = false);
void SetForce(int iBody, cTV_3DVECTOR* vForce, bool bLocalSpace = false);
cTV_3DVECTOR GetTorque(int iBody,bool bLocalSpace=false);
cTV_3DVECTOR GetForce(int iBody,bool bLocalSpace=false);
cTV_3DMATRIX GetBodyMatrix(int iBody);
cTV_3DVECTOR GetBodyAngularVelocity(int iBody,bool bLocalSpace=false);
cTV_3DVECTOR GetBodyLinearVelocity(int iBody,bool bLocalSpace=false );
int CreateStaticTerrainBody(CTVLandscape* lLand);
void SetBodyMatrix(int iBody, cTV_3DMATRIX* mMatrix);
void SetAutoFreeze(int iBody, bool bAutoFreezeEnable = true, float fLinearThreshold = 0.001f, float fAngularThreshold = 0.001f, int iNumStep = 20);
bool IsBodyFrozen(int iBody);
bool IsJointBroken(int iJoint);
cTV_3DVECTOR GetJointForce(int iJoint);
int GetMaterialGroupByName(char* sMaterialGroupName);
void SetBodyRotation(int iBody, float fRotationAngleX, float fRotationAngleY, float fRotationAngleZ);
int CreateMeshBody(float fMass, CTVMesh* pMesh, cCONST_TV_PHYSICSBODY_BOUNDING eBoundingType = cTV_BODY_BOX, bool bRecenterMesh = true);
// special addmesh EX with offset.
void AddMeshEx(int rigidBodyIndex, CTVMesh* pMesh, cCONST_TV_PHYSICSBODY_BOUNDING eBoundingType, float fTranslationOffsetX, float fTranslationOffsetY, float fTranslationOffsetZ, float fRotationOffsetX, float fRotationOffsetY, float fRotationOffsetZ);
void AddBoxEx(int rigidBodyIndex, cTV_3DVECTOR* vBoxSize, float fTranslationOffsetX, float fTranslationOffsetY, float fTranslationOffsetZ, float fRotationOffsetX, float fRotationOffsetY, float fRotationOffsetZ);
void AddSphereEx(int rigidBodyIndex, float fSphereRadius, float fTranslationOffsetX, float fTranslationOffsetY, float fTranslationOffsetZ, float fRotationOffsetX, float fRotationOffsetY, float fRotationOffsetZ);
cTV_3DVECTOR GetBodyPosition(int rigidBodyIndex);
void SetBodyBuoyancy(int rigidBodyIndex, bool bEnable, float fLiquidDensity, float fLinearFriction, float fAngularFriction, cTV_PLANE* pLiquidPlane);
void SetHingeJointMotor(int iHingeJoint, bool bMotorEnable, bool bUseSpeedLimit, float fMaxAngularSpeed = 1.0f, float fMaxAcceleration = 1.0f, float fDamping = 0.1f );
void AddCylinder(int rigidBodyIndex, float fRadius, float fHeight);
void AddCylinderEx(int rigidBodyIndex, float fRadius, float fHeight, float fTranslationOffsetX, float fTranslationOffsetY, float fTranslationOffsetZ, float fRotationOffsetX, float fRotationOffsetY, float fRotationOffsetZ);
void AddCone(int rigidBodyIndex, float fRadius, float fHeight);
void AddConeEx(int rigidBodyIndex, float fRadius, float fHeight, float fTranslationOffsetX, float fTranslationOffsetY, float fTranslationOffsetZ, float fRotationOffsetX, float fRotationOffsetY, float fRotationOffsetZ);
void AddConvexHull(int rigidBodyIndex, CTVMesh* pSourceMesh);
void AddConvexHullEx(int rigidBodyIndex, CTVMesh* pSourceMesh, float fTranslationOffsetX, float fTranslationOffsetY, float fTranslationOffsetZ, float fRotationOffsetX, float fRotationOffsetY, float fRotationOffsetZ);
void RenderDebugInfo(bool bWireFrame);
void EnableDebugInfo(int iBodyIndex, bool bEnable = true, int iColor = -1);
void SetBodyTag(int iBody, char* sTag);
char* GetBodyTag(int iBody);
int PollEvents();
int GetEventType(int iEventID);
cTV_EVENT_PHYSICSCOLLISION GetEventCollisionDesc(int iEventID);
float GetHingeJointAngle(int iHingeJoint);
float GetSliderJointPosition(int iSliderJoint);
float GetHingeJointRotationSpeed(int iHingeJoint);
void SetHingeTorque(int iHingeJoint, float fTorque);
cTV_3DVECTOR GetBodyInertia(int iBody);
void SetBodyContiniousCollision(int iBody, bool bEnableContiniousCollision = true);
float GetBodyTotalVolume(int iBody);
void SetBodyCoriolisForces(int iBody, bool bEnableCoriolisForce = false );
void SetBodyMass(int iBody, float fMass);
void Explosion(cTV_3DVECTOR* vPosition, float fRadius, float fStrength, bool bCheckCollisions = false, float fMassLimit = -1.0f);
void SetSolverModel(cCONST_TV_PHYSICS_SOLVER eSolverModel);
void SetFrictionModel(cCONST_TV_PHYSICS_FRICTION eFrictionModel);
void SetJointStiffness(int iJoint, float fStiffness);
void SetBodyQuaternion(int iBody, cTV_3DQUATERNION* quat);
cTV_3DQUATERNION GetBodyQuaternion(int iBody);
void AddConvexHullFromPoints(int rigidBodyIndex, cTV_3DVECTOR* vFirstElementOfArray, int iNumPoints, float fTranslationOffsetX, float fTranslationOffsetY, float fTranslationOffsetZ, float fRotationOffsetX, float fRotationOffsetY, float fRotationOffsetZ);
void AddEllipsoidEx(int rigidBodyIndex, cTV_3DVECTOR* vEllipsoidRadius, float fTranslationOffsetX, float fTranslationOffsetY, float fTranslationOffsetZ, float fRotationOffsetX, float fRotationOffsetY, float fRotationOffsetZ);
void AddEllipsoid(int rigidBodyIndex, cTV_3DVECTOR* vEllipsoidCenter, cTV_3DVECTOR* vEllipsoidRadius);
void FreezeBody(int iBody);
void UnfreezeBody(int iBody);
void SetJointCollision(int iJoint, bool bCollision);
void SetMaterialInteractionContinuousCollision(int iMaterialGroup1, int iMaterialGroup2, bool bEnable);
void AddCapsule(int rigidBodyIndex, cTV_3DVECTOR* vCapsuleCenter, float fRadius, float fHeight);
void AddCapsuleEx(int rigidBodyIndex, float fRadius, float fHeight, float fTranslationOffsetX, float fTranslationOffsetY, float fTranslationOffsetZ, float fRotationOffsetX, float fRotationOffsetY, float fRotationOffsetZ);
float GetBodyMass(int iBody);
void SetBodyCenterOfMass(int iBody, cTV_3DVECTOR* vCenterOfMass);
cTV_3DVECTOR GetBodyCenterOfMass(int iBody);
void SetWorldSize(cTV_3DVECTOR* vWorldBoxMin, cTV_3DVECTOR* vWorldBoxMax);
int CreateUniversalJoint(int iAttachedBody, int iParentBody, cTV_3DVECTOR* vAttachPointInWorldSpace, cTV_3DVECTOR* vUnitAxis1, cTV_3DVECTOR* vUnitAxis2);
void SetUniversalJointLimits(int iUniversalJoint, bool bLimitEnable, float fMaxAngle1, float fMinAngle1, float fMaxAngle2, float fMinAngle2);
cTV_3DVECTOR GetBodyRotation(int iBody);
void SetBodyCollidable(int iBody, bool bCollidable);
bool AdvancedCollision(cTV_3DVECTOR* vStartRay, cTV_3DVECTOR* vEndRay, cTV_COLLISIONRESULT* retResult);
void EnableCPUOptimizations(bool bEnable);
void ComputeAccurateInertia(int iBodyID);
void DestroySimulation(int iSimulationID);
int GetActiveSimulation();
void SetActiveSimulation(int iSimulationID);
int CreateNewSimulation();
bool TestCollision(int iBody1, int iBody2, bool bFillEvents);
};