afxXM_VelocityOffset.cpp
Engine/source/afx/xm/afxXM_VelocityOffset.cpp
Classes:
Public Functions
ConsoleDocClass(afxXM_VelocityOffsetData , "@brief An xmod <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">datablock.\n\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">afxXMods\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">AFX\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Datablocks\n</a>" )
Detailed Description
Public Functions
ConsoleDocClass(afxXM_VelocityOffsetData , "@brief An xmod <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">datablock.\n\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">afxXMods\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">AFX\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Datablocks\n</a>" )
IMPLEMENT_CO_DATABLOCK_V1(afxXM_VelocityOffsetData )
1 2 3//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 4// Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames 5// Copyright (C) 2015 Faust Logic, Inc. 6// 7// Permission is hereby granted, free of charge, to any person obtaining a copy 8// of this software and associated documentation files (the "Software"), to 9// deal in the Software without restriction, including without limitation the 10// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 11// sell copies of the Software, and to permit persons to whom the Software is 12// furnished to do so, subject to the following conditions: 13// 14// The above copyright notice and this permission notice shall be included in 15// all copies or substantial portions of the Software. 16// 17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23// IN THE SOFTWARE. 24// 25//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 26 27#include "afx/arcaneFX.h" 28 29#include "math/mathIO.h" 30#include "math/mathUtils.h" 31 32#include "afx/afxEffectWrapper.h" 33#include "afx/afxChoreographer.h" 34#include "afx/xm/afxXfmMod.h" 35#include "afx/util/afxPath3D.h" 36#include "afx/util/afxPath.h" 37 38//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 39// VELOCITY OFFSET 40 41class afxXM_VelocityOffsetData : public afxXM_WeightedBaseData 42{ 43 typedef afxXM_WeightedBaseData Parent; 44 45public: 46 F32 offset_factor; 47 bool offset_pos2; 48 bool normalize; 49 50public: 51 /*C*/ afxXM_VelocityOffsetData(); 52 /*C*/ afxXM_VelocityOffsetData(const afxXM_VelocityOffsetData&, bool = false); 53 54 void packData(BitStream* stream); 55 void unpackData(BitStream* stream); 56 57 virtual bool allowSubstitutions() const { return true; } 58 59 static void initPersistFields(); 60 61 afxXM_Base* create(afxEffectWrapper* fx, bool on_server); 62 63 DECLARE_CONOBJECT(afxXM_VelocityOffsetData); 64 DECLARE_CATEGORY("AFX"); 65}; 66 67//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~// 68 69class afxXM_VelocityOffset_weighted : public afxXM_WeightedBase 70{ 71 typedef afxXM_WeightedBase Parent; 72 73 afxConstraint* cons; 74 F32 offset_factor; 75 bool normalize; 76 77public: 78 /*C*/ afxXM_VelocityOffset_weighted(afxXM_VelocityOffsetData*, afxEffectWrapper*); 79 80 virtual void start(F32 timestamp); 81 virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params); 82}; 83 84//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~// 85 86// this fixed variation is used when 87// the weight factors are constant. 88 89class afxXM_VelocityOffset_fixed : public afxXM_Base 90{ 91 typedef afxXM_Base Parent; 92 93 afxConstraint* cons; 94 F32 offset_factor; 95 bool normalize; 96 97public: 98 /*C*/ afxXM_VelocityOffset_fixed(afxXM_VelocityOffsetData*, afxEffectWrapper*); 99 100 virtual void start(F32 timestamp); 101 virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params); 102}; 103 104//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~// 105 106class afxXM_VelocityOffset2_weighted : public afxXM_WeightedBase 107{ 108 typedef afxXM_WeightedBase Parent; 109 110 afxConstraint* cons; 111 F32 offset_factor; 112 bool normalize; 113 114public: 115 /*C*/ afxXM_VelocityOffset2_weighted(afxXM_VelocityOffsetData*, afxEffectWrapper*); 116 117 virtual void start(F32 timestamp); 118 virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params); 119}; 120 121//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~// 122 123// this fixed variation is used when 124// the weight factors are constant. 125 126class afxXM_VelocityOffset2_fixed : public afxXM_Base 127{ 128 typedef afxXM_Base Parent; 129 130 afxConstraint* cons; 131 F32 offset_factor; 132 bool normalize; 133 134public: 135 /*C*/ afxXM_VelocityOffset2_fixed(afxXM_VelocityOffsetData*, afxEffectWrapper*); 136 137 virtual void start(F32 timestamp); 138 virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params); 139}; 140 141//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 142// VELOCITY OFFSET 143 144IMPLEMENT_CO_DATABLOCK_V1(afxXM_VelocityOffsetData); 145 146ConsoleDocClass( afxXM_VelocityOffsetData, 147 "@brief An xmod datablock.\n\n" 148 149 "@ingroup afxXMods\n" 150 "@ingroup AFX\n" 151 "@ingroup Datablocks\n" 152); 153 154afxXM_VelocityOffsetData::afxXM_VelocityOffsetData() 155{ 156 offset_factor = 1.0f; 157 offset_pos2 = false; 158 normalize = false; 159} 160 161afxXM_VelocityOffsetData::afxXM_VelocityOffsetData(const afxXM_VelocityOffsetData& other, bool temp_clone) : afxXM_WeightedBaseData(other, temp_clone) 162{ 163 offset_factor = other.offset_factor; 164 offset_pos2 = other.offset_pos2; 165 normalize = other.normalize; 166} 167 168void afxXM_VelocityOffsetData::initPersistFields() 169{ 170 addField("offsetFactor", TypeF32, Offset(offset_factor, afxXM_VelocityOffsetData), 171 "..."); 172 addField("offsetPos2", TypeBool, Offset(offset_pos2, afxXM_VelocityOffsetData), 173 "..."); 174 addField("normalize", TypeBool, Offset(normalize, afxXM_VelocityOffsetData), 175 "..."); 176 177 Parent::initPersistFields(); 178} 179 180void afxXM_VelocityOffsetData::packData(BitStream* stream) 181{ 182 Parent::packData(stream); 183 stream->write(offset_factor); 184 stream->write(offset_pos2); 185} 186 187void afxXM_VelocityOffsetData::unpackData(BitStream* stream) 188{ 189 Parent::unpackData(stream); 190 stream->read(&offset_factor); 191 stream->read(&offset_pos2); 192} 193 194afxXM_Base* afxXM_VelocityOffsetData::create(afxEffectWrapper* fx, bool on_server) 195{ 196 afxXM_VelocityOffsetData* datablock = this; 197 198 if (getSubstitutionCount() > 0) 199 { 200 datablock = new afxXM_VelocityOffsetData(*this, true); 201 this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex()); 202 } 203 204 if (datablock->offset_pos2) 205 { 206 if (datablock->hasFixedWeight()) 207 return new afxXM_VelocityOffset2_fixed(datablock, fx); 208 else 209 return new afxXM_VelocityOffset2_weighted(datablock, fx); 210 } 211 else 212 { 213 if (datablock->hasFixedWeight()) 214 return new afxXM_VelocityOffset_fixed(datablock, fx); 215 else 216 return new afxXM_VelocityOffset_weighted(datablock, fx); 217 } 218} 219 220//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~// 221 222afxXM_VelocityOffset_weighted::afxXM_VelocityOffset_weighted(afxXM_VelocityOffsetData* db, afxEffectWrapper* fxw) 223: afxXM_WeightedBase(db, fxw) 224{ 225 cons = 0; 226 offset_factor = db->offset_factor*db->getWeightFactor(); 227 normalize = db->normalize; 228} 229 230void afxXM_VelocityOffset_weighted::start(F32 timestamp) 231{ 232 Parent::start(timestamp); 233 234 cons = fx_wrapper->getPosConstraint(); 235 if (!cons) 236 Con::errorf(ConsoleLogEntry::General, 237 "afxXM_VelocityOffset: failed to find a SceneObject derived constraint source."); 238} 239 240void afxXM_VelocityOffset_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params) 241{ 242 if (!cons) 243 return; 244 245 SceneObject* scene_obj = cons->getSceneObject(); 246 if (scene_obj) 247 { 248 Point3F vel_vec = scene_obj->getVelocity(); 249 if (!vel_vec.isZero()) 250 { 251 if (normalize) 252 vel_vec.normalize(); 253 F32 wt_factor = calc_weight_factor(elapsed); 254 params.pos += vel_vec*offset_factor*wt_factor; 255 } 256 } 257} 258 259//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~// 260 261afxXM_VelocityOffset_fixed::afxXM_VelocityOffset_fixed(afxXM_VelocityOffsetData* db, afxEffectWrapper* fxw) 262: afxXM_Base(db, fxw) 263{ 264 cons = 0; 265 offset_factor = db->offset_factor*db->getWeightFactor(); 266 normalize = db->normalize; 267} 268 269void afxXM_VelocityOffset_fixed::start(F32 timestamp) 270{ 271 Parent::start(timestamp); 272 273 cons = fx_wrapper->getPosConstraint(); 274 if (!cons) 275 Con::errorf(ConsoleLogEntry::General, 276 "afxXM_VelocityOffset: failed to find a SceneObject derived constraint source."); 277} 278 279void afxXM_VelocityOffset_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params) 280{ 281 if (!cons) 282 return; 283 284 SceneObject* scene_obj = cons->getSceneObject(); 285 if (scene_obj) 286 { 287 Point3F vel_vec = scene_obj->getVelocity(); 288 if (!vel_vec.isZero()) 289 { 290 if (normalize) 291 vel_vec.normalize(offset_factor); 292 params.pos += vel_vec*offset_factor; 293 } 294 } 295} 296 297//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~// 298 299afxXM_VelocityOffset2_weighted::afxXM_VelocityOffset2_weighted(afxXM_VelocityOffsetData* db, afxEffectWrapper* fxw) 300: afxXM_WeightedBase(db, fxw) 301{ 302 cons = 0; 303 offset_factor = db->offset_factor*db->getWeightFactor(); 304 normalize = db->normalize; 305} 306 307void afxXM_VelocityOffset2_weighted::start(F32 timestamp) 308{ 309 Parent::start(timestamp); 310 311 cons = fx_wrapper->getAimConstraint(); 312 if (!cons) 313 Con::errorf(ConsoleLogEntry::General, 314 "afxXM_VelocityOffset: failed to find a SceneObject derived constraint source."); 315} 316 317void afxXM_VelocityOffset2_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params) 318{ 319 if (!cons) 320 return; 321 322 SceneObject* scene_obj = cons->getSceneObject(); 323 if (scene_obj) 324 { 325 Point3F vel_vec = scene_obj->getVelocity(); 326 if (!vel_vec.isZero()) 327 { 328 if (normalize) 329 vel_vec.normalize(); 330 F32 wt_factor = calc_weight_factor(elapsed); 331 params.pos2 += vel_vec*offset_factor*wt_factor; 332 } 333 } 334} 335 336//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~// 337 338afxXM_VelocityOffset2_fixed::afxXM_VelocityOffset2_fixed(afxXM_VelocityOffsetData* db, afxEffectWrapper* fxw) 339: afxXM_Base(db, fxw) 340{ 341 cons = 0; 342 offset_factor = db->offset_factor*db->getWeightFactor(); 343 normalize = db->normalize; 344} 345 346void afxXM_VelocityOffset2_fixed::start(F32 timestamp) 347{ 348 Parent::start(timestamp); 349 350 cons = fx_wrapper->getAimConstraint(); 351 if (!cons) 352 Con::errorf(ConsoleLogEntry::General, 353 "afxXM_VelocityOffset: failed to find a SceneObject derived constraint source."); 354} 355 356void afxXM_VelocityOffset2_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params) 357{ 358 if (!cons) 359 return; 360 361 SceneObject* scene_obj = cons->getSceneObject(); 362 if (scene_obj) 363 { 364 Point3F vel_vec = scene_obj->getVelocity(); 365 if (!vel_vec.isZero()) 366 { 367 if (normalize) 368 vel_vec.normalize(offset_factor); 369 params.pos2 += vel_vec*offset_factor; 370 } 371 } 372} 373 374//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 375