Need help with TriggerMass please!

Hi guys! So i changed a piece of code in DoorOpen.cpp from >=, to == so that I could get the doors to only open when specific objects are placed on them:

code:
if (GetTotalMassOfActorsOnPlate() == TriggerMass)
{
OnOpenEvent.Broadcast();

}
else
{
OnCloseEvent.Broadcast();
}

However… the doors are not opening at all, despite objects I place on the pressure plates being exactly equal to the triggermass set on any one particular door. i.e. on door one, triggermass is 50. I set my defaultpawn to 50kg in the blueprint window… door doesn’t open. Likewise with two chairs (each set to 25 mass each).

Does anyone know why this is happening? Any help would be greatly appreciated! Thanks :slight_smile:

Hello, Jabbawalden!

I tried your code and got exactly the same problem.

Here is why its not working:
If you log out the total mass, you’ll see that for some unknown for me reason it is not exactly the same as you specify in the editor. For instanse, Im settind an actor mass to be 30 and my TriggerMass is set to 30, but GetTotalMassOfActorsOnPLate returns the value of 29.999998.

LogTemp: Warning: TotalMass is 29.999998

Setting the mass on a runtime and logging the GetTotalMassOfActorsOnPLate otput:
com-optimize%20(1)

29.999998 != 30 , thus the door stays closed.

Also, this value drift doesnt seem to be consistent, 25 kilos set in the editor, remains 25 in code.

You can make your code work by rounding the total mass ( return round(TotalMass); )
It works, but Im not sure if that it is the right way to solve this problem.

Would be cool to know whats going on with that mass value and why does it deviate from what was specified in editor.

1 Like

Hey mate, thanks so much for your reply and explanation!

A very strange problem indeed.

I will try rounding the TotalMass instead!

It’s a “normal” behaviour of floating point numbers.
Because of its inner format, floating point isn’t able to precisely store every rational number. This is also true for items’ position in space, and algorithms that search for “touch” events must take this into account.

So, when comparing floating point numbers, you must always apply some tolerance. Directly confronting them (with ==) is destined to fail “at random”.

EDIT: beware that rounding (to the nearest integer, I suppose) implicitly means you accept a tolerance of 1. If you have a TotalMass of 29.50001, it would be rounded to 30 so you’re accepting a triggering mass of ~0.5 kg less than intended. This can or cannot be acceptable.

A more precise way could be:

if (abs(TotalMass - TriggeringMass) <= 0.01f) { /* trigger activated */ }

This accepts a TotalMass differing from the TriggeringMass no more than 1/100th of a Kg.

1 Like

Privacy & Terms