Physics examples in c++?

Hi all. I’m having some issues getting into coding physics in c++. I am simply trying to add force on the Z axis of my actor when clicking left mouse button. The problem is, I can’t find ANY examples of how to actually use AddForce, or how to get the component I need to call AddForce on… The API reference only shows what parameter types to use, no straight forward examples. After 4 years coding in Unity c#, I feel so new because I can’t wrap my head around this stuff or find what I’m looking for in the documentation :confused: If someone could point me in the right direction, it’d be greatly appreciated.

#include "PhysicsTest1.h"
#include "PhysicsTest.h"


APhysicsTest::APhysicsTest(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	PrimaryActorTick.bCanEverTick = true;
	Clicked = false;
}

void APhysicsTest::Tick(float DeltaSeconds)
{
	Super::Tick(DeltaSeconds);

	if ((GetKeyState(VK_LBUTTON) & 0x80) != 0)
	{
		if (!Clicked)
		{
			Clicked = true;
			GEngine->AddOnScreenDebugMessage(-1, 3, FColor::Red, "Clicked.");
			
			//Need to add force here!
		}
	}

	else
	{
		Clicked = false;
	}
}

AddForce is part of UPrimitiveComponent which ids parent of all graphical components like UStaticMeshComponent.
You define components from class constructor. In header file you mace var for component:

TSubobjectPtr<UStaticMeshComponent> MeshComp;

In Class constructor:

//Grab mesh from content menager, you can get path by right click asset and "Copy Reference"
static ConstructorHelpers::FObjectFinder<UStaticMesh> Mesh(TEXT("StaticMesh'/Path/To/Mesh'"));
//Create Mesh component
MeshComp = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("Mesh"));
//Set you mesh to mesh component, you can set other paramenters too
MeshComp->StaticMesh = Mesh.Object;
//Set mesh component as a root component... or else you got other root component 
//then you need to use AddComponent(), see example below
RootComponent = MeshComp;

And there you go, you can call AddForce from RootComponent :slight_smile:

You might be also interested in MovmentComponent which got motion physics. In header you add variable for new component

TSubobjectPtr<UMovmentComponent> Movement;

and in constructor of class you construct the component in to the actor

//Create the component
Movement = PCIP.CreateDefaultSubobject<UMovementComponent>(this, TEXT("Movement"));
//Set it's parameters
Movement->Something = ?;
//Add component to actor
AddComponent(FName("Movement"), false, FTransform(), NULL);

When I try to use Mesh.Object, Mesh is undeclared.

Did you grab the mesh asset? This what should declere it

static ConstructorHelpers::FObjectFinder<UStaticMesh> Mesh(TEXT("StaticMesh'/Path/To/Mesh'"));

Yeah, here’s what I’m trying to use in the constructor.

static ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Game/Meshes/MyMesh.MyMesh'"));
	MeshComp = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("Mesh"));
	MeshComp->StaticMesh = Mesh.Object;
	RootComponent = MeshComp;

You calling FObjectFinder constructor without declaring variable Mesh, is should be like this:

static ConstructorHelpers::FObjectFinder<UStaticMesh> Mesh(TEXT("StaticMesh'/Game/Meshes/MyMesh.MyMesh'"));

Ahh how did I miss that lol. Well that got me where I need to be! Thanks so much for your time and help :smiley:

No problem :slight_smile: For future, you can attach any component with this, here you got list of components that engine has(ones that start with U):

And you can FObjectFinder to grab any asset, you just need to set right class for specific type of assets (for example for Blueprint, you need to use UBlueprint), but keep in mind it works only in constructor, the “Mesh” is name of variable, you can change it as you like ;]

Nice! Bookmarked, thanks again man. This will let me get past this issue and keep going. I hate getting stuck but I love that people are here to help!

I would love to do this in blueprint but like you were am mega lost currently :frowning:

placing components in blueprint is a lot easier