Download

Weird mass when attaching object to main mesh

I’m just wondering if anyone else has come across this, or has any tips on what I’m doing wrong.

I have a main player mesh on a pawn, which is replicated. I manually attach a new mesh to a socket on each instance of the mesh across the network. The final mass however seems completely off. Here’s what I get:

Calling GetMass() on:

Original Mesh = 1.0
New Equipment = 0.0
Final Mesh = 8.07987183

This is consistent across the server and clients.

Here’s my attachment code




        UEquipmentComponent* NewEquipment = ConstructObject<UEquipmentComponent>(EquipmentToAttach,this);
	if (!NewEquipment)
	{
		GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, FString("Creating equipment component failed!"));
		return;
	}
	GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Green, FString("Mass of Pawn: ") + FString::SanitizeFloat(StaticMeshComponent->GetMass()) + FString(" : ROLE = ") + FString::FromInt(Role));
	GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Green, FString("Mass of new equip: ") + FString::SanitizeFloat(NewEquipment->GetMass()) + FString(" : ROLE = ") + FString::FromInt(Role));
	FName SocketName = FName(*SocketNames(int)AttachmentPoint]);
	NewEquipment->AttachTo(StaticMeshComponent, SocketName,EAttachLocation::KeepRelativeOffset,true);
	NewEquipment->SetVisibility(true,true);
	NewEquipment->PlayerPawn = this;
	NewEquipment->RegisterComponentWithWorld(GetWorld());
	NewEquipment->ReceiverID = UniqueID;
	NewEquipment->SetAllMassScale(0.0);
	if (AttachmentPoint == EAttachmentPoint::attach_top)
	{
		equip_top = NewEquipment;
	}
	if (AttachmentPoint == EAttachmentPoint::attach_right_arm)
	{
		equip_right_arm = NewEquipment;
		equip_right_arm->SetAllMassScale(0.000);
	}
	GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Green, FString("Mass of new Pawn: ") + FString::SanitizeFloat(StaticMeshComponent->GetMass()) + FString(" : ROLE = ") + FString::FromInt(Role));
	


I store a reference to the new equipment (child of UStaticMeshComponent) in a variable equip_right_arm. I can see that the mass of that is equal to the final irregular mass. HOWEVER, if I disable the line “equip_right_arm = NewEquipment;” I get the same result for the final Pawn mass.

Any ideas?

On further testing, I can manually set the mass with “Override Mass” and “Mass in Kg” in the property editor for the blueprint, and when I attach something, the mass seems correct. HOWEVER, before I attach something I get a mass of 1.0, regardless of what I put in those boxes.

I had a look at what values were being used in game with :



FBodyInstance* BodyInst = StaticMeshComponent->GetBodyInstance();
GEngine->AddOnScreenDebugMessage(-1, 300.0f, FColor::Yellow, FString("mass :  ") + FString::SanitizeFloat(BodyInst->MassInKg));
GEngine->AddOnScreenDebugMessage(-1, 300.0f, FColor::Yellow, FString("override mass :  ") + FString::FromInt(BodyInst->bOverrideMass));


And for my mesh before attaching anything, the values are correct. They just don’t seem to be applied to the physics.

I tried this to try and force an update, but nothing has worked:



BodyInst->PutInstanceToSleep();
BodyInst->WakeInstance();
BodyInst->UpdatePhysicalMaterials();
BodyInst->UpdateMassProperties();


Turns out that because my mesh had collision set to OverlapAll the initial mass is ALWAYS 1.0 - regardless of any other mesh settings. If I set it to BlockAll then the initial mass (formerly 1.0) is now calculated correctly. Attaching an object seems to overcome the initial error. Is this a bug?

Just in case anyone else stumbles across this one, it is a bug:

https://answers.unrealengine.com/questions/229098/mass-incorrect-if-physics-overlap-all.html

UE-16076