一、Physics Bodies:
老生常談,AActor只是一個(gè)容器,所以是不可能有物理模擬部分的(AStaticMeshActor實(shí)例在世界大綱中可以設(shè)置,這個(gè)梗要看代碼)。在API文檔中搜索SetSimulatePhysics,也證明了猜測,在virtual void UPrimitiveComponent::SetSimulatePhysics(bool bSimulate) 首次出現(xiàn);并分別被USkeletalMeshComponent和UDestructibleComponent組件override。所以物理模擬是針對組件的,更具體一步,是針對圖元(primitive)組件及子類的。明白這一點(diǎn),繼續(xù)。
二、在世界大綱中,Physics Bodies是什么?
呃,其實(shí)就是這個(gè)了:

三、如何才能有這個(gè)東東?
勾選物理模擬就自動有了(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("模擬網(wǎng)格")); 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的觀點(diǎn)來講,UPrimitiveComponet自動有了Rigidbody,只是如果勾選模擬物理,就是Unity中的Rigidbody,如果不勾選,就相當(dāng)于unity中勾選了“Is Kinematic”選項(xiàng),這時(shí)該組件的行為表現(xiàn)出運(yùn)動學(xué)的特征,唔,運(yùn)動學(xué)請參考前一篇。在官檔中的例子也很有趣,摘錄如下:
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);
}
};
浙公網(wǎng)安備 33010602011771號