FT - PostStep Update as a Delegate

+ Can be used to finely control actuators between mj_steps
This commit is contained in:
Jb win 2025-04-30 13:47:42 +07:00 committed by Jean-Baptiste Briant (JB)
parent 568d67d04f
commit d311284050
2 changed files with 30 additions and 0 deletions

View File

@ -109,6 +109,12 @@ template <typename ComponentType> void AMujocoVolumeActor::AssignComponentsToArr
} }
} }
template <typename UserClass>
void AMujocoVolumeActor::BindPostPhysicDelegate(UserClass* Object, void(UserClass::* Func)(float))
{
PostPhysicUpdateDelegate.BindUObject(Object, Func);
}
void AMujocoVolumeActor::InitializeMujoco() void AMujocoVolumeActor::InitializeMujoco()
{ {
if (!Options) if (!Options)
@ -391,11 +397,17 @@ void AMujocoVolumeActor::Tick(float DeltaTime)
{ {
if (MujocoData) if (MujocoData)
{ {
// TODO rename FrameSkip -> SimStepsPerGameFrame - Min 1 - Default 16
// TODO timestep default .001 (1ms)
// TODO mj_step only inside the for loop
mj_step(MujocoModel.Get(), MujocoData.Get()); mj_step(MujocoModel.Get(), MujocoData.Get());
PostPhysicUpdateDelegate.ExecuteIfBound(MujocoData->time);
for (int32 Frame = 0; Frame < FrameSkip; ++Frame) for (int32 Frame = 0; Frame < FrameSkip; ++Frame)
{ {
mj_step(MujocoModel.Get(), MujocoData.Get()); mj_step(MujocoModel.Get(), MujocoData.Get());
PostPhysicUpdateDelegate.ExecuteIfBound(MujocoData->time);
} }
for (int32 i = 1; i < BodyComponents.Num(); ++i) for (int32 i = 1; i < BodyComponents.Num(); ++i)

View File

@ -21,6 +21,10 @@ DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnMujocoCompileBegin);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnMujocoCompileError, FString, Error); DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnMujocoCompileError, FString, Error);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnMujocoCompileSuccess); DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnMujocoCompileSuccess);
// To be called after mj_step
DECLARE_DELEGATE_OneParam(FPostPhysicUpdate, float);
UCLASS(Blueprintable, BlueprintType) UCLASS(Blueprintable, BlueprintType)
class LUCKYMUJOCO_API AMujocoVolumeActor : public AActor class LUCKYMUJOCO_API AMujocoVolumeActor : public AActor
{ {
@ -76,6 +80,20 @@ public:
UPROPERTY(BlueprintAssignable, Category = "Mujoco | Events") UPROPERTY(BlueprintAssignable, Category = "Mujoco | Events")
FOnMujocoCompileSuccess OnMujocoCompileSuccess; FOnMujocoCompileSuccess OnMujocoCompileSuccess;
// ---------------------------
// ------- POST UPDATE -------
// ---------------------------
private:
FPostPhysicUpdate PostPhysicUpdateDelegate;
public:
/**
* @description Register a delegate to be executed after mj_step
* @param Object
*/
template<typename UserClass>
void BindPostPhysicDelegate(UserClass* Object, void (UserClass::*Func)(float));
UFUNCTION(BlueprintCallable, Category = "Mujoco") UFUNCTION(BlueprintCallable, Category = "Mujoco")
void SetActuatorValue(const FString& ActuatorName, double Value); void SetActuatorValue(const FString& ActuatorName, double Value);