Files
UnrealEngineUWP/Engine/Source/Runtime/AnimGraphRuntime/Private/BoneControllers/AnimNode_SpringBone.cpp
Marc Audy 3c0e962858 Copying //UE4/Dev-Frame to //UE4/Main
#lockdown Nick.Penwarden

==========================
MAJOR FEATURES + CHANGES
==========================

Change 2775736 on 2015/11/20 by Richard.Hinckley

	Fix for Paper2D issue with repeated imports in one edutor session. Paper2D import process now creates a new importer at the end. This prevents the sprite sheet import process from leaving frame data around, causing subsequent imports (including imports of different sprite sheets) to include this data inappropriately.

	#codereview michael.noland

Change 2776352 on 2015/11/20 by Zak.Middleton

	#ue4 - Avoid useless DetachFromParent() for the same pending AttachParent during registration. Added missing UpdateOverlaps() when detaching from object simulating physics.

	#rb Marc.Audy, Ori.Cohen
	#codereview James.Golding

Change 2776401 on 2015/11/20 by Mieszko.Zielinski

	Implemented a way to do batched points projection to navmesh, where every point can declare a custom projection box #UE4

	The biggest advantage here is that projection box is independent from projected point - no more manual offsetting of projected point to achieve "100uu up and 500uu down"-like functionality

	#jira UE-23705
	#rb Lukasz.Furman

Change 2777450 on 2015/11/23 by Martin.Wilson

	Bake additive data into animations during cooking to avoid doing additive calculations and extra pose extraction and blending at runtime

	#rb Thomas.Sarkanen

Change 2777698 on 2015/11/23 by Mieszko.Zielinski

	Gameplay debugging tools fixes #UE4

	Fixes:
	- made newly added logs respect Log Visualizer's filters
	- added handling of invalid data when trying to draw EGameplayDebuggerShapeElement::Cylinder in AGameplayDebuggingHUDComponent::DrawPerception. This is a patch, root cause to be found.
	- fixed Log Visualizer resetting it's data while trying to serialize invalid objects. This is a patch, root cause to be addressed.

	In addition
	- while at it removed bunch of 'auto' and 'class' keywords from the files I've touched

	#rb Lukasz.Furman

Change 2777762 on 2015/11/23 by Mieszko.Zielinski

	Removed BlackboardComponent's functionality deprecated since 4.7 #UE4

	#rb Lukasz.Furman

Change 2777839 on 2015/11/23 by Zak.Middleton

	#ue4 - Wrap all vector library calls to math functions through our FMath versions, so they benefit from fixes or improvements therein. Added Exp2() function.

	#rb Laurent.Delayen

Change 2777840 on 2015/11/23 by Zak.Middleton

	#ue4 - Fix up uses of library math functions to go through our FMath namespace.

	#rb Laurent.Delayen

Change 2778287 on 2015/11/23 by Stan.Melax

	deprecation of  FCollisionQueryParams(bool)

	See 2774707 description for the whole story

	#OR-9936

	#codereview marc.audy

	Changes to kite will have to be in a separate check-in

	I couldn't submit to all files from the framework branch   addition fixes have their files are shelved in cl 2778299

Change 2778507 on 2015/11/23 by Marc.Audy

	Eliminate spurious cook warnings for known missing packages
	#rb Michael.Noland

Change 2778546 on 2015/11/23 by Aaron.McLeran

	Moving occlusion feature settings from audio component to sound attenuation settings struct.

	- Sound attenuation setting struct is used for all sounds that do 3d spatialization so it make sense for the occlusion feature settings to be there.
	- Kept old low-pass frequency filter setting values on audio component (where HighFrequencyAttenuation used to be)

	#rb Zak.Middleton

Change 2778664 on 2015/11/23 by Zak.Middleton

	#ue4 - Clarify some comparison functions (IsZero, IsNearlyZero, Equals) in FRotator to explain that they compare as orientations, not other interpretations such as rotational speed, winding, etc.

	#rb Aaron.Mcleran
	#codereview Frank.Gigliotti

Change 2779335 on 2015/11/24 by Mieszko.Zielinski

	Another VisualLog patch to avoid crashing due to a core bug that remains to be investigated #UE4

	Again, the core bug here is related visual log trying to serialize invalid objects on a regular basis.

	#rb Lukasz.Furman

Change 2779338 on 2015/11/24 by Benn.Gallagher

	Fixed crash in Persona when focus is taken from a different window
	#jira UE-22516
	#rb Ben.Cosh

Change 2779375 on 2015/11/24 by Benn.Gallagher

	Fix for deadlock in destructibles. Aquiring actor buffer without releasing causes an infinite wait on next aquire.
	#rb Ori.Cohen

