Page's Personal Website

Physx两轮载具简单实现

2021-06-13

[TOC]

想实现一个摩托车的物理载具,所以先在Snippet里面改了一下

PxVehicleNoDrive

看到UE4中是用PxVehicleNoDrive实现的,就想在SnippetVehicleNoDrive快速改一下看下效果,结果改了几行代码就基本看到效果了

1.修改轮子个数

VehicleDesc initVehicleDesc()
{
    // 省略...
    // two wheels
    const PxU32 nbWheels = 2;

2.修改报错

主要是默认是四个轮子的,然后默认代码传多了,删掉即可

void startBrakeMode()
{
    gVehicleNoDrive->setBrakeTorque(0, 1000.0f);
    gVehicleNoDrive->setBrakeTorque(1, 1000.0f);
    //gVehicleNoDrive->setBrakeTorque(2, 1000.0f);
    //gVehicleNoDrive->setBrakeTorque(3, 1000.0f);
}
void startHandbrakeTurnLeftMode()
{
    //gVehicleNoDrive->setBrakeTorque(2, 1000.0f);
    //gVehicleNoDrive->setBrakeTorque(3, 1000.0f);
    gVehicleNoDrive->setDriveTorque(0, 1000.0f);
    gVehicleNoDrive->setDriveTorque(1, 1000.0f);
    gVehicleNoDrive->setSteerAngle(0, 1.0f); 
    gVehicleNoDrive->setSteerAngle(1, 1.0f); 
}
void startHandbrakeTurnRightMode()
{
    //gVehicleNoDrive->setBrakeTorque(2, 1000.0f);
    //gVehicleNoDrive->setBrakeTorque(3, 1000.0f);
    gVehicleNoDrive->setDriveTorque(0, 1000.0f);
    gVehicleNoDrive->setDriveTorque(1, 1000.0f);
    gVehicleNoDrive->setSteerAngle(0, -1.0f); 
    gVehicleNoDrive->setSteerAngle(1, -1.0f); 
}
void releaseAllControls()
{
    gVehicleNoDrive->setDriveTorque(0, 0.0f);
    gVehicleNoDrive->setDriveTorque(1, 0.0f);
    //gVehicleNoDrive->setDriveTorque(2, 0.0f);
    //gVehicleNoDrive->setDriveTorque(3, 0.0f);
    gVehicleNoDrive->setBrakeTorque(0, 0.0f);
    gVehicleNoDrive->setBrakeTorque(1, 0.0f);
    //gVehicleNoDrive->setBrakeTorque(2, 0.0f);
    //gVehicleNoDrive->setBrakeTorque(3, 0.0f);
    gVehicleNoDrive->setSteerAngle(0, 0.0f);
    gVehicleNoDrive->setSteerAngle(1, 0.0f);
    //gVehicleNoDrive->setSteerAngle(2, 0.0f);
    //gVehicleNoDrive->setSteerAngle(3, 0.0f);
}

3.控制台提示参数不对

..\..\PhysXVehicle\src\PxVehicleUpdate.cpp (244) : invalid parameter : PxVehicleComputeSprungMasses: sprungMassCoordinates must all be valid coordinates

断点看一下就知道问题在哪里了,主要是计算便宜的时候坐标是无穷大,修改下下面的代码即可

void computeWheelCenterActorOffsets
(const PxF32 wheelFrontZ, const PxF32 wheelRearZ, const PxVec3& chassisDims, const PxF32 wheelWidth, const PxF32 wheelRadius, const PxU32 numWheels, PxVec3* wheelCentreOffsets)
{
            // 两个轮子的时候deltaZ算的时候除以0了
        //const PxF32 deltaZ = (wheelFrontZ - wheelRearZ) / (numLeftWheels - 1.0f);
        PX_UNUSED(numWheels);
        PX_UNUSED(wheelWidth);
        //front wheel offset from origin.
        wheelCentreOffsets[0] = PxVec3(0.f, -(chassisDims.y / 2 + wheelRadius), wheelRearZ);
        //rear wheel offsets from origin.
        wheelCentreOffsets[1] = PxVec3(0.f, -(chassisDims.y / 2 + wheelRadius), wheelFrontZ);
}

PVD效果

因为渲染窗口没有重播功能,不好找角度,就在PVD里面录制了 two effect


Similar Posts

下一篇 删除父类指针

Comments