Hello there, this is my first time posting so sorry if I make some mistake in the way of doing it.
Well, first of all, a bit of context: I’m trying to create a rope swing mechanic in C++. I followed the tutorial made by CodeLikeMe Unreal Rope Swinging System Tutorial - YouTube and I’m trying to adapt it to C++.
I could do it with more or less difficulty, but I managed to do the same: A cable component with an object attached to its end (in this case a ball) with a Physic constraint between the ball and the root component. Everything was working fine, but when rescaling the ball, it happens that the ball falls to the ground and start making some strange movements. I tried the same with blueprints and still happens after rescaling. If done with the object’s original size, it behaves as expected.
I don’t know If maybe I’m missing something in the configuration of physics or something, but is strange that it doesn’t work. The only difference between the tutorial and what I’m doing, besides the fact that I’m trying it on C++, is that I’m making it on Unreal Engine 5.
this is the rope.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Rope.generated.h"
UCLASS()
class SAVIORADVENTURES_API ARope : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ARope();
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cable")
class UCableComponent* Cable;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cable")
USceneComponent* OurRootComponent;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cable")
UStaticMeshComponent* CableEnd;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cable")
class UPhysicsConstraintComponent* PhysicsConstraint;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cable")
class UCapsuleComponent* EndCollider;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
and this is the rope.cpp
// Sets default values
ARope::ARope()
{
// 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;
OurRootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("OurRootComponent"));
SetRootComponent(OurRootComponent);
Cable = CreateDefaultSubobject<UCableComponent>(TEXT("Cable"));
Cable->SetupAttachment(GetRootComponent());
Cable->bAttachEnd = false;
Cable->CableLength = 400.f;
CableEnd = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CableEnd"));
CableEnd->SetupAttachment(GetRootComponent());
ConstructorHelpers::FObjectFinder<UStaticMesh> CableEndAsset(TEXT("StaticMesh '/Engine/BasicShapes/Sphere.Sphere'")); //Para encontrar la esfera
CableEnd->SetStaticMesh(CableEndAsset.Object);
CableEnd->SetSimulatePhysics(true);
Cable->AttachEndTo.ComponentProperty = FName("CableEnd");
Cable->bAttachEnd = true;
Cable->EndLocation = FVector(0.f);
PhysicsConstraint = CreateDefaultSubobject<UPhysicsConstraintComponent>(TEXT("RopePhysicsConstraint"));
PhysicsConstraint->SetupAttachment(GetRootComponent());
FConstrainComponentPropName name1;
FConstrainComponentPropName name2;
name1.ComponentName = FName("OurRootComponent");
name2.ComponentName = FName("CableEnd");
PhysicsConstraint->ComponentName1 = name1;
PhysicsConstraint->ComponentName2 = name2;
EndCollider = CreateDefaultSubobject<UCapsuleComponent>(TEXT("EndCollider"));
EndCollider->SetupAttachment(CableEnd);
CableEnd->SetRelativeScale3D(FVector(0.1f, 0.1f, 0.1f));
}
// Called when the game starts or when spawned
void ARope::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ARope::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
If someone has a clue, it would be really helpful.
Again, thank you in advance and sorry if I made something wrong while asking.
UPDATE: It seems to be a problem with UE5, because replicating this project on UE4 doesn’t give the problem. It seems to be necessary to increment on
Project Settings → Engine Physics → Chaos Physics → Solver Options → Iterations. Putting it up to 200 seems to fix the problem, but I’m not quite satisfied with this solution. If someone has an idea, is welcome.