Lovense Integration
ULovenseToyAnimationControlComponent Class Reference

This component calculates Lovense command speeds based on the average bone movement of N bones on N skeletal mesh components and broadcasts commands to all connected toys.
The commands that are sent are ELovenseCommand::Pattern for Lovense Remote and ELovenseCommand::Vibrate, ELovenseCommand::RotateClockwise, ELovenseCommand::RotateAntiClockwise, ELovenseCommand::AirAuto for Lovense Connect.
For ELovenseCommand::Pattern, a pattern will be generated every 4 seconds based on animation data that gets passed in via getAnimationDataDelegate. This mode is called "Pattern Generation".
For the Lovense Connect commands, we average the physics linear velocities of all bones in real time. This mode is called "Realtime Evaluation". More...

#include <LovenseIntegration/Source/LovenseIntegration/Public/Components/LovenseToyAnimationControlComponent.h>

Inheritance diagram for ULovenseToyAnimationControlComponent:

Public Member Functions

virtual void Initialize (const TArray< USkeletalMeshComponent * > &inSkeletalMeshComponents, const TArray< FName > &inBoneNames, const FLovenseGetAnimationData &inGetAnimationDataDelegate)
 Initializes the component with required data. Can be called multiple times. More...
 
virtual void StartBehaviour ()
 Resets running values and calls SetEnabled(true).
Should be called when first enabling this component and after each animation transition (See StartTransition()).
 
virtual void StartTransition ()
 Sets max toy speed to maxToySpeedTargetDuringTransition and flips rotation direction.
Should be called before blending to a new animation, though it's not necessary.
This will prevent the toy from freaking out due to high bone velocities during an animation blend.
The Rotation direction change only works for Lovense Connect, as rotation direction can't be controlled with Lovense Remote.
Call StartBehaviour() once transition has finished to reset.
 
virtual void SetEnabled (bool value)
 Enable/Disable the component. Component will not run and no commands will be sent while the component is disabled. More...
 
FORCEINLINE bool IsEnabled ()
 
virtual void OnGamePaused ()
 Disables the component when the game has been paused to stop the toys. More...
 
virtual void OnGameUnpaused ()
 Enables the component when the game has been unpaused. More...
 
