Download

Scaling Object Without Repositioning it???

Hello,

I am trying to scale my 3D objects, however when I scale them in code, they always move along an axis and changes its original position.

How can I simply resize (scale) my object while keeping it in its original location?
I am trying to create a 3D map that is tiled with different tile sizes. The end result is my tiles are displaced when scaling.
Also, it shouldn’t be my code?.. The unscaled squares are exactly where they should be.

Quick demonstration of the problem:



	FTransform InstanceTransform;

	for (int i = 1; i <= 10; i++) {
		UInstancedStaticMeshComponent* BlockCopy;
		BlockCopy = NewObject<UInstancedStaticMeshComponent>(BlockInstance);
		FVector SpawnLocation(i*100, 1, 0.f);
		InstanceTransform.SetLocation(SpawnLocation);
		BlockCopy->AddInstance(InstanceTransform);
		if (i > 5 && i < 8) {
			BlockCopy->SetWorldScale3D(FVector(2, 2.f, 1.f));
		}
		else if(i>=8) {
			BlockCopy->SetWorldScale3D(FVector(1, 4.f, 1.f));
		}
		BlockCopy->SetStaticMesh(BlockMesh.Object);
	}


I expected this to create a straight solid line along the x-axis, but at the scaling points it breaks up.
scale.png

I hope I explained it clear, thank you very much! :smiley:

Surely someone out there knows how to solve this? :slight_smile:

Here’s the intended result for clarification:
scale2.png

I must be missing some option to keep it from moving.

Likely the origin of the component isn’t centered.

You could try something like:



// Set our Component location to the middle of whatever half our bounds size is.
BlockCopy->SetRelativeLocation(-(BlockCopy->GetBounds() * 0.5f));


Seems to be on the right track, but I get an error: class “UInstancedStaticMeshComponent” has no member “GetBounds”



	UInstancedStaticMeshComponent *BlockInstance;
	BlockInstance = CreateDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("InstancedStaticMeshComponentROOT"));
	static ConstructorHelpers::FObjectFinder<UStaticMesh> BlockMesh(TEXT("StaticMesh'/Game/Geometry/Meshes/1M_Cube.1M_Cube'"));

	FTransform InstanceTransform;

	for (int x = 1; x <= 10; x++) {
		UInstancedStaticMeshComponent* BlockCopy;
		BlockCopy = NewObject<UInstancedStaticMeshComponent>(BlockInstance);
		BlockCopy->SetRelativeLocation(-(BlockCopy->GetBounds() * 0.5f));
		FVector SpawnLocation(x*100, 1, 0.f);
		InstanceTransform.SetLocation(SpawnLocation);
		BlockCopy->AddInstance(InstanceTransform);
		if (x > 5 && x < 8) {
			BlockCopy->SetWorldScale3D(FVector(2, 2.f, 1.f));
		}
		else if(x >= 8) {
			BlockCopy->SetWorldScale3D(FVector(1, 4.f, 1.f));
		}
		BlockCopy->SetStaticMesh(BlockMesh.Object);
	}


EDIT: Seems GetBounds() is for UStaticMesh, but I need Instanced Meshes, otherwise defeats the purpose of performance. There must be some other way to use this on Instanced Meshes? Really stuck. :frowning: