首页 > 代码库 > 物理部分的一些设置

物理部分的一些设置

一、Physics Bodies:

老生常谈,AActor只是一个容器,所以是不可能有物理模拟部分的(AStaticMeshActor实例在世界大纲中可以设置,这个梗要看代码)。在API文档中搜索SetSimulatePhysics,也证明了猜测,在virtual void UPrimitiveComponent::SetSimulatePhysics(bool bSimulate) 首次出现;并分别被USkeletalMeshComponent和UDestructibleComponent组件override。所以物理模拟是针对组件的,更具体一步,是针对图元(primitive)组件及子类的。明白这一点,继续。

二、在世界大纲中,Physics Bodies是什么?

呃,其实就是这个了:

技术分享

三、如何才能有这个东东?

勾选物理模拟就自动有了(Nvidia PhysX SDK决定的)。偷下懒,引用“Unreal engine physics essentials”原话:We can also create Physics Bodies by creating Physics Assets and Skeletal Meshes, which automatically have the properties of physics by default. Lastly, Shape Components in blueprints, such as spheres, boxes, and capsules will automatically gain the properties of a Physics Body if they are set for any sort of collision, overlap, or other physics simulation events. As always, remember to ensure that our asset has a collision applied to it before attempting to simulate physics or establish Physics Bodies, otherwise the simulation will not work.

四、代码中如何访问?

AMyActor::AMyActor()
{
     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don‘t need it.
    PrimaryActorTick.bCanEverTick = true;

    SC_RootSphere = CreateDefaultSubobject<USphereComponent>(TEXT("RootSphere"));
    SC_RootSphere->InitSphereRadius(50.0f);
    SetRootComponent(SC_RootSphere);

    SM_MyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("模拟网格"));
    SM_MyMesh->SetupAttachment(SC_RootSphere);

    ConstructorHelpers::FObjectFinder<UStaticMesh> MeshAsset(TEXT("StaticMesh‘/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere‘"));
    if (MeshAsset.Succeeded())
    {
        SM_MyMesh->SetStaticMesh(MeshAsset.Object);
    }

    SM_MyMesh->SetSimulatePhysics(true);
    SM_MyMesh->BodyInstance.SetMassOverride(1000.0f);
}

五、引用UE官档一句原话(https://docs.unrealengine.com/latest/INT/GettingStarted/FromUnity/index.html):In UE4, The collision component and rigidbody component are one.  The base class for this is UPrimitiveComponent, which has many subclasses (USphereComponent, UCapsuleComponent, etc.) to suit your needs.

所以,按unity的观点来讲,UPrimitiveComponet自动有了Rigidbody,只是如果勾选模拟物理,就是Unity中的Rigidbody,如果不勾选,就相当于unity中勾选了“Is Kinematic”选项,这时该组件的行为表现出运动学的特征,唔,运动学请参考前一篇。在官档中的例子也很有趣,摘录如下:

Kinematic Rigidbodies

Unity C#:

 

public class MyComponent : MonoBehaviour
{
    void Start()
    {
        rigidbody.isKinimatic = true;
        rigidbody.velocity = transform.forward * 10.0f;
    }
}

 UE4 C++:

UCLASS()
class AMyActor : public AActor
{
    GENERATED_BODY()

    UPROPERTY()
    UPrimitiveComponent* PhysicalComp;

    AMyActor()
    {
        PhysicalComp = CreateDefaultSubobject<USphereComponent>(TEXT("CollisionAndPhysics"));
        PhysicalComp->SetSimulatePhysics(false);
        PhysicalComp->SetPhysicsLinearVelocity(GetActorRotation().Vector() * 100.0f);
    }
};

 

物理部分的一些设置