virtual void TickComponent (float DeltaTime, ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override
 
virtual void EndPlay (const EEndPlayReason::Type EndPlayReason) override
 
FORCEINLINE float GetMaxRecordedVelocityDecaySpeed ()
 Get the speed (per second) at which maxRecordedVelocity will decay at.
Only relevant for realtime evaluation.
 
FORCEINLINE void SetMaxRecordedVelocityDecaySpeed (float newDecaySpeed)
 Set the speed (per second) at which maxRecordedVelocity will decay at.
Only relevant for realtime evaluation.
 
FORCEINLINE float GetMaxRecordedVelocityDecayMinimum ()
 Naming is a bit confusing, this is the minimum value that maxRecordedVelocity can decay to.
Only relevant for realtime evaluation.
 
FORCEINLINE void SetMaxRecordedVelocityDecayMinimum (float newDecayMinimum)
 Naming is a bit confusing, this is the minimum value that maxRecordedVelocity can decay to.
Only relevant for realtime evaluation.
 
FORCEINLINE float GetMaxToySpeedInterpolationSpeed ()
 Get interpolation speed at which currentMaxToySpeed will be interpolated towards maxToySpeedTarget.
Only relevant for realtime evaluation.
 
FORCEINLINE void SetMaxToySpeedInterpolationSpeed (float newInterpolationSpeed)
 Set interpolation speed at which currentMaxToySpeed will be interpolated towards maxToySpeedTarget.
Only relevant for realtime evaluation.
 
FORCEINLINE float GetMaxToySpeedTargetDuringTransition ()
 Get maximum toy command speed that maxToySpeedTarget will be set to during transitions (See StartTransition()).
Value is between 1-20.
Only relevant for realtime evaluation.
 
FORCEINLINE void SetMaxToySpeedTargetDuringTransition (float newSpeedTargetDuringTransition)
 Set maximum toy command speed that maxToySpeedTarget will be set to during transitions (See StartTransition()).
Value should be between 1-20.
Only relevant for realtime evaluation.
 
FORCEINLINE FVector2D GetPatternToySpeedInputRange ()
 Get input value range for the average velocity to toy command speed conversion.
E.g. if X = 5cm/s and Y = 15cm/s, toy command speed will be 1 if average velocity is <=5cm/s and 20 if average velocity is >= 15cm/s.
Values are >=0 and X<=Y.
Only relevant for pattern generation.
 
FORCEINLINE void SetPatternToySpeedInputRange (FVector2D newInputRange)
 Set input value range for the average velocity to toy command speed conversion.
E.g. if X = 5cm/s and Y = 15cm/s, toy command speed will be 1 if average velocity is <=5cm/s and 20 if average velocity is >= 15cm/s.
Values should be >=0 and X<=Y.
Only relevant for pattern generation.
 
const FORCEINLINE TArray< USkeletalMeshComponent * > & GetSkeletalMeshComponents ()
 Get the skeletal meshes that will be evaluated. All of these components have to use the same skeleton or have all bones defined in boneNames.
Gets passed in via Initialize().
 
const FORCEINLINE TArray< FName > & GetBoneNames ()
 Get the names of the bones of the skeletal meshes that will be evaluated.
This component calculates an average velocity across these bones of all skeletal meshes, which is then converted to a toy command speed.
Gets passed in via Initialize().
 
FORCEINLINE float GeMaxRecordedVelocity ()
 Get the current maximum velocity that has been calculated. Used to remap the current velocity to a toy command speed value.
This will slowly decay if the current velocity is smaller. Decay speed is defined by maxRecordedVelocityDecaySpeed.
Only relevant for realtime evaluation.
 
FORCEINLINE float GeCurrentMaxToySpeed ()
 Get the current maximum toy command speed.
Is interpolated towards maxToySpeedTarget with maxToySpeedInterpolationSpeed.
Only relevant for realtime evaluation.
 
FORCEINLINE float GeMaxToySpeedTarget ()
 Get the target value that currentMaxToySpeed gets interpolated to.
Is usually 20, but will be dropped to maxToySpeedTargetDuringTransition during transitions (see StartTransition()).
Only relevant for realtime evaluation.
 
FORCEINLINE bool GeRotateClockwise ()
 Whether the toys should rotate clockwise or anticlockwise.
Gets toggled in StartTransition().
Only relevant for realtime evaluation.
 

Protected Member Functions

virtual void OnPatternGenerationTimerFinished ()
 Called every 4 seconds when the generate pattern timer loops.
Generates a pattern based on animation data passed in via getAnimationDataDelegate and sends a pattern command with the generated pattern.
 
virtual void RealtimeEvaluationUpdate (float deltaTime)
 Called every tick.
Calculates current toy command speed based on the average physics linear velocity of all boneNames of all skeletalMeshComponents and sends Lovense Connect commands.
 
virtual bool GeneratePattern (const TArray< class UAnimSequence * > &animations, const TArray< float > &times, FLovensePattern &outPattern)
 Called by OnPatternGenerationTimerFinished().
Generates a pattern based on animation data passed in via getAnimationDataDelegate. More...
 

Protected Attributes

double maxAverageVelocity
 Maximum velocity the average velocity of all bones on all skeletal mesh components will be capped to.
Should be as small as possible to prevent situations where it takes a long time for maxRecordedVelocity to decay.
Only relevant for realtime evaluation.
 
float maxRecordedVelocityDecaySpeed
 The speed (per second) at which maxRecordedVelocity will decay at.
Only relevant for realtime evaluation.
 
float maxRecordedVelocityDecayMinimum
 Naming is a bit confusing, this is the minimum value that maxRecordedVelocity can decay to.
Only relevant for realtime evaluation.
 
float maxToySpeedInterpolationSpeed
 Interpolation speed at which currentMaxToySpeed will be interpolated towards maxToySpeedTarget.
Only relevant for realtime evaluation.
 
float maxToySpeedTargetDuringTransition
 Maximum toy command speed that maxToySpeedTarget will be set to during transitions (See StartTransition()).
Value should be between 1-20.
Only relevant for realtime evaluation.
 
FVector2D patternToySpeedInputRange
 Input value range for the average velocity to toy command speed conversion.
E.g. if X = 5cm/s and Y = 15cm/s, toy command speed will be 1 if average velocity is <=5cm/s and 20 if average velocity is >= 15cm/s.
Values should be >=0 and X<=Y.
Only relevant for pattern generation.
 
TArray< TObjectPtr< USkeletalMeshComponent > > skeletalMeshComponents
 The skeletal meshes that will be evaluated. All of these components have to use the same skeleton or have all bones defined in boneNames.
Gets passed in via Initialize().
 
TArray< FName > boneNames
 The names of the bones of the skeletal meshes that will be evaluated.
This component calculates an average velocity across these bones of all skeletal meshes, which is then converted to a toy command speed.
Gets passed in via Initialize().
 
double maxRecordedVelocity
 The current maximum velocity that has been calculated. Used to remap the current velocity to a toy command speed value.
This will slowly decay if the current velocity is smaller. Decay speed is defined by maxRecordedVelocityDecaySpeed.
Only relevant for realtime evaluation.
 
float currentMaxToySpeed
 The current maximum toy command speed.
Is interpolated towards maxToySpeedTarget with maxToySpeedInterpolationSpeed.
Only relevant for realtime evaluation.
 
float maxToySpeedTarget
 The target value that currentMaxToySpeed gets interpolated to.
Is usually 20, but will be dropped to maxToySpeedTargetDuringTransition during transitions (see StartTransition()).
Only relevant for realtime evaluation.
 
bool bRotateClockwise
 Whether the toys should rotate clockwise or anticlockwise.
Gets toggled in StartTransition().
Only relevant for realtime evaluation.
 
bool bIsEnabled
 Whether the component is currently enabled. Component will not run and no commands will be sent while the component is disabled.
 
FTimerHandle patternGenerationTimerHandle
 Handle for the pattern generation timer.
Timer is looping with an interval of 4 seconds.
Will generate and send a pattern command on each iteration.
Only relevant for pattern generation.
 
FLovenseGetAnimationData getAnimationDataDelegate
 Executed every 4 seconds in OnPatternGenerationTimerFinished() when pattern generation timer loops to retrieve animation data from external code that uses this component.
Gets passed in via Initialize().
Only relevant for pattern generation.
 

Detailed Description

This component calculates Lovense command speeds based on the average bone movement of N bones on N skeletal mesh components and broadcasts commands to all connected toys.
The commands that are sent are ELovenseCommand::Pattern for Lovense Remote and ELovenseCommand::Vibrate, ELovenseCommand::RotateClockwise, ELovenseCommand::RotateAntiClockwise, ELovenseCommand::AirAuto for Lovense Connect.
For ELovenseCommand::Pattern, a pattern will be generated every 4 seconds based on animation data that gets passed in via getAnimationDataDelegate. This mode is called "Pattern Generation".
For the Lovense Connect commands, we average the physics linear velocities of all bones in real time. This mode is called "Realtime Evaluation".

Note
You will need to manually call OnGamePaused() / OnGameUnpaused() on this component to stop toys during game pause as there are no global events for that in unreal.
I recommend creating a child class from this to handle that.
This diagram depicts which methods should be called on the ULovenseToyAnimationControlComponent and when.
+-----------+ +---------------------------------------+
+-----------+ +---------------------------------------+
| |
| Initialize() |
|----------------------------------------->|
| |
|----------------------------------------->|
| |
| |
| |
| Animation Loop |
|--------------- |
| | |
|<-------------- |
| |
| |
| |
|----------------------------------------->|
| |
| Animation Transition |
|--------------------- |
| | |
|<-------------------- |
| |
|----------------------------------------->|
| |
| |
------------------\ | |
| Game was paused |-| |
|-----------------| | OnGamePaused() |
|----------------------------------------->|
--------------------\ | |
| Game was unpaused |-| |
|-------------------| | OnGameUnpaused() |
|----------------------------------------->|
| |
| |
-----------\ | |
| Finished |-| |
|----------| | SetEnabled(false) |
|----------------------------------------->|
| |

Member Function Documentation

◆ GeneratePattern()

bool ULovenseToyAnimationControlComponent::GeneratePattern ( const TArray< class UAnimSequence * > &  animations,
const TArray< float > &  times,
FLovensePattern outPattern 
)
protectedvirtual

Called by OnPatternGenerationTimerFinished().
Generates a pattern based on animation data passed in via getAnimationDataDelegate.

Returns
Only returns false if input data is invalid.

◆ Initialize()

void ULovenseToyAnimationControlComponent::Initialize ( const TArray< USkeletalMeshComponent * > &  inSkeletalMeshComponents,
const TArray< FName > &  inBoneNames,
const FLovenseGetAnimationData &  inGetAnimationDataDelegate 
)
virtual

Initializes the component with required data. Can be called multiple times.

Note
Will also disable the component, so you will need to manually call StartBehaviour() (for first initialization) or SetEnabled(true).
Parameters
inSkeletalMeshComponentsThe skeletal meshes that will be evaluated. All of these components have to use the same skeleton or have all bones defined in boneNames.
inBoneNamesThe names of the bones of the skeletal meshes that will be evaluated.
This component calculates an average velocity across these bones of all skeletal meshes, which is then converted to a toy command speed.
inGetAnimationDataDelegateEvent delegate called once every 4 seconds to retrieve animation data. See FLovenseCalculateAnimationTime for more information.

◆ IsEnabled()

FORCEINLINE bool ULovenseToyAnimationControlComponent::IsEnabled ( )
inline
Returns
Whether the component is currently enabled. Component will not run and no commands will be sent while the component is disabled.

◆ OnGamePaused()

void ULovenseToyAnimationControlComponent::OnGamePaused ( )
virtual

Disables the component when the game has been paused to stop the toys.

Note
You will need to manually call this as there are no global events for that in unreal.

◆ OnGameUnpaused()

void ULovenseToyAnimationControlComponent::OnGameUnpaused ( )
virtual

Enables the component when the game has been unpaused.

Note
You will need to manually call this as there are no global events for that in unreal.

◆ SetEnabled()

void ULovenseToyAnimationControlComponent::SetEnabled ( bool  value)
virtual

Enable/Disable the component. Component will not run and no commands will be sent while the component is disabled.

Parameters
valueIf false, realtime evaluation will be stopped, pattern generation timer will be stopped and all commands cleared.
If true, realtime evaluation will be (re)started, will immediately generate and send a pattern command and (re)start the generate pattern timer.

The documentation for this class was generated from the following files:
ULovenseToyAnimationControlComponent
This component calculates Lovense command speeds based on the average bone movement of N bones on N s...
Definition: LovenseToyAnimationControlComponent.h:162
ULovenseToyAnimationControlComponent::StartTransition
virtual void StartTransition()
Sets max toy speed to maxToySpeedTargetDuringTransition and flips rotation direction....
Definition: LovenseToyAnimationControlComponent.cpp:88
ULovenseToyAnimationControlComponent::Initialize
virtual void Initialize(const TArray< USkeletalMeshComponent * > &inSkeletalMeshComponents, const TArray< FName > &inBoneNames, const FLovenseGetAnimationData &inGetAnimationDataDelegate)
Initializes the component with required data. Can be called multiple times.
Definition: LovenseToyAnimationControlComponent.cpp:68
ULovenseToyAnimationControlComponent::SetEnabled
virtual void SetEnabled(bool value)
Enable/Disable the component. Component will not run and no commands will be sent while the component...
Definition: LovenseToyAnimationControlComponent.cpp:94
ULovenseToyAnimationControlComponent::OnGamePaused
virtual void OnGamePaused()
Disables the component when the game has been paused to stop the toys.
Definition: LovenseToyAnimationControlComponent.cpp:112
ULovenseToyAnimationControlComponent::OnGameUnpaused
virtual void OnGameUnpaused()
Enables the component when the game has been unpaused.
Definition: LovenseToyAnimationControlComponent.cpp:118
ULovenseToyAnimationControlComponent::StartBehaviour
virtual void StartBehaviour()
Resets running values and calls SetEnabled(true). Should be called when first enabling this compone...
Definition: LovenseToyAnimationControlComponent.cpp:80