humanphys.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWPhys *
  23. * *
  24. * $Archive:: /Commando/Code/wwphys/humanphys.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 10/27/01 4:32p $*
  29. * *
  30. * $Revision:: 44 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * HumanPhysClass::HumanPhysClass -- Constructor *
  35. * HumanPhysClass::HumanPhysClass -- Constructor, initializes from a Definition *
  36. * HumanPhysClass::~HumanPhysClass -- Destructor *
  37. * HumanPhysClass::Timestep -- Simulate this object for time dt *
  38. * HumanPhysClass::Check_Ground -- check the ground state *
  39. * HumanPhysClass::Ballistic_Move -- ballistic motion *
  40. * HumanPhysClass::Slide_Move -- Sliding down a slope *
  41. * HumanPhysClass::Normal_Move -- Moving under user control *
  42. * HumanPhysClass::Compute_Desired_Move_Vector -- compute the move vector *
  43. * HumanPhysClass::Get_Factory -- returns the PersistFactory for save-load support *
  44. * HumanPhysClass::Save -- Save this object *
  45. * HumanPhysClass::Load -- Load this object *
  46. * HumanPhysClass::Render -- Render this object *
  47. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  48. #include "humanphys.h"
  49. #include "wwdebug.h"
  50. #include "physcoltest.h"
  51. #include "pscene.h"
  52. #include "physcon.h"
  53. #include "physcontrol.h"
  54. #include "wwphysids.h"
  55. #include "persistfactory.h"
  56. #include "simpledefinitionfactory.h"
  57. #include "wwhack.h"
  58. #include "wwprofile.h"
  59. DECLARE_FORCE_LINK(humanphys);
  60. #define VERBOSE_LOGGING 0
  61. #if VERBOSE_LOGGING
  62. #define VERBOSE_LOG(x) if (WWDEBUG_TRIGGER(WWDEBUG_TRIGGER_GENERIC0)) { WWDEBUG_SAY(x); }
  63. #else
  64. #define VERBOSE_LOG(x)
  65. #endif
  66. bool HumanPhysClass::_DisableHumanSimulation = false;
  67. bool HumanPhysClass::_DisableHumanRendering = false;
  68. /*
  69. ** Declare a PersistFactory for HumanPhysClass
  70. */
  71. SimplePersistFactoryClass<HumanPhysClass,PHYSICS_CHUNKID_HUMANPHYS> _HumanPhysFactory;
  72. /*
  73. ** Chunk-ID's used by HumanPhysClass
  74. */
  75. enum
  76. {
  77. HUMANPHYS_CHUNK_PHYS3 = 0x04040404, // parent Phys3Class data
  78. HUMANPHYS_CHUNK_VARIABLES,
  79. HUMANPHYS_VARIABLE_JUSTJUMPED = 0x00
  80. };
  81. /*
  82. ** SLOPE_SPEED_ADJUSTMENT - fraction of the speed to shave off when player is walking on
  83. ** the steepest possible slope (just before sliding takes over). Also the fraction to
  84. ** add when the player is walking straight downhill
  85. ** AIR_SPEED_ADJUSTMENT - scale factor to apply to the users move when in the air
  86. */
  87. const float SLOPE_SPEED_REDUCTION = 0.1f;
  88. const float AIR_MOVE_SCALE = 0.1f;
  89. /***********************************************************************************************
  90. * HumanPhysClass::HumanPhysClass -- Constructor *
  91. * *
  92. * INPUT: *
  93. * *
  94. * OUTPUT: *
  95. * *
  96. * WARNINGS: *
  97. * *
  98. * HISTORY: *
  99. * 9/16/2000 gth : Created. *
  100. *=============================================================================================*/
  101. HumanPhysClass::HumanPhysClass(void) :
  102. IsAIControlledJump (false)
  103. {
  104. JustJumped = false;
  105. Set_Mass(1.0f); // ??
  106. // (gth) turn on shadows for all humans for now
  107. Enable_Shadow_Generation(true);
  108. }
  109. /***********************************************************************************************
  110. * HumanPhysClass::Init -- initializes from a Definition *
  111. * *
  112. * INPUT: *
  113. * *
  114. * OUTPUT: *
  115. * *
  116. * WARNINGS: *
  117. * *
  118. * HISTORY: *
  119. * 9/16/2000 gth : Created. *
  120. *=============================================================================================*/
  121. void HumanPhysClass::Init(const HumanPhysDefClass & def)
  122. {
  123. Phys3Class::Init(def);
  124. // (gth) turn on shadows for all humans for now
  125. Enable_Shadow_Generation(true);
  126. }
  127. /***********************************************************************************************
  128. * HumanPhysClass::~HumanPhysClass -- Destructor *
  129. * *
  130. * INPUT: *
  131. * *
  132. * OUTPUT: *
  133. * *
  134. * WARNINGS: *
  135. * *
  136. * HISTORY: *
  137. *=============================================================================================*/
  138. HumanPhysClass::~HumanPhysClass(void)
  139. {
  140. }
  141. /***********************************************************************************************
  142. * HumanPhysClass::Timestep -- Simulate this object for time dt *
  143. * *
  144. * INPUT: *
  145. * *
  146. * OUTPUT: *
  147. * *
  148. * WARNINGS: *
  149. * *
  150. * HISTORY: *
  151. * 9/16/2000 gth : Created. *
  152. *=============================================================================================*/
  153. void HumanPhysClass::Timestep(float dt)
  154. {
  155. VERBOSE_LOG(("HumanPhys::Timestep\r\n"));
  156. bool was_on_ground = OnGround;
  157. if (Is_Asleep()) {
  158. if ((Controller != NULL) && (!Controller->Is_Inactive())) {
  159. Set_Flag(ASLEEP,false);
  160. }
  161. }
  162. Phys3Class::Timestep(dt);
  163. if (Is_Asleep()) {
  164. return;
  165. }
  166. {
  167. WWPROFILE("HumanPhys::Timestep");
  168. // if we didn't jump, we're not on the ground, we were on the ground, and our Z velocity is >0, set it to 0
  169. if (was_on_ground && !OnGround && !JustJumped && (State.Velocity.Z > 0.0f)) {
  170. State.Velocity.Z = 0.0f;
  171. }
  172. // clear the just jumped flag if its set and we've reached the apex.
  173. if (JustJumped && State.Velocity.Z < 0.0f) {
  174. JustJumped = false;
  175. }
  176. }
  177. }
  178. /***********************************************************************************************
  179. * HumanPhysClass::Render -- Render this object *
  180. * *
  181. * INPUT: *
  182. * *
  183. * OUTPUT: *
  184. * *
  185. * WARNINGS: *
  186. * *
  187. * HISTORY: *
  188. *=============================================================================================*/
  189. void HumanPhysClass::Render(RenderInfoClass & rinfo)
  190. {
  191. #ifdef WWDEBUG
  192. PhysicsSceneClass::Get_Instance()->Debug_Display_Dynamic_Vis_Node(VisNodeID);
  193. #endif
  194. Phys3Class::Render(rinfo);
  195. }
  196. /***********************************************************************************************
  197. * HumanPhysClass::Check_Ground -- check the ground state *
  198. * *
  199. * Humans override this so that they don't stick to the ground when they want to jump *
  200. * *
  201. * INPUT: *
  202. * *
  203. * OUTPUT: *
  204. * *
  205. * WARNINGS: *
  206. * *
  207. * HISTORY: *
  208. * 9/16/2000 gth : Created. *
  209. * 7/20/2001 gth : Created. *
  210. *=============================================================================================*/
  211. void HumanPhysClass::Check_Ground(const AABoxClass & box,GroundStateStruct * gs,float check_dist)
  212. {
  213. if (JustJumped) {
  214. gs->OnGround = false;
  215. } else {
  216. Phys3Class::Check_Ground(box,gs,check_dist);
  217. }
  218. }
  219. /***********************************************************************************************
  220. * HumanPhysClass::Ballistic_Move -- ballistic motion *
  221. * *
  222. * INPUT: *
  223. * *
  224. * OUTPUT: *
  225. * *
  226. * WARNINGS: *
  227. * *
  228. * HISTORY: *
  229. * 9/16/2000 gth : Created. *
  230. *=============================================================================================*/
  231. bool HumanPhysClass::Ballistic_Move(float dt)
  232. {
  233. WWPROFILE("HumanPhys::Ballistic_Move");
  234. VERBOSE_LOG(("HumanPhys::Ballistic_Move\r\n"));
  235. // Compute a move vector for the object flying through the air...
  236. Vector3 move;
  237. float accel = PhysicsConstants::GravityAcceleration.Z * GravScale;
  238. Vector3 start_vel = State.Velocity;
  239. Vector3 start_pos = State.Position;
  240. move.X = State.Velocity.X * dt;
  241. move.Y = State.Velocity.Y * dt;
  242. move.Z = 0.5f * accel * dt * dt + State.Velocity.Z * dt;
  243. bool moved = Apply_Move(move,dt);
  244. // Compute X,Y velocities from the actual move that was performed
  245. State.Velocity.X = (State.Position.X - start_pos.X) / dt;
  246. State.Velocity.Y = (State.Position.Y - start_pos.Y) / dt;
  247. // Compute the analytical Z velocity and the ad-hoc Z velocity, the
  248. // more negative one is the one to keep. What this does is use the
  249. // analytical velocity unless the character hits a roof.
  250. State.Velocity.Z = start_vel.Z + accel * dt;
  251. #if 0
  252. State.Velocity.Z = min((State.Position.Z - start_pos.Z) / dt,start_vel.Z + accel * dt);
  253. #endif
  254. // Now let the user adjust the movement a little
  255. if (Controller && !IsAIControlledJump) {
  256. Vector3 player_move(Controller->Get_Move_Vector());
  257. player_move.Rotate_Z(Heading);
  258. State.Velocity.X += AIR_MOVE_SCALE * NormSpeed * player_move.X;
  259. State.Velocity.Y += AIR_MOVE_SCALE * NormSpeed * player_move.Y;
  260. Vector3 xy_vel = State.Velocity;
  261. xy_vel.Z = 0;
  262. if (xy_vel.Length2() > NormSpeed * NormSpeed) {
  263. float scale = NormSpeed / xy_vel.Length();
  264. State.Velocity.X *= scale;
  265. State.Velocity.Y *= scale;
  266. }
  267. }
  268. IsAIControlledJump &= (Is_In_Contact () == false);
  269. return moved;
  270. }
  271. /***********************************************************************************************
  272. * HumanPhysClass::Slide_Move -- Sliding down a slope *
  273. * *
  274. * Humans can jump when sliding down a slope *
  275. * *
  276. * INPUT: *
  277. * *
  278. * OUTPUT: *
  279. * *
  280. * WARNINGS: *
  281. * *
  282. * HISTORY: *
  283. * 9/16/2000 gth : Created. *
  284. *=============================================================================================*/
  285. bool HumanPhysClass::Slide_Move(const GroundStateStruct & gs,float dt)
  286. {
  287. WWPROFILE("HumanPhys::Slide_Move");
  288. VERBOSE_LOG(("HumanPhys::Slide_Move\r\n"));
  289. // Compute a move vector which causes the object to slide down the slope...
  290. Vector3 start_pos = State.Position;
  291. Vector3 move = NormSpeed * dt * gs.Down;
  292. if (Controller != NULL) {
  293. if (Controller->Get_Move_Vector().Z > 0.0f) {
  294. move.Z += Controller->Get_Move_Vector().Z * dt;
  295. JustJumped = true;
  296. }
  297. }
  298. bool moved = Apply_Move(move,dt,true,false,true);
  299. State.Velocity = (State.Position - start_pos)/dt;
  300. return moved;
  301. }
  302. /***********************************************************************************************
  303. * HumanPhysClass::Normal_Move -- Moving under user control *
  304. * *
  305. * INPUT: *
  306. * *
  307. * OUTPUT: *
  308. * *
  309. * WARNINGS: *
  310. * *
  311. * HISTORY: *
  312. * 9/16/2000 gth : Created. *
  313. *=============================================================================================*/
  314. bool HumanPhysClass::Normal_Move(const GroundStateStruct & gs,float dt)
  315. {
  316. WWPROFILE("HumanPhys::Normal_Move");
  317. VERBOSE_LOG(("HumanPhys::Normal_Move\r\n"));
  318. if (Controller == NULL) {
  319. return false;
  320. }
  321. /*
  322. ** STEP ONE: Compute the desired Move. The desired move is determined by the player's
  323. ** controller setting and the plane that we are currently standing on.
  324. */
  325. Vector3 move;
  326. Compute_Desired_Move_Vector(gs,dt,&move);
  327. /*
  328. ** STEP TWO: Try to apply the move and snap back down to the ground. If we did not
  329. ** jump and we end up on an un-walkable slope, we'll re-wind and clip our original
  330. ** move so that it avoids that slope.
  331. */
  332. Vector3 start_position = State.Position;
  333. Vector3 start_move = move;
  334. bool moved = Apply_Move(move,dt,true,!JustJumped);
  335. if ((moved) && (!JustJumped)) {
  336. Snap_To_Ground(State.Position - start_position,true);
  337. }
  338. /*
  339. ** STEP THREE: If we snapped down onto an un-walkable slope, revert our state,
  340. ** clip the original move against the equivalent wall for this slope, and try
  341. ** one more time.
  342. */
  343. if (moved && !JustJumped && (GroundState.OnGround) && (GroundState.Normal.Z < SlideNormalZ)) {
  344. State.Position = start_position;
  345. move = start_move;
  346. Vector3 slope_wall = GroundState.Normal;
  347. slope_wall.Z = 0.0f;
  348. slope_wall.Normalize();
  349. Clip_Move(&slope_wall,1,&move);
  350. moved = Apply_Move(move,dt,true,!JustJumped);
  351. if ((moved) && (!JustJumped)) {
  352. Snap_To_Ground(State.Position - start_position,true);
  353. }
  354. }
  355. /*
  356. ** STEP FOUR: Finally, compute our velocity in case we became airborne
  357. */
  358. State.Velocity = (State.Position - start_position) / dt;
  359. // BMG Unless they just jumped
  360. if ( !JustJumped ) {
  361. State.Velocity.Z = WWMath::Min(State.Velocity.Z,0.0f); // don't ever let humans launch off slopes
  362. }
  363. return moved;
  364. }
  365. /***********************************************************************************************
  366. * HumanPhysClass::Compute_Desired_Move_Vector -- compute the move vector *
  367. * *
  368. * INPUT: *
  369. * *
  370. * OUTPUT: *
  371. * *
  372. * WARNINGS: *
  373. * *
  374. * HISTORY: *
  375. * 9/16/2000 gth : Created. *
  376. *=============================================================================================*/
  377. void HumanPhysClass::Compute_Desired_Move_Vector(const GroundStateStruct & gs,float dt,Vector3 * set_move)
  378. {
  379. Vector3 move(Controller->Get_Move_Vector());
  380. if (move.Z < 0.0f) {
  381. move.Z = 0.0f;
  382. }
  383. float jump = move.Z;
  384. if (jump > 0.0f) {
  385. move.Z = 0.0f;
  386. JustJumped = true;
  387. }
  388. /*
  389. ** rotate the move vector into "2D" world space
  390. */
  391. move.Rotate_Z(Heading);
  392. /*
  393. ** rotate the move vector onto the current contact plane
  394. */
  395. Vector3 axis;
  396. Vector3::Cross_Product(Vector3(0,0,1),gs.Normal,&axis);
  397. float axis_len = axis.Length2();
  398. if (axis_len > 0.0f) {
  399. float s_angle = axis.Length();
  400. float c_angle = Vector3::Dot_Product(Vector3(0,0,1),gs.Normal);
  401. axis.Normalize();
  402. Matrix3 rotation(axis,s_angle,c_angle);
  403. Matrix3::Rotate_Vector(rotation,move,&move);
  404. }
  405. /*
  406. ** check the result to ensure it is 90deg from the plane normal (moving on plane)
  407. */
  408. #ifdef WWDEBUG
  409. Vector3 movedir = move;
  410. movedir.Normalize();
  411. WWASSERT(fabs(Vector3::Dot_Product(movedir,gs.Normal)) < WWMATH_EPSILON);
  412. #endif
  413. /*
  414. ** adjust the vector to make uphill slower and downhill faster
  415. */
  416. float dot = Vector3::Dot_Product(move,gs.Down);
  417. float scale = (1.0f - gs.Normal.Z) / (1.0f - SlideNormalZ);
  418. move = move + SLOPE_SPEED_REDUCTION * scale * dot * move;
  419. /*
  420. ** Multiply by the human velocity
  421. */
  422. move *= NormSpeed;
  423. if (jump > 0.0f) {
  424. move.Z = jump;
  425. }
  426. move *= dt;
  427. /*
  428. ** done!
  429. */
  430. *set_move = move;
  431. }
  432. /***********************************************************************************************
  433. * HumanPhysClass::Get_Factory -- returns the PersistFactory for save-load support *
  434. * *
  435. * INPUT: *
  436. * *
  437. * OUTPUT: *
  438. * *
  439. * WARNINGS: *
  440. * *
  441. * HISTORY: *
  442. * 9/16/2000 gth : Created. *
  443. *=============================================================================================*/
  444. const PersistFactoryClass & HumanPhysClass::Get_Factory(void) const
  445. {
  446. return _HumanPhysFactory;
  447. }
  448. /***********************************************************************************************
  449. * HumanPhysClass::Save -- Save this object *
  450. * *
  451. * INPUT: *
  452. * *
  453. * OUTPUT: *
  454. * *
  455. * WARNINGS: *
  456. * *
  457. * HISTORY: *
  458. * 9/16/2000 gth : Created. *
  459. *=============================================================================================*/
  460. bool HumanPhysClass::Save(ChunkSaveClass &csave)
  461. {
  462. csave.Begin_Chunk(HUMANPHYS_CHUNK_PHYS3);
  463. Phys3Class::Save(csave);
  464. csave.End_Chunk();
  465. csave.Begin_Chunk(HUMANPHYS_CHUNK_VARIABLES);
  466. WRITE_MICRO_CHUNK(csave,HUMANPHYS_VARIABLE_JUSTJUMPED,JustJumped);
  467. csave.End_Chunk();
  468. return true;
  469. }
  470. /***********************************************************************************************
  471. * HumanPhysClass::Load -- Load this object *
  472. * *
  473. * INPUT: *
  474. * *
  475. * OUTPUT: *
  476. * *
  477. * WARNINGS: *
  478. * *
  479. * HISTORY: *
  480. * 9/16/2000 gth : Created. *
  481. *=============================================================================================*/
  482. bool HumanPhysClass::Load(ChunkLoadClass &cload)
  483. {
  484. while (cload.Open_Chunk()) {
  485. switch(cload.Cur_Chunk_ID())
  486. {
  487. case HUMANPHYS_CHUNK_PHYS3:
  488. Phys3Class::Load(cload);
  489. break;
  490. case HUMANPHYS_CHUNK_VARIABLES:
  491. while (cload.Open_Micro_Chunk()) {
  492. switch(cload.Cur_Micro_Chunk_ID()) {
  493. READ_MICRO_CHUNK(cload,HUMANPHYS_VARIABLE_JUSTJUMPED,JustJumped);
  494. }
  495. cload.Close_Micro_Chunk();
  496. }
  497. break;
  498. default:
  499. WWDEBUG_SAY(("Unhandled Chunk: 0x%X File: %s Line: %d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  500. break;
  501. }
  502. cload.Close_Chunk();
  503. }
  504. return true;
  505. }
  506. /***********************************************************************************************
  507. * HumanPhysClass::Jump_To_Point -- Jump to the given X, Y, Z location. *
  508. * *
  509. * INPUT: *
  510. * *
  511. * OUTPUT: *
  512. * *
  513. * WARNINGS: *
  514. * *
  515. * HISTORY: *
  516. * 10/19/2000 pds : Created. *
  517. *=============================================================================================*/
  518. void HumanPhysClass::Jump_To_Point (const Vector3 &dest_pos)
  519. {
  520. const float gravity = PhysicsConstants::GravityAcceleration.Z * Get_Gravity_Multiplier();
  521. const float minangle = 0.3491f; // 20 degrees.
  522. const float maxangle = 1.2217f; // 70 degrees.
  523. Vector3 displacement, velocity, acceleration;
  524. float x, y, theta;
  525. float time;
  526. acceleration.Set (0.0f, 0.0f, gravity);
  527. displacement = (dest_pos - Get_Transform().Get_Translation());
  528. // Calculate a launch/landing angle most appropriate for the jump to be made.
  529. // Horizontal jumps will use the minimum launch angle, nera vertical jumps will
  530. // use the maximum launch/landing angle.
  531. x = Vector2 (displacement.X, displacement.Y).Length();
  532. y = WWMath::Fabs (displacement.Z);
  533. theta = WWMath::Lerp (minangle, maxangle, y / (x + y));
  534. // If character is jumping up then theta is the landing angle
  535. // - otherwise theta is the launch angle.
  536. if (displacement.Z >= 0.0f) {
  537. // Using standard projectile math, final velocity is given by:
  538. //
  539. // vfy = viy + g * t (1)
  540. //
  541. // and
  542. //
  543. // vfx = vix = x / t (2)
  544. //
  545. // and
  546. //
  547. // viy = y / t - 0.5 * g * t (3)
  548. //
  549. // where vfy is final vertical velocity, viy is initial vertical velocity, g is acceleration
  550. // due to gravity, t is time, vfx is final horizontal velocity, vix is initial horizontal
  551. // velocity, y is vertical displacement.
  552. //
  553. // We require at landing time:
  554. // tan (theta) = vfy / vfx
  555. // = (viy + g * t) / (x / t) (4).
  556. //
  557. // Substituting (3) in (4):
  558. //
  559. // tan (theta) = (y / t - 0.5 * g * t + g * t) / (x / t).
  560. //
  561. // Rearranging gives:
  562. //
  563. // t = sqrt ((2.0f * (-x * tan (theta) - y)) / g).
  564. time = sqrt (2.0f * ((-x * tanf (theta)) - y) / gravity);
  565. } else {
  566. // Using standard projectile math, vertical displacement is given by:
  567. //
  568. // y = s * sin(theta) * t + 0.5 * g * t^2 (1)
  569. //
  570. // where s is speed, theta is launch angle, g is acceleration due to gravity(-ve),
  571. // and t is time.
  572. //
  573. // Horizontal displacement is given by:
  574. //
  575. // x = s * sin(theta) * t (2)
  576. //
  577. // because horizontal velocity is constant.
  578. // Substituting (2) for t in (1) gives:
  579. //
  580. // y = (s * sin(theta) * x / (s * cos(theta))) + 0.5 * g * t^2
  581. //
  582. // which simplifies in terms of t to:
  583. //
  584. // t = sqrt (2.0 * (y - x * tan(theta)) / g).
  585. time = sqrt (2.0f * (-y - (x * tanf (theta))) / gravity);
  586. }
  587. // Calculate velocity vector using standard projectile math.
  588. velocity = (displacement / time) - ((0.5f * acceleration) * time);
  589. //
  590. // Set the new velocity
  591. //
  592. Set_Velocity(velocity);
  593. //
  594. // Reset some internal states so the physics object knows its
  595. // in a jumping state.
  596. //
  597. Invalidate_Ground_State();
  598. Set_Flag(ASLEEP,false);
  599. JustJumped = true;
  600. IsAIControlledJump = true;
  601. return ;
  602. }
  603. /****************************************************************************************************
  604. **
  605. ** HumanPhysDefClass Implementation
  606. **
  607. ****************************************************************************************************/
  608. /*
  609. ** Persist factory for HumanPhysDefClass
  610. */
  611. SimplePersistFactoryClass<HumanPhysDefClass,PHYSICS_CHUNKID_HUMANPHYSDEF> _HumanPhysDefFactory;
  612. /*
  613. ** Definition factory for HumanPhysDefClass. This makes it show up in the editor
  614. */
  615. DECLARE_DEFINITION_FACTORY(HumanPhysDefClass, CLASSID_HUMANPHYSDEF, "Human") _HumanPhysDefDefFactory;
  616. /*
  617. ** Chunk ID's used by HumanPhysDefClass
  618. */
  619. enum
  620. {
  621. HUMANPHYSDEF_CHUNK_PHYS3DEF = 0x00516000, // phys3def data (parent class)
  622. };
  623. HumanPhysDefClass::HumanPhysDefClass(void)
  624. {
  625. }
  626. uint32 HumanPhysDefClass::Get_Class_ID (void) const
  627. {
  628. return CLASSID_HUMANPHYSDEF;
  629. }
  630. PersistClass * HumanPhysDefClass::Create(void) const
  631. {
  632. HumanPhysClass * obj = NEW_REF(HumanPhysClass,());
  633. obj->Init(*this);
  634. return obj;
  635. }
  636. const PersistFactoryClass & HumanPhysDefClass::Get_Factory (void) const
  637. {
  638. return _HumanPhysDefFactory;
  639. }
  640. bool HumanPhysDefClass::Save(ChunkSaveClass &csave)
  641. {
  642. csave.Begin_Chunk(HUMANPHYSDEF_CHUNK_PHYS3DEF);
  643. Phys3DefClass::Save(csave);
  644. csave.End_Chunk();
  645. // no variables for now
  646. return true;
  647. }
  648. bool HumanPhysDefClass::Load(ChunkLoadClass &cload)
  649. {
  650. while (cload.Open_Chunk()) {
  651. switch(cload.Cur_Chunk_ID()) {
  652. case HUMANPHYSDEF_CHUNK_PHYS3DEF:
  653. Phys3DefClass::Load(cload);
  654. break;
  655. default:
  656. WWDEBUG_SAY(("Unhandled Chunk: 0x%X File: %s Line: %d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  657. break;
  658. }
  659. cload.Close_Chunk();
  660. }
  661. return true;
  662. }
  663. bool HumanPhysDefClass::Is_Type(const char * type_name)
  664. {
  665. if (stricmp(type_name,HumanPhysDefClass::Get_Type_Name()) == 0) {
  666. return true;
  667. } else {
  668. return Phys3DefClass::Is_Type(type_name);
  669. }
  670. }