VR-Engage  2.2
Loading...
Searching...
No Matches
projectile.h
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4*******************************************************************************/
5
6//! \file projectile.h
7//! \ingroup vreVrfobjcore
8//! \brief Projectile class for VREngage
9//!
10//! This file defines the DtProjectile class, which represents objects fired from weapons
11//! in VREngage simulations. It models projectiles that follow ballistic trajectories,
12//! taking into account the effects of gravity and drag.
13
14#pragma once
18
19#include <readerWriter/rwReal.h>
20#include <readerWriter/rwVector.h>
21
22#include <vrfobjcore/simObjectReference.h>
23
24#include <vrfutil/rwEntityType.h>
25
26class DtPhysicalWorld;
27
28namespace makVre
29{
31
32//! \brief Type identifier string for the projectile class
33const char DtProjectileType[] = "DtProjectile";
34
35//! \brief Class representing ballistic projectiles in simulations
36//!
37//! This class represents objects fired from weapons that follow ballistic trajectories,
38//! taking into account effects such as gravity and drag. Projectiles are updated each
39//! simulation frame to calculate their position based on physics, and can be used to
40//! detect collisions with terrain or other entities.
41class VREVRFOBJCORE_DLL DtProjectile : public DtReaderWriter
42{
43public:
44 //! \brief Constructor with string name
45 //! \param name The name of this projectile
46 //! \param parentRegistry Parent registry for reader/writer functionality (optional)
47 //!
48 //! Creates a new projectile with the specified name.
49 DtProjectile(const DtString& name, DtReaderWriterRegistry* const parentRegistry = NULL);
50
51 //! \brief Constructor with symbol string name
52 //! \param name The name of this projectile as a symbol string
53 //! \param parentRegistry Parent registry for reader/writer functionality (optional)
54 //!
55 //! Creates a new projectile with the specified name.
56 DtProjectile(const DtSymbolString& name, DtReaderWriterRegistry* const parentRegistry = NULL);
57
58 //! \brief Copy constructor
59 //! \param orig The source projectile to copy from
60 //! \param parentRegistry Parent registry for reader/writer functionality (optional)
61 //!
62 //! Creates a new projectile as a copy of the provided original.
63 DtProjectile(const DtProjectile& orig, DtReaderWriterRegistry* const parentRegistry = NULL);
64
65 //! \brief Virtual destructor
66 //!
67 //! Cleans up resources used by the projectile.
68 virtual ~DtProjectile() override {}
69
70 //! \brief Assignment operator
71 //! \param orig The source projectile to copy from
72 //! \return Reference to this projectile after assignment
73 //!
74 //! Assigns the contents of the provided projectile to this one.
76
77 //! \brief Creates a clone of this projectile
78 //! \return Pointer to a new projectile that is a clone of this one
79 //!
80 //! Creates a new projectile as a deep copy of this one.
81 virtual DtProjectile* clone() const;
82
83 //! \brief Equality operator
84 //! \param other The projectile to compare with (unnamed parameter)
85 //! \return True if the projectiles are equal, false otherwise
86 //!
87 //! Compares this projectile with another to determine if they are equal.
88 bool operator==(const DtProjectile&) const;
89
90 //! \brief Inequality operator
91 //! \param rhs The projectile to compare with
92 //! \return True if the projectiles are not equal, false otherwise
93 //!
94 //! Compares this projectile with another to determine if they are not equal.
95 bool operator!=(const DtProjectile& rhs) { return !(*this == rhs); };
96
97 //! Returns the class type of projectile. Used to create the correct class type
98 //! of projectile object on read from an mtl file. Derived classes should
99 //! return their own unqiue type string to identify themselves. Returns
100 //! DtProjectileType.
101 virtual DtString type() const;
102
103 //! \brief Structure containing projectile update results
104 //!
105 //! This structure contains the results of updating a projectile's position,
106 //! including the distance traveled in the current frame and the last position.
108 {
109 //! \brief Distance traveled in the current frame in meters
111
112 //! \brief Last position of the projectile before the update
113 DtVector lastPosition;
114 };
115
116 //! \brief Updates the projectile's state for the current frame
117 //! \param deltaTime Time elapsed since the last update in seconds
118 //! \return Result of the update, including detonation information if applicable
119 //!
120 //! Updates the projectile's position and velocity based on physics calculations,
121 //! and checks for collisions with terrain or entities. If a collision is detected,
122 //! the returned result will contain information about the detonation.
123 virtual DtProjectileDetonationResult update(double deltaTime);
124
125 //! \brief Sets the munition type
126 //! \param munitionType The DIS entity type of the munition
127 //!
128 //! Sets the entity type that identifies this munition in DIS communications.
129 virtual void setMunitionType(const DtEntityType& munitionType);
130
131 //! \brief Sets the projectile cross-section
132 //! \param crossSection The cross-sectional area in square meters
133 //!
134 //! Sets the cross-sectional area of the projectile, which affects drag calculations.
135 virtual void setCrossSection(double crossSection);
136
137 //! \brief Sets the drag coefficient
138 //! \param dragCoefficient The dimensionless drag coefficient
139 //!
140 //! Sets the drag coefficient of the projectile, which affects how much air resistance
141 //! slows down the projectile in flight.
142 virtual void setDragCoefficient(double dragCoefficient);
143
144 //! \brief Sets the projectile mass
145 //! \param mass The mass in kilograms
146 //!
147 //! Sets the mass of the projectile in kilograms, which affects trajectory calculations.
148 virtual void setMass(double mass);
149
150 //! \brief Sets the maximum range
151 //! \param maxRange The maximum range in meters
152 //!
153 //! Sets the maximum range the projectile can travel before being removed from the simulation.
154 virtual void setMaxRange(double maxRange);
155
156 //! \brief Sets the projectile position
157 //! \param position The position vector in local coordinates
158 //!
159 //! Sets the current position of the projectile in local coordinates.
160 virtual void setPosition(DtVector position);
161
162 //! \brief Sets the projectile velocity
163 //! \param velocity The velocity vector in meters per second
164 //!
165 //! Sets the current velocity of the projectile in meters per second.
166 virtual void setVelocity(DtVector velocity);
167
168 //! \brief Sets the distance traveled
169 //! \param distanceTraveled The distance traveled in meters
170 //!
171 //! Sets the total distance the projectile has traveled since being fired.
172 virtual void setDistanceTraveled(double distanceTraveled);
173
174 //! \brief Sets the firing entity
175 //! \param firingEntity Pointer to the entity that fired this projectile
176 //!
177 //! Sets the entity that fired this projectile.
178 virtual void setFiringEntity(DtSimObject* firingEntity);
179
180 //! \brief Sets the physical world
181 //! \param physicalWorld Pointer to the physical world for terrain queries
182 //!
183 //! Sets the physical world used for terrain queries and gravity direction.
184 virtual void setPhysicalWorld(DtPhysicalWorld* physicalWorld);
185 //! \brief Gets the munition type
186 //! \return Reference to the DIS entity type of the munition
187 //!
188 //! Returns the entity type that identifies this munition in DIS communications.
189 virtual const DtEntityType& munitionType() { return myMunitionType; }
190
191 //! \brief Gets the projectile velocity
192 //! \return The current velocity vector in meters per second
193 //!
194 //! Returns the current velocity of the projectile in meters per second.
195 virtual DtVector velocity() { return myVelocity; }
196
197 //! Adds a given projectile creation function and corresponding
198 //! type to the DtProjectileFactory.
199 static void addProjectileCreatorFcn(const std::string& type, makVre::DtProjectileCreatorFcn fcn);
200
201 //! Overrides base class putData to output our type.
202 virtual void putData(std::string& fp, int indentLevel = 0) override;
203
204 //! getData is just used to get us past the type in the data stream,
205 //! since it's not actually stored.
206 virtual DtlObjPtr getData(DtlObjPtr args,
207 const DtReaderWriter::SearchPaths& searchPaths,
208 const DtVariableBindingsList& variableBindings) override;
209
210 //! The factory for creating objects of type DtProjectile. The
211 //! factory is used to generate projectiles on demand, when reading
212 //! in from file (say from a process state repository entry in an
213 //! order of battle file).
214 //! @{
217 //! @}
218
219 //! Creates and initializes a projectile when reading in from an
220 //! MTL (MAK Technologies Lisp) file (i.e. an order of battle file).
221 //! \param mtlStream Input stream from an MTL file.
222 //! \return Newly created and initialized instance of this class, with data
223 //! read in from the MTL stream.
224 static DtProjectile* createProjectile(DtlObjPtr mtlStream);
225
226 //! Creates and initializes a new instance of this class
227 //! \param type The projectile type identifier string
228 //! \param pName DtReaderWriter name of the component
229 //! \param parentRegistry Parent registry for reader/writer functionality (optional)
230 //! \return Newly created and initialized instance of this class
232 const char* type, const char* pName, DtReaderWriterRegistry* const parentRegistry = NULL);
233
234 //! \return Newly created instance of this class, with an empty
235 //! DtReaderWriter name. Creation methods are registered with a
236 //! DtComponentDescriptorFactory, so they can be created on demand as they
237 //! are read in from a parameter database file.
238 static DtProjectile* creator(const DtString& name, DtReaderWriterRegistry* const parentRegistry = NULL);
239
240protected:
241 //! \brief Default constructor (not implemented)
242 //!
243 //! Default constructor is protected and not implemented to prevent
244 //! creation of instances without proper initialization.
246
247 //! \brief Updates the projectile's position based on physics
248 //! \param deltaTime Time elapsed since the last update in seconds
249 //! \return Structure containing update results
250 //!
251 //! Internal method that calculates the projectile's new position and velocity
252 //! based on physics, including gravity and drag effects.
254
255 //! \brief Checks if the projectile has collided with anything
256 //! \param projectUpdateResult Results from the position update
257 //! \return Information about the collision and detonation
258 //!
259 //! Internal method that checks if the projectile has collided with terrain
260 //! or entities based on its updated position.
262
263protected:
264 //! \brief The DIS entity type of the munition
265 //!
266 //! Stores the entity type that identifies this munition in DIS communications.
267 DtRwEntityType myMunitionType;
268
269 //! \brief The cross-sectional area of the projectile
270 //!
271 //! Stores the cross-sectional area of the projectile in square meters,
272 //! which affects drag calculations.
274
275 //! \brief The drag coefficient of the projectile
276 //!
277 //! Stores the dimensionless drag coefficient, which affects how much
278 //! air resistance slows down the projectile in flight.
280
281 //! \brief The mass of the projectile in kilograms
282 //!
283 //! Stores the mass of the projectile, which affects trajectory calculations.
284 DtRwReal myMass;
285
286 //! \brief The maximum range of the projectile in meters
287 //!
288 //! Stores the maximum range the projectile can travel before being removed
289 //! from the simulation. This is technically a weapon property but is stored
290 //! with the projectile to determine the maximum flight distance.
291 DtRwReal myMaxRange;
292
293 //! \brief The current position of the projectile
294 //!
295 //! Stores the current position of the projectile in local coordinates.
296 //! This is updated every frame.
297 DtRwVector myPosition;
298
299 //! \brief The current velocity of the projectile
300 //!
301 //! Stores the current velocity of the projectile in meters per second.
302 //! This is updated every frame based on physics calculations.
303 DtRwVector myVelocity;
304
305 //! \brief The total distance the projectile has traveled
306 //!
307 //! Stores the total distance the projectile has traveled since being fired.
308 //! This is used to test against the maximum range to determine when to
309 //! remove the projectile if it hasn't collided with anything.
311
312 //! \brief Pointer to the entity that fired this projectile
313 //!
314 //! Stores a pointer to the entity that fired this projectile.
315 DtSimObject* myFiringEntity;
316
317 //! \brief Pointer to the physical world
318 //!
319 //! Stores a pointer to the physical world for terrain queries and
320 //! to retrieve the world's gravity direction.
321 DtPhysicalWorld* myPhysicalWorld;
322
323 //! \brief Factory for creating projectile instances
324 //!
325 //! Static pointer to the factory used to construct projectiles. Used to create
326 //! the appropriate type of DtProjectile object when reading from files.
328};
329} // namespace makVre
Class representing the result of a projectile detonation.
Definition projectileDetonationResult.h:33
Factory for creating projectile objects.
Definition projectileFactory.h:42
DtRwReal myCrossSection
The cross-sectional area of the projectile.
Definition projectile.h:273
DtRwReal myDragCoefficient
The drag coefficient of the projectile.
Definition projectile.h:279
virtual ProjectileUpdateResult updateProjectilePosition(double deltaTime)
Updates the projectile's position based on physics.
virtual ~DtProjectile() override
Virtual destructor.
Definition projectile.h:68
virtual void setPosition(DtVector position)
Sets the projectile position.
virtual DtString type() const
Returns the class type of projectile. Used to create the correct class type of projectile object on r...
DtProjectile()
Default constructor (not implemented)
virtual void setCrossSection(double crossSection)
Sets the projectile cross-section.
virtual DtProjectile * clone() const
Creates a clone of this projectile.
static DtProjectile * createProjectile(DtlObjPtr mtlStream)
Creates and initializes a projectile when reading in from an MTL (MAK Technologies Lisp) file (i....
virtual DtlObjPtr getData(DtlObjPtr args, const DtReaderWriter::SearchPaths &searchPaths, const DtVariableBindingsList &variableBindings) override
getData is just used to get us past the type in the data stream, since it's not actually stored.
static DtProjectileFactory * theFactory
Factory for creating projectile instances.
Definition projectile.h:327
static void addProjectileCreatorFcn(const std::string &type, makVre::DtProjectileCreatorFcn fcn)
Adds a given projectile creation function and corresponding type to the DtProjectileFactory.
DtProjectile(const DtString &name, DtReaderWriterRegistry *const parentRegistry=NULL)
Constructor with string name.
static DtProjectileFactory * factory()
The factory for creating objects of type DtProjectile. The factory is used to generate projectiles on...
virtual DtProjectileDetonationResult update(double deltaTime)
Updates the projectile's state for the current frame.
virtual void setMaxRange(double maxRange)
Sets the maximum range.
DtProjectile(const DtSymbolString &name, DtReaderWriterRegistry *const parentRegistry=NULL)
Constructor with symbol string name.
DtSimObject * myFiringEntity
Pointer to the entity that fired this projectile.
Definition projectile.h:315
DtRwEntityType myMunitionType
The DIS entity type of the munition.
Definition projectile.h:267
static DtProjectile * createProjectile(const char *type, const char *pName, DtReaderWriterRegistry *const parentRegistry=NULL)
Creates and initializes a new instance of this class.
virtual void setFiringEntity(DtSimObject *firingEntity)
Sets the firing entity.
DtRwReal myMaxRange
The maximum range of the projectile in meters.
Definition projectile.h:291
DtRwReal myMass
The mass of the projectile in kilograms.
Definition projectile.h:284
static void setFactory(DtProjectileFactory *factory)
The factory for creating objects of type DtProjectile. The factory is used to generate projectiles on...
DtRwVector myVelocity
The current velocity of the projectile.
Definition projectile.h:303
virtual const DtEntityType & munitionType()
Gets the munition type.
Definition projectile.h:189
DtProjectile & operator=(const DtProjectile &orig)
Assignment operator.
virtual void setMunitionType(const DtEntityType &munitionType)
Sets the munition type.
virtual void setDragCoefficient(double dragCoefficient)
Sets the drag coefficient.
virtual DtVector velocity()
Gets the projectile velocity.
Definition projectile.h:195
bool operator!=(const DtProjectile &rhs)
Inequality operator.
Definition projectile.h:95
DtPhysicalWorld * myPhysicalWorld
Pointer to the physical world.
Definition projectile.h:321
DtRwVector myPosition
The current position of the projectile.
Definition projectile.h:297
virtual void setVelocity(DtVector velocity)
Sets the projectile velocity.
DtProjectile(const DtProjectile &orig, DtReaderWriterRegistry *const parentRegistry=NULL)
Copy constructor.
virtual void setMass(double mass)
Sets the projectile mass.
static DtProjectile * creator(const DtString &name, DtReaderWriterRegistry *const parentRegistry=NULL)
virtual void setPhysicalWorld(DtPhysicalWorld *physicalWorld)
Sets the physical world.
bool operator==(const DtProjectile &) const
Equality operator.
virtual void setDistanceTraveled(double distanceTraveled)
Sets the distance traveled.
DtRwReal myDistanceTraveled
The total distance the projectile has traveled.
Definition projectile.h:310
virtual void putData(std::string &fp, int indentLevel=0) override
Overrides base class putData to output our type.
virtual DtProjectileDetonationResult checkForProjectileCollision(const ProjectileUpdateResult &projectUpdateResult)
Checks if the projectile has collided with anything.
Export macros for the VREngage Object Core library.
#define VREVRFOBJCORE_DLL
Platform-specific DLL export/import declaration.
Definition export.h:27
Include export definitions for this library.
Definition glsVreMessageUtil.h:49
DtProjectile *(*)(const DtString &name, DtReaderWriterRegistry *const parentRegistry) DtProjectileCreatorFcn
Function pointer type for projectile creation functions.
Definition projectileFactory.h:34
const char DtProjectileType[]
Type identifier string for the projectile class.
Definition projectile.h:33
Projectile detonation result class for VREngage.
Structure containing projectile update results.
Definition projectile.h:108
DtVector lastPosition
Last position of the projectile before the update.
Definition projectile.h:113
double distanceTraveledInFrame
Distance traveled in the current frame in meters.
Definition projectile.h:110