home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 10 / Engine / SceneObject.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-10-01  |  20.2 KB  |  579 lines

  1. //-----------------------------------------------------------------------------
  2. // SceneObject.h implementation.
  3. // Refer to the SceneObject.h interface for more details.
  4. //
  5. // Programming a Multiplayer First Person Shooter in DirectX
  6. // Copyright (c) 2004 Vaughan Young
  7. //-----------------------------------------------------------------------------
  8. #include "Engine.h"
  9.  
  10. //-----------------------------------------------------------------------------
  11. // The scene object class constructor.
  12. //-----------------------------------------------------------------------------
  13. SceneObject::SceneObject( unsigned long type, char *meshName, char *meshPath, bool sharedMesh )
  14. {
  15.     // Set the object's type;
  16.     SetType( type );
  17.  
  18.     // Zero the scene object's translation and rotation.
  19.     SetTranslation( 0.0f, 0.0f, 0.0f );
  20.     SetRotation( 0.0f, 0.0f, 0.0f );
  21.  
  22.     // The object is initially at rest.
  23.     SetVelocity( 0.0f, 0.0f, 0.0f );
  24.     SetSpin( 0.0f, 0.0f, 0.0f );
  25.  
  26.     // The object is initially facing into the positive z-axis.
  27.     m_forward = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
  28.     m_right = D3DXVECTOR3( 1.0f, 0.0f, 0.0f );
  29.  
  30.     // Initially the object has no friction.
  31.     m_friction = 0.0f;
  32.  
  33.     // Clear the collision stamp.
  34.     m_collisionStamp = -1;
  35.  
  36.     // The object is visible, enabled, solid, and registering collisons by default.
  37.     m_visible = true;
  38.     m_enabled = true;
  39.     m_ghost = false;
  40.     m_ignoreCollisions = false;
  41.  
  42.     // Initially the object is not touching the ground.
  43.     m_touchingGround = false;
  44.  
  45.     // Set the object's mesh.
  46.     m_mesh = NULL;
  47.     SetMesh( meshName, meshPath, sharedMesh );
  48. }
  49.  
  50. //-----------------------------------------------------------------------------
  51. // The scene object class destructor.
  52. //-----------------------------------------------------------------------------
  53. SceneObject::~SceneObject()
  54. {
  55.     // Destroy the object's mesh.
  56.     if( m_sharedMesh == true )
  57.         g_engine->GetMeshManager()->Remove( &m_mesh );
  58.     else
  59.         SAFE_DELETE( m_mesh );
  60. }
  61.  
  62. //-----------------------------------------------------------------------------
  63. // Updates the object.
  64. //-----------------------------------------------------------------------------
  65. void SceneObject::Update( float elapsed, bool addVelocity )
  66. {
  67.     // Calculate the friction for this update.
  68.     float friction = 1.0f - m_friction * elapsed;
  69.  
  70.     // Move the object.
  71.     m_velocity *= friction;
  72.     if( addVelocity == true )
  73.     {
  74.         D3DXVECTOR3 velocity = m_velocity * elapsed;
  75.         AddTranslation( velocity.x, velocity.y, velocity.z );
  76.     }
  77.  
  78.     // Spin the object.
  79.     m_spin *= friction;
  80.     D3DXVECTOR3 spin = m_spin * elapsed;
  81.     AddRotation( spin.x, spin.y, spin.z );
  82.  
  83.     // Update the object's world matrix.
  84.     D3DXMatrixMultiply( &m_worldMatrix, &m_rotationMatrix, &m_translationMatrix );
  85.  
  86.     // Create a view matrix for the object.
  87.     D3DXMatrixInverse( &m_viewMatrix, NULL, &m_worldMatrix );
  88.  
  89.     // Update the object's forward vector.
  90.     m_forward.x = (float)sin( m_rotation.y );
  91.     m_forward.y = (float)-tan( m_rotation.x );
  92.     m_forward.z = (float)cos( m_rotation.y );
  93.     D3DXVec3Normalize( &m_forward, &m_forward );
  94.  
  95.     // Update the object's right vector.
  96.     m_right.x = (float)cos( m_rotation.y );
  97.     m_right.y = (float)tan( m_rotation.z );
  98.     m_right.z = (float)-sin( m_rotation.y );
  99.     D3DXVec3Normalize( &m_right, &m_right );
  100.  
  101.     // Update the object's bounding volume using the translation matrix only.
  102.     // This will maintain an axis aligned bounding box around the object in
  103.     // world space rather than the object's local space.
  104.     RepositionBoundingVolume( &m_translationMatrix );
  105. }
  106.  
  107. //-----------------------------------------------------------------------------
  108. // Renders the object.
  109. //-----------------------------------------------------------------------------
  110. void SceneObject::Render( D3DXMATRIX *world )
  111. {
  112.     // Ignore the object if it has no mesh.
  113.     if( m_mesh == NULL )
  114.         return;
  115.  
  116.     // Check if the object's world tranformation matrix has been overridden.
  117.     if( world == NULL )
  118.         g_engine->GetDevice()->SetTransform( D3DTS_WORLD, &m_worldMatrix );
  119.     else
  120.         g_engine->GetDevice()->SetTransform( D3DTS_WORLD, world );
  121.  
  122.     // Render the object's mesh.
  123.     m_mesh->Render();
  124. }
  125.  
  126. //-----------------------------------------------------------------------------
  127. // Called when something collides with the object.
  128. //-----------------------------------------------------------------------------
  129. void SceneObject::CollisionOccurred( SceneObject *object, unsigned long collisionStamp )
  130. {
  131.     // Set the collision stamp.
  132.     m_collisionStamp = collisionStamp;
  133. }
  134.  
  135. //-----------------------------------------------------------------------------
  136. // Applies the given force to the object in the forwards/backwards direction.
  137. //-----------------------------------------------------------------------------
  138. void SceneObject::Drive( float force, bool lockYAxis )
  139. {
  140.     D3DXVECTOR3 realForce = m_forward * force;
  141.  
  142.     m_velocity.x += realForce.x;
  143.     m_velocity.z += realForce.z;
  144.  
  145.     if( lockYAxis == false )
  146.         m_velocity.y += realForce.y;
  147. }
  148.  
  149. //-----------------------------------------------------------------------------
  150. // Applies the given force to the object in the right/left direction.
  151. //-----------------------------------------------------------------------------
  152. void SceneObject::Strafe( float force, bool lockYAxis )
  153. {
  154.     D3DXVECTOR3 realForce = m_right * force;
  155.  
  156.     m_velocity.x += realForce.x;
  157.     m_velocity.z += realForce.z;
  158.  
  159.     if( lockYAxis == false )
  160.         m_velocity.y += realForce.y;
  161. }
  162.  
  163. //-----------------------------------------------------------------------------
  164. // Stops the object moving.
  165. //-----------------------------------------------------------------------------
  166. void SceneObject::Stop()
  167. {
  168.     m_velocity = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
  169.     m_spin = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
  170. }
  171.  
  172. //-----------------------------------------------------------------------------
  173. // Sets the object's translation.
  174. //-----------------------------------------------------------------------------
  175. void SceneObject::SetTranslation( float x, float y, float z )
  176. {
  177.     m_translation.x = x;
  178.     m_translation.y = y;
  179.     m_translation.z = z;
  180.  
  181.     D3DXMatrixTranslation( &m_translationMatrix, m_translation.x, m_translation.y, m_translation.z );
  182. }
  183.  
  184. //-----------------------------------------------------------------------------
  185. // Sets the object's translation.
  186. //-----------------------------------------------------------------------------
  187. void SceneObject::SetTranslation( D3DXVECTOR3 translation )
  188. {
  189.     m_translation = translation;
  190.  
  191.     D3DXMatrixTranslation( &m_translationMatrix, m_translation.x, m_translation.y, m_translation.z );
  192. }
  193.  
  194. //-----------------------------------------------------------------------------
  195. // Adds the given translation to the object's current translation.
  196. //-----------------------------------------------------------------------------
  197. void SceneObject::AddTranslation( float x, float y, float z )
  198. {
  199.     m_translation.x += x;
  200.     m_translation.y += y;
  201.     m_translation.z += z;
  202.  
  203.     D3DXMatrixTranslation( &m_translationMatrix, m_translation.x, m_translation.y, m_translation.z );
  204. }
  205.  
  206. //-----------------------------------------------------------------------------
  207. // Adds the given translation to the object's current translation.
  208. //-----------------------------------------------------------------------------
  209. void SceneObject::AddTranslation( D3DXVECTOR3 translation )
  210. {
  211.     m_translation += translation;
  212.  
  213.     D3DXMatrixTranslation( &m_translationMatrix, m_translation.x, m_translation.y, m_translation.z );
  214. }
  215.  
  216. //-----------------------------------------------------------------------------
  217. // Returns the object's translation.
  218. //-----------------------------------------------------------------------------
  219. D3DXVECTOR3 SceneObject::GetTranslation()
  220. {
  221.     return m_translation;
  222. }
  223.  
  224. //-----------------------------------------------------------------------------
  225. // Sets the object's rotation.
  226. //-----------------------------------------------------------------------------
  227. void SceneObject::SetRotation( float x, float y, float z )
  228. {
  229.     m_rotation.x = x;
  230.     m_rotation.y = y;
  231.     m_rotation.z = z;
  232.  
  233.     D3DXMATRIX rotationX, rotationY;
  234.     D3DXMatrixRotationX( &rotationX, m_rotation.x );
  235.     D3DXMatrixRotationY( &rotationY, m_rotation.y );
  236.     D3DXMatrixRotationZ( &m_rotationMatrix, m_rotation.z );
  237.     D3DXMatrixMultiply( &m_rotationMatrix, &m_rotationMatrix, &rotationX );
  238.     D3DXMatrixMultiply( &m_rotationMatrix, &m_rotationMatrix, &rotationY );
  239. }
  240.  
  241. //-----------------------------------------------------------------------------
  242. // Sets the object's rotation.
  243. //-----------------------------------------------------------------------------
  244. void SceneObject::SetRotation( D3DXVECTOR3 rotation )
  245. {
  246.     m_rotation = rotation;
  247.  
  248.     D3DXMATRIX rotationX, rotationY;
  249.     D3DXMatrixRotationX( &rotationX, m_rotation.x );
  250.     D3DXMatrixRotationY( &rotationY, m_rotation.y );
  251.     D3DXMatrixRotationZ( &m_rotationMatrix, m_rotation.z );
  252.     D3DXMatrixMultiply( &m_rotationMatrix, &m_rotationMatrix, &rotationX );
  253.     D3DXMatrixMultiply( &m_rotationMatrix, &m_rotationMatrix, &rotationY );
  254. }
  255.  
  256. //-----------------------------------------------------------------------------
  257. // Adds the given rotation to the object's current rotation.
  258. //-----------------------------------------------------------------------------
  259. void SceneObject::AddRotation( float x, float y, float z )
  260. {
  261.     m_rotation.x += x;
  262.     m_rotation.y += y;
  263.     m_rotation.z += z;
  264.  
  265.     D3DXMATRIX rotationX, rotationY;
  266.     D3DXMatrixRotationX( &rotationX, m_rotation.x );
  267.     D3DXMatrixRotationY( &rotationY, m_rotation.y );
  268.     D3DXMatrixRotationZ( &m_rotationMatrix, m_rotation.z );
  269.     D3DXMatrixMultiply( &m_rotationMatrix, &m_rotationMatrix, &rotationX );
  270.     D3DXMatrixMultiply( &m_rotationMatrix, &m_rotationMatrix, &rotationY );
  271. }
  272.  
  273. //-----------------------------------------------------------------------------
  274. // Adds the given rotation to the object's current rotation.
  275. //-----------------------------------------------------------------------------
  276. void SceneObject::AddRotation( D3DXVECTOR3 rotation )
  277. {
  278.     m_rotation += rotation;
  279.  
  280.     D3DXMATRIX rotationX, rotationY;
  281.     D3DXMatrixRotationX( &rotationX, m_rotation.x );
  282.     D3DXMatrixRotationY( &rotationY, m_rotation.y );
  283.     D3DXMatrixRotationZ( &m_rotationMatrix, m_rotation.z );
  284.     D3DXMatrixMultiply( &m_rotationMatrix, &m_rotationMatrix, &rotationX );
  285.     D3DXMatrixMultiply( &m_rotationMatrix, &m_rotationMatrix, &rotationY );
  286. }
  287.  
  288. //-----------------------------------------------------------------------------
  289. // Returns the object's rotation.
  290. //-----------------------------------------------------------------------------
  291. D3DXVECTOR3 SceneObject::GetRotation()
  292. {
  293.     return m_rotation;
  294. }
  295.  
  296. //-----------------------------------------------------------------------------
  297. // Sets the object's velocity.
  298. //-----------------------------------------------------------------------------
  299. void SceneObject::SetVelocity( float x, float y, float z )
  300. {
  301.     m_velocity.x = x;
  302.     m_velocity.y = y;
  303.     m_velocity.z = z;
  304. }
  305.  
  306. //-----------------------------------------------------------------------------
  307. // Sets the object's velocity.
  308. //-----------------------------------------------------------------------------
  309. void SceneObject::SetVelocity( D3DXVECTOR3 velocity )
  310. {
  311.     m_velocity = velocity;
  312. }
  313.  
  314. //-----------------------------------------------------------------------------
  315. // Adds the given velocity to the object's current velocity.
  316. //-----------------------------------------------------------------------------
  317. void SceneObject::AddVelocity( float x, float y, float z )
  318. {
  319.     m_velocity.x += x;
  320.     m_velocity.y += y;
  321.     m_velocity.z += z;
  322. }
  323.  
  324. //-----------------------------------------------------------------------------
  325. // Adds the given velocity to the object's current velocity.
  326. //-----------------------------------------------------------------------------
  327. void SceneObject::AddVelocity( D3DXVECTOR3 velocity )
  328. {
  329.     m_velocity += velocity;
  330. }
  331.  
  332. //-----------------------------------------------------------------------------
  333. // Returns the object's velocity.
  334. //-----------------------------------------------------------------------------
  335. D3DXVECTOR3 SceneObject::GetVelocity()
  336. {
  337.     return m_velocity;
  338. }
  339.  
  340. //-----------------------------------------------------------------------------
  341. // Sets the object's spin.
  342. //-----------------------------------------------------------------------------
  343. void SceneObject::SetSpin( float x, float y, float z )
  344. {
  345.     m_spin.x = x;
  346.     m_spin.y = y;
  347.     m_spin.z = z;
  348. }
  349.  
  350. //-----------------------------------------------------------------------------
  351. // Sets the object's spin.
  352. //-----------------------------------------------------------------------------
  353. void SceneObject::SetSpin( D3DXVECTOR3 spin )
  354. {
  355.     m_spin = spin;
  356. }
  357.  
  358. //-----------------------------------------------------------------------------
  359. // Adds the given spin to the object's current spin.
  360. //-----------------------------------------------------------------------------
  361. void SceneObject::AddSpin( float x, float y, float z )
  362. {
  363.     m_spin.x += x;
  364.     m_spin.y += y;
  365.     m_spin.z += z;
  366. }
  367.  
  368. //-----------------------------------------------------------------------------
  369. // Adds the given spin to the object's current spin.
  370. //-----------------------------------------------------------------------------
  371. void SceneObject::AddSpin( D3DXVECTOR3 spin )
  372. {
  373.     m_spin = spin;
  374. }
  375.  
  376. //-----------------------------------------------------------------------------
  377. // Returns the object's spin.
  378. //-----------------------------------------------------------------------------
  379. D3DXVECTOR3 SceneObject::GetSpin()
  380. {
  381.     return m_spin;
  382. }
  383.  
  384. //-----------------------------------------------------------------------------
  385. // Returns the object's forward vector.
  386. //-----------------------------------------------------------------------------
  387. D3DXVECTOR3 SceneObject::GetForwardVector()
  388. {
  389.     return m_forward;
  390. }
  391.  
  392. //-----------------------------------------------------------------------------
  393. // Returns the object's right vector.
  394. //-----------------------------------------------------------------------------
  395. D3DXVECTOR3 SceneObject::GetRightVector()
  396. {
  397.     return m_right;
  398. }
  399.  
  400. //-----------------------------------------------------------------------------
  401. // Returns a pointer to the object's current translation matrix.
  402. //-----------------------------------------------------------------------------
  403. D3DXMATRIX *SceneObject::GetTranslationMatrix()
  404. {
  405.     return &m_translationMatrix;
  406. }
  407.  
  408. //-----------------------------------------------------------------------------
  409. // Returns a pointer to the object's current rotation matrix.
  410. //-----------------------------------------------------------------------------
  411. D3DXMATRIX *SceneObject::GetRotationMatrix()
  412. {
  413.     return &m_rotationMatrix;
  414. }
  415.  
  416. //-----------------------------------------------------------------------------
  417. // Returns a pointer to the object's current world matrix.
  418. //-----------------------------------------------------------------------------
  419. D3DXMATRIX *SceneObject::GetWorldMatrix()
  420. {
  421.     return &m_worldMatrix;
  422. }
  423.  
  424. //-----------------------------------------------------------------------------
  425. // Returns a pointer to the object's current view matrix.
  426. //-----------------------------------------------------------------------------
  427. D3DXMATRIX *SceneObject::GetViewMatrix()
  428. {
  429.     return &m_viewMatrix;
  430. }
  431.  
  432. //-----------------------------------------------------------------------------
  433. // Sets the object's type.
  434. //-----------------------------------------------------------------------------
  435. void SceneObject::SetType( unsigned long type )
  436. {
  437.     m_type = type;
  438. }
  439.  
  440. //-----------------------------------------------------------------------------
  441. // Returns the object's type.
  442. //-----------------------------------------------------------------------------
  443. unsigned long SceneObject::GetType()
  444. {
  445.     return m_type;
  446. }
  447.  
  448. //-----------------------------------------------------------------------------
  449. // Sets the object's friction.
  450. //-----------------------------------------------------------------------------
  451. void SceneObject::SetFriction( float friction )
  452. {
  453.     m_friction = friction;
  454. }
  455.  
  456. //-----------------------------------------------------------------------------
  457. // Returns the collision stamp.
  458. //-----------------------------------------------------------------------------
  459. unsigned long SceneObject::GetCollisionStamp()
  460. {
  461.     return m_collisionStamp;
  462. }
  463.  
  464. //-----------------------------------------------------------------------------
  465. // Sets the object's visible flag.
  466. //-----------------------------------------------------------------------------
  467. void SceneObject::SetVisible( bool visible )
  468. {
  469.     m_visible = visible;
  470. }
  471.  
  472. //-----------------------------------------------------------------------------
  473. // Returns the object's visible flag.
  474. //-----------------------------------------------------------------------------
  475. bool SceneObject::GetVisible()
  476. {
  477.     return m_visible;
  478. }
  479.  
  480. //-----------------------------------------------------------------------------
  481. // Sets the object's enabled flag.
  482. //-----------------------------------------------------------------------------
  483. void SceneObject::SetEnabled( bool enabled )
  484. {
  485.     m_enabled = enabled;
  486. }
  487.  
  488. //-----------------------------------------------------------------------------
  489. // Returns the object's enabled flag.
  490. //-----------------------------------------------------------------------------
  491. bool SceneObject::GetEnabled()
  492. {
  493.     return m_enabled;
  494. }
  495.  
  496. //-----------------------------------------------------------------------------
  497. // Sets the object's ghost flag.
  498. //-----------------------------------------------------------------------------
  499. void SceneObject::SetGhost( bool ghost )
  500. {
  501.     m_ghost = ghost;
  502. }
  503.  
  504. //-----------------------------------------------------------------------------
  505. // Returns the object's ghost flag.
  506. //-----------------------------------------------------------------------------
  507. bool SceneObject::GetGhost()
  508. {
  509.     return m_ghost;
  510. }
  511.  
  512. //-----------------------------------------------------------------------------
  513. // Sets the object's ignore collisions flag.
  514. //-----------------------------------------------------------------------------
  515. void SceneObject::SetIgnoreCollisions( bool ignoreCollisions )
  516. {
  517.     m_ignoreCollisions = ignoreCollisions;
  518. }
  519.  
  520. //-----------------------------------------------------------------------------
  521. // Returns the object's ignore collisions flag.
  522. //-----------------------------------------------------------------------------
  523. bool SceneObject::GetIgnoreCollisions()
  524. {
  525.     return m_ignoreCollisions;
  526. }
  527.  
  528. //-----------------------------------------------------------------------------
  529. // Set's the the flag for touching the ground.
  530. //-----------------------------------------------------------------------------
  531. void SceneObject::SetTouchingGroundFlag( bool touchingGround )
  532. {
  533.     m_touchingGround = touchingGround;
  534. }
  535.  
  536. //-----------------------------------------------------------------------------
  537. // Returns true if the object is touching the ground.
  538. //-----------------------------------------------------------------------------
  539. bool SceneObject::IsTouchingGround()
  540. {
  541.     return m_touchingGround;
  542. }
  543.  
  544. //-----------------------------------------------------------------------------
  545. // Sets the mesh for this scene object.
  546. //-----------------------------------------------------------------------------
  547. void SceneObject::SetMesh( char *meshName, char *meshPath, bool sharedMesh )
  548. {
  549.     // Destroy the object's exisiting mesh.
  550.     if( m_sharedMesh == true )
  551.         g_engine->GetMeshManager()->Remove( &m_mesh );
  552.     else
  553.         SAFE_DELETE( m_mesh );
  554.  
  555.     // Indicate if the object is sharing this mesh.
  556.     m_sharedMesh = sharedMesh;
  557.  
  558.     // Ensure a mesh was specified.
  559.     if( meshName != NULL && meshPath != NULL )
  560.     {
  561.         // Load the object's mesh.
  562.         if( m_sharedMesh == true )
  563.             m_mesh = g_engine->GetMeshManager()->Add( meshName, meshPath );
  564.         else
  565.             m_mesh = new Mesh( meshName, meshPath );
  566.  
  567.         // Clone the mesh's bounding volume. The bounding volume will be used
  568.         // to maintain an axis aligned bounding volume in world space.
  569.         CloneBoundingVolume( m_mesh->GetBoundingBox(), m_mesh->GetBoundingSphere() );
  570.     }
  571. }
  572.  
  573. //-----------------------------------------------------------------------------
  574. // Returns a pointer to the object's mesh.
  575. //-----------------------------------------------------------------------------
  576. Mesh *SceneObject::GetMesh()
  577. {
  578.     return m_mesh;
  579. }