VR-Engage  2.2
Loading...
Searching...
No Matches
vreRtdTickCallback.h
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4*******************************************************************************/
5
6//! \file vreRtdTickCallback.h
7//! \ingroup vreVrfRtd
8//! \brief Tick callback handler for VREngage RTD components
9//!
10//! This file defines the DtVreRtdTickCallback class, which manages the periodic
11//! updating of RTD simulation states during each simulation frame in the VREngage
12//! environment. It handles time synchronization between RTD and VRF systems.
13
14#pragma once
15
18
19#include <vrfcgf/cgf.h>
20#include <vrfutil/profiler.h>
21
22#include <vrfobjcore/localObject.h>
23
24
25namespace vreRtd
26{
27//! \brief Tick callback handler for VREngage RTD components
28//!
29//! This singleton class manages the periodic updating of RTD simulation states
30//! during each simulation frame. It synchronizes the time between the RTD and VRF
31//! systems, and handles environmental updates such as wind conditions.
32class RTD_VRF_BACKEND_PLUGIN_EXPORT DtVreRtdTickCallback
33{
34public:
35 //! \brief Default constructor
36 //!
37 //! Initializes a new tick callback handler with default values.
44
45 //! \brief Static callback function for frame ticks
46 //! \param usr User data pointer (points to the tick callback instance)
47 //!
48 //! Called once per simulation frame to update the RTD world. This function
49 //! synchronizes the RTD simulation time with the VRF exercise time and
50 //! performs one or more RTD world updates as needed to keep the systems
51 //! in sync.
52 static void callback(void* usr)
53 {
54 DtVRF_PROFILE("vreRtd::tickCallback");
55
57
58 if (!DtVreRtdGlobal::getInstance()->cgf()->simulationServices()->isPaused())
59 {
60 // Update environmental conditions like wind
61 posttickcb->updateWind();
62
63 // Get current exercise time and fixed delta time
64 DtTime exerciseTime = DtVreRtdGlobal::getInstance()->cgf()->simulationServices()->exerciseTime();
65 smReal fixedDt = vreRtd::DtVreRtdGlobal::getInstance()->rtdWorld()->getFixedDeltaTime();
66
67 // Subtract fixed delta time to avoid the simulation time of RTD exceeding the
68 // VRF simulation time (see myTimeAccumulator of RTD world for details).
69 // This doesn't consider time accumulator of world, so there may be
70 // another update step after world update().
71 smReal dt = exerciseTime - DtVreRtdGlobal::getInstance()->rtdWorld()->getSimulationTime() - fixedDt;
72
73 // Ensure non-negative time step
74 if (dt < 0)
75 {
76 dt = 0;
77 }
78
79 // Perform initial world update
80 DtVreRtdGlobal::getInstance()->rtdWorld()->update(dt);
81
82 // If there is enough time, make another update to catch up fully
83 if (exerciseTime - DtVreRtdGlobal::getInstance()->rtdWorld()->getSimulationTime() >= fixedDt)
84 {
85 DtVreRtdGlobal::getInstance()->rtdWorld()->update(fixedDt);
86 }
87 }
88 }
89
90 //! \brief Singleton instance of the tick callback handler
92
93 //! \brief Gets the singleton instance of the tick callback handler
94 //! \return Pointer to the singleton instance
95 //!
96 //! Returns the singleton instance of the tick callback handler, creating it
97 //! if it doesn't already exist. This is the proper way to access the tick callback handler.
99 {
100 if (theInstance == 0)
101 {
103 }
104
105 return theInstance;
106 }
107
108 //! \brief Updates the current wind conditions
109 //!
110 //! Updates the current wind direction and speed from the environment.
111 //! This is called once per frame during the tick callback.
113
114 //! \brief Timestamp of the current frame
115 //!
116 //! Stores the timestamp of the current frame for time synchronization.
118
119 //! \brief Current wind direction in radians
121
122 //! \brief Current wind speed in meters per second
124};
125} // namespace vreRtd
static DtVreRtdGlobal * getInstance()
Gets the singleton instance of the global RTD manager.
RTD::World * rtdWorld()
Gets the RTD world object.
Definition vreRtdGlobal.h:192
static DtVreRtdTickCallback * getInstance()
Gets the singleton instance of the tick callback handler.
Definition vreRtdTickCallback.h:98
DtVreRtdTickCallback()
Default constructor.
Definition vreRtdTickCallback.h:38
static void callback(void *usr)
Static callback function for frame ticks.
Definition vreRtdTickCallback.h:52
double myCurWindDir
Current wind direction in radians.
Definition vreRtdTickCallback.h:120
double myCurWindSpeed
Current wind speed in meters per second.
Definition vreRtdTickCallback.h:123
smReal myFrameTimestamp
Timestamp of the current frame.
Definition vreRtdTickCallback.h:117
void updateWind()
Updates the current wind conditions.
static DtVreRtdTickCallback * theInstance
Singleton instance of the tick callback handler.
Definition vreRtdTickCallback.h:91
Definition vreRtdConfigFileReader.h:18
Common definitions for the RTD backend library.
Global singleton manager for VREngage RTD components.