Change 2779753 on 2015/11/24 by Zak.Middleton

	#ue4 - FMath::Atan2() no longer calls atan2f() because of some compiler or library bugs causing it to randomly return NaN for valid input. It now uses a high-precision minimax approximation instead, measured to be 2x faster than the stock C version.

	#rb Brian.Karis

Change 2779853 on 2015/11/24 by Marc.Audy
2015-12-02 16:42:06 -05:00

226 lines
7.1 KiB
C++

// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
#include "AnimGraphRuntimePrivatePCH.h"
#include "BoneControllers/AnimNode_SpringBone.h"
#include "Animation/AnimInstanceProxy.h"
/////////////////////////////////////////////////////
// FAnimNode_SpringBone
FAnimNode_SpringBone::FAnimNode_SpringBone()
: bLimitDisplacement(false)
, MaxDisplacement(0.0f)
, SpringStiffness(50.0f)
, SpringDamping(4.0f)
, ErrorResetThresh(256.0f)
, bNoZSpring_DEPRECATED(false)
, bTranslateX(true)
, bTranslateY(true)
, bTranslateZ(true)
, bRotateX(false)
, bRotateY(false)
, bRotateZ(false)
, RemainingTime(0.f)
, bHadValidStrength(false)
, BoneLocation(FVector::ZeroVector)
, BoneVelocity(FVector::ZeroVector)
{
}
void FAnimNode_SpringBone::Initialize(const FAnimationInitializeContext& Context)
{
FAnimNode_SkeletalControlBase::Initialize(Context);
RemainingTime = 0.0f;
}
void FAnimNode_SpringBone::CacheBones(const FAnimationCacheBonesContext& Context)
{
FAnimNode_SkeletalControlBase::CacheBones(Context);
}
void FAnimNode_SpringBone::UpdateInternal(const FAnimationUpdateContext& Context)
{
FAnimNode_SkeletalControlBase::UpdateInternal(Context);
RemainingTime += Context.GetDeltaTime();
// Fixed step simulation at 120hz
FixedTimeStep = (1.f / 120.f) * TimeDilation;
}
void FAnimNode_SpringBone::GatherDebugData(FNodeDebugData& DebugData)
{
const float ActualAlpha = AlphaScaleBias.ApplyTo(Alpha);
//MDW_TODO Add more output info?
FString DebugLine = DebugData.GetNodeName(this);
DebugLine += FString::Printf(TEXT("(Alpha: %.1f%% RemainingTime: %.3f)"), ActualAlpha*100.f, RemainingTime);
DebugData.AddDebugItem(DebugLine);
ComponentPose.GatherDebugData(DebugData);
}
FORCEINLINE void CopyToVectorByFlags(FVector& DestVec, const FVector& SrcVec, bool bX, bool bY, bool bZ)
{
if (bX)
{
DestVec.X = SrcVec.X;
}
if (bY)
{
DestVec.Y = SrcVec.Y;
}
if (bZ)
{
DestVec.Z = SrcVec.Z;
}
}
void FAnimNode_SpringBone::EvaluateBoneTransforms(USkeletalMeshComponent* SkelComp, FCSPose<FCompactPose>& MeshBases, TArray<FBoneTransform>& OutBoneTransforms)
{
check(OutBoneTransforms.Num() == 0);
const bool bNoOffset = !bTranslateX && !bTranslateY && !bTranslateZ;
if (bNoOffset)
{
return;
}
// Location of our bone in world space
const FBoneContainer& BoneContainer = MeshBases.GetPose().GetBoneContainer();
const FCompactPoseBoneIndex SpringBoneIndex = SpringBone.GetCompactPoseIndex(BoneContainer);
const FTransform& SpaceBase = MeshBases.GetComponentSpaceTransform(SpringBoneIndex);
FTransform BoneTransformInWorldSpace = (SkelComp != NULL) ? SpaceBase * SkelComp->GetComponentToWorld() : SpaceBase;
FVector const TargetPos = BoneTransformInWorldSpace.GetLocation();
AActor* SkelOwner = (SkelComp != NULL) ? SkelComp->GetOwner() : NULL;
if ((SkelComp != NULL) && (SkelComp->AttachParent != NULL) && (SkelOwner == NULL))
{
SkelOwner = SkelComp->AttachParent->GetOwner();
}
// Init values first time
if (RemainingTime == 0.0f)
{
BoneLocation = TargetPos;
BoneVelocity = FVector::ZeroVector;
}
while (RemainingTime > FixedTimeStep)
{
// Update location of our base by how much our base moved this frame.
FVector const BaseTranslation = SkelOwner ? (SkelOwner->GetVelocity() * FixedTimeStep) : FVector::ZeroVector;
BoneLocation += BaseTranslation;
// Reinit values if outside reset threshold
if (((TargetPos - BoneLocation).SizeSquared() > (ErrorResetThresh*ErrorResetThresh)))
{
BoneLocation = TargetPos;
BoneVelocity = FVector::ZeroVector;
}
// Calculate error vector.
FVector const Error = (TargetPos - BoneLocation);
FVector const DampingForce = SpringDamping * BoneVelocity;
FVector const SpringForce = SpringStiffness * Error;
// Calculate force based on error and vel
FVector const Acceleration = SpringForce - DampingForce;
// Integrate velocity
// Make sure damping with variable frame rate actually dampens velocity. Otherwise Spring will go nuts.
float const CutOffDampingValue = 1.f/FixedTimeStep;
if (SpringDamping > CutOffDampingValue)
{
float const SafetyScale = CutOffDampingValue / SpringDamping;
BoneVelocity += SafetyScale * (Acceleration * FixedTimeStep);
}
else
{
BoneVelocity += (Acceleration * FixedTimeStep);
}
// Clamp velocity to something sane (|dX/dt| <= ErrorResetThresh)
float const BoneVelocityMagnitude = BoneVelocity.Size();
if (BoneVelocityMagnitude * FixedTimeStep > ErrorResetThresh)
{
BoneVelocity *= (ErrorResetThresh / (BoneVelocityMagnitude * FixedTimeStep));
}
// Integrate position
FVector const OldBoneLocation = BoneLocation;
FVector const DeltaMove = (BoneVelocity * FixedTimeStep);
BoneLocation += DeltaMove;
// Filter out spring translation based on our filter properties
CopyToVectorByFlags(BoneLocation, TargetPos, !bTranslateX, !bTranslateY, !bTranslateZ);
// If desired, limit error
if (bLimitDisplacement)
{
FVector CurrentDisp = BoneLocation - TargetPos;
// Too far away - project back onto sphere around target.
if (CurrentDisp.SizeSquared() > FMath::Square(MaxDisplacement))
{
FVector DispDir = CurrentDisp.GetSafeNormal();
BoneLocation = TargetPos + (MaxDisplacement * DispDir);
}
}
// Update velocity to reflect post processing done to bone location.
BoneVelocity = (BoneLocation - OldBoneLocation) / FixedTimeStep;
check( !BoneLocation.ContainsNaN() );
check( !BoneVelocity.ContainsNaN() );
RemainingTime -= FixedTimeStep;
}
// Now convert back into component space and output - rotation is unchanged.
FTransform OutBoneTM = SpaceBase;
OutBoneTM.SetLocation( SkelComp->GetComponentToWorld().InverseTransformPosition(BoneLocation) );
const bool bUseRotation = bRotateX || bRotateY || bRotateZ;
if (bUseRotation)
{
FCompactPoseBoneIndex ParentBoneIndex = MeshBases.GetPose().GetParentBoneIndex(SpringBoneIndex);
const FTransform& ParentSpaceBase = MeshBases.GetComponentSpaceTransform(ParentBoneIndex);
FVector ParentToTarget = (TargetPos - ParentSpaceBase.GetLocation()).GetSafeNormal();
FVector ParentToCurrent = (BoneLocation - ParentSpaceBase.GetLocation()).GetSafeNormal();
FQuat AdditionalRotation = FQuat::FindBetweenNormals(ParentToTarget, ParentToCurrent);
// Filter rotation based on our filter properties
FVector EularRot = AdditionalRotation.Euler();
CopyToVectorByFlags(EularRot, FVector::ZeroVector, !bRotateX, !bRotateY, !bRotateZ);
OutBoneTM.SetRotation(FQuat::MakeFromEuler(EularRot) * OutBoneTM.GetRotation());
}
// Output new transform for current bone.
OutBoneTransforms.Add(FBoneTransform(SpringBoneIndex, OutBoneTM));
}
bool FAnimNode_SpringBone::IsValidToEvaluate(const USkeleton* Skeleton, const FBoneContainer& RequiredBones)
{
return (SpringBone.IsValid(RequiredBones));
}
void FAnimNode_SpringBone::InitializeBoneReferences(const FBoneContainer& RequiredBones)
{
SpringBone.Initialize(RequiredBones);
}
void FAnimNode_SpringBone::PreUpdate(const UAnimInstance* InAnimInstance)
{
const USkeletalMeshComponent* SkelComp = InAnimInstance->GetSkelMeshComponent();
const UWorld* World = SkelComp->GetWorld();
check(World->GetWorldSettings());
TimeDilation = World->GetWorldSettings()->GetEffectiveTimeDilation();
}