Guest User

Untitled

a guest
May 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.33 KB | None | 0 0
  1.  
  2.  
  3. #include"Physics.h"
  4. #include"commonFile.h"
  5.  
  6.  
  7.  
  8. Ground::Ground(): Planes()
  9. {
  10. point=Vector3D(0.0f,0.0f,0.0f);
  11. normal=Vector3D(0.0f,1.0f,0.0f);
  12. }
  13.  
  14. Physics::Physics()
  15. {
  16.  
  17. }
  18.  
  19. Physics::~Physics()
  20. {
  21.  
  22. }
  23.  
  24.  
  25. void Physics::Update(float dt)
  26. {
  27.  
  28. // compute speed
  29. speed = fabs(linearVelocity.Norm());
  30. // compute the drag force
  31. dragForce = -0.5f*rho*speed*dragCoefficient*area*linearVelocity;
  32. // update the ball position using Euler method
  33. position = position+linearVelocity*dt;
  34.  
  35. if(position.y < radius/*If the Y-component of the ball position <= radius */ )
  36. {
  37. //clamp it to the value of the radius
  38. position.y = radius;
  39. }
  40.  
  41. // make the object move (translate) in real-time using D3DXMatrixTranslation()
  42. D3DXMatrixTranslation(&WorldMatrix, position.x, position.y, position.z);
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49. void Physics::ApplyFriction(float dt)
  50. {
  51. Vector3D staticFriction;// static friction
  52. Vector3D kineticFriction;// kinetic friction
  53. Vector3D tangentForce; // tangential component of the applied force
  54. Vector3D normal; // normal vector of the ground
  55. Vector3D tangent; // vector in the direction of the tangential component of the applied force
  56. Ground ground;
  57. Vector3D d;// unit vector of the applied force
  58.  
  59. // normal to ground normal vector (0,1,0)
  60. normal=ground.normal;
  61.  
  62.  
  63. if(linearVelocity.Norm() == 0/*1/linearVelocity norm is 0.*/ )
  64. {
  65. //--- stable static state ----
  66.  
  67. // compute static friction
  68. staticFriction = -StaticFrictionCoefficient*normalForce.Norm()*normal;
  69. // set friction force equal to static friction
  70. frictionForce = staticFriction;
  71. // set linear velocity to (0,0,0)
  72. linearVelocity = Vector3D( 0.0f, 0.0f, 0.0f);
  73. // set linear acceleration to (0,0,0)
  74. linearAcceleration= Vector3D( 0.0f, 0.0f, 0.0f);
  75.  
  76. // compute the total force applied to the ball (we realy do not need that since a and v are zero);
  77. // DO NOT ADD the static friction force since it should not cause motion
  78. totalForces= weight + normalForce;
  79.  
  80. }
  81.  
  82.  
  83. if(linearVelocity.Norm() > 0 && force.Norm() >= staticFriction.Norm()/*1/linearVelocity norm>0 AND force norm >= staticFriction Norm*/ )
  84. {
  85. //---- this is the dynamic state---
  86.  
  87. // Compute Kinetic(Dynamic) friction , see note for the formula
  88. kineticFriction = -DynamicFrictionCoefficient*normalForce.Norm()*linearVelocity.Normalize();
  89. // set the frictionForce equal to the previously computed kineticFriction
  90. frictionForce = kineticFriction;
  91. // compute the total force , included friction and drag force , on the ball
  92. totalForces = force + weight + normalForce + frictionForce + dragForce;
  93. // compute the linear acceleration of the ball
  94. linearAcceleration = totalForces/mass;
  95. // compute the linear velocity of the ball
  96. linearVelocity = linearVelocity + linearAcceleration*dt;
  97. }
  98.  
  99. if(linearVelocity.Norm() == 0 && force.Norm() >= staticFriction.Norm()/*1/linear Velocity norm=0 AND force norm()>static Friction norm*/ )
  100. {
  101. //--- this is the unstable static state---
  102.  
  103. // compute static friction in the opposite direction of the ground normal vector.
  104. staticFriction = -StaticFrictionCoefficient*normalForce.Norm()*force.Normalize();
  105. float slidingFactor;// a sliding factor to make the transition from static to dynamic friction state.
  106.  
  107. // compute the sliding factor . see note
  108. slidingFactor = force.Norm() / staticFriction.Norm();
  109. // compute a small initial velocity to break the transition from static friction to dynamic friction
  110. // in the direction of the applied force normalized vector.
  111. linearVelocity = slidingFactor * force.Normalize();
  112.  
  113. // now,the ball is moving with a non-zero velocity.Kinetic friction is being applied.
  114. // Compute it(see note on Kinetic friction)
  115. kineticFriction = -DynamicFrictionCoefficient*normalForce.Norm()*linearVelocity.Normalize();
  116. // Set the friction force(frictionForce) equal to the kinetic friction(kineticFriction) as computed above
  117. frictionForce = kineticFriction;
  118. // compute the total forces on the ball : weight , drag , friction force ,normal and applied force(force)
  119. totalForces = force + weight + normalForce + frictionForce + dragForce;
  120. // compute the linear acceleration
  121. linearAcceleration = totalForces/mass;
  122. // compute the updated linear velocity
  123. linearVelocity = linearVelocity + linearAcceleration*dt;
  124. }
  125. // now clamp the velocity by its components to zero by doing the followings:
  126.  
  127. // if the X component of velocity is 0.1(close to 0) , set it to 0
  128. if( fabs(linearVelocity.x) <= 0.1f)
  129. linearVelocity.x = 0.0f;
  130.  
  131. // if the Y component of velocity is 0.1 (close to 0), set it to 0
  132. if (fabs(linearVelocity.y) <= 0.1f)
  133. linearVelocity.y = 0.0f;
  134. // if the Z component of velocity is 0.1(close to 0) , set it to 0
  135. if (fabs(linearVelocity.z) <= 0.1f)
  136. linearVelocity.z = 0.0f;
  137.  
  138.  
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145. COLLISION_STATE CheckSpherePlaneCollision(RigidBody& body1,Ground& ground)
  146. {
  147. COLLISION_STATE HasCollided=NO_COLLISION;
  148.  
  149. float Vrn; // relative speed in the collision normal direction
  150. float d; // d of plane equation n*p+d=0 where d=-Plane_normal*point_on_plane
  151. float R;// to represent radius
  152. Vector3D n=ground.normal;
  153. Vector3D P=ground.point;
  154. Vector3D C= body1.position;
  155. // compute d of the plane
  156. d=-n*P;
  157. // Set R to body1 radius
  158. R=body1.radius;
  159.  
  160. // compute body1 normal velocity
  161. body1.normalVelocity = (body1.linearVelocity*n)*n.Normalize();
  162. // compute body1 tangential velocity
  163. body1.tangentialVelocity = body1.linearVelocity - body1.normalVelocity;
  164. // compute the relative speed in the direction of the collision normal n that is Vrn=normalVelocity*n .
  165. Vrn=body1.normalVelocity*n;
  166.  
  167.  
  168. if(body1.normalVelocity.Norm() < 0.1f/*1/if body1 normal velocity is less than 0.1(close to zero)*/)
  169. {
  170. // then set the normal velocity to zero vector
  171. body1.normalVelocity = Vector3D( 0.0f, 0.0f, 0.0f);
  172. //and Vrn to zero.
  173. Vrn = 0;
  174. }
  175.  
  176.  
  177. if(Vrn < 0 && fabs(n*C+d) <= R/*1/Vrn<0 and |n*C+d |<=R */)
  178. {
  179. //set HasCollided equal to COLLISION;
  180. HasCollided = COLLISION;
  181. }
  182. else
  183.  
  184. if(Vrn == 0 && fabs(n*C+d) == R/*1/if Vrn =0 and |n*C+d|=R*/ )
  185. {
  186. //set HasCollided equal to RESTING_CONTACT;
  187. HasCollided = RESTING_CONTACT;
  188. }
  189.  
  190. else
  191.  
  192. if(Vrn > 0 || fabs(n*C+d) == R/*1/if Vrn>0 OR |n*C+d|>R*/ )
  193. {
  194. //set HasCollided equal to NO_COLLISION;
  195. HasCollided = NO_COLLISION;
  196. }
  197.  
  198. return HasCollided;
  199. }
  200.  
  201.  
  202. void HandleSpherePlaneCollision(RigidBody& body1 ,Ground& ground)
  203. {
  204. float e; // coefficient of Restitution
  205. // Set e to body1 coefficient Of Restitution
  206. e = body1.coefficientOfRestitution;
  207. // compute body1 linear velocity after contact ; see note
  208. body1.linearVelocity = -e*body1.normalVelocity + body1.tangentialVelocity;
  209.  
  210. }
  211.  
  212.  
  213. void HandleRestingContact(RigidBody& body1 ,Ground& ground,float dt)
  214. {
  215.  
  216.  
  217. if(body1.position.y == body1.radius/*1/ if body1 position Y-component equal to its radius*/)
  218. {
  219. // then apply friction ( applyFriction(dt) ) to body1.
  220. body1.ApplyFriction(dt);
  221. }
  222. }
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229. ///////////////////////////////////////////////////////////////////////////////////////
  230.  
  231.  
  232. LPD3DXBUFFER D3DXMtrlBuffer=NULL;
  233.  
  234.  
  235.  
  236. Mesh::MeshData::MeshData()
  237. {
  238. meshTextures=NULL;
  239. meshMaterials=NULL;
  240. numberOfMaterials=0;
  241. d3dxMaterials=NULL;
  242. mesh=NULL;
  243. referenceCount=0;
  244. }
  245.  
  246. Mesh::MeshData::~MeshData()
  247. {
  248. if(meshMaterials!=NULL)
  249. {
  250. delete [] meshMaterials ;
  251. }
  252.  
  253.  
  254. if(meshTextures!=NULL)
  255. {
  256.  
  257. for (int i=0; i<numberOfMaterials;i++)
  258. {
  259. if(meshTextures[i]!=NULL)
  260. {
  261. meshTextures[i]->Release();
  262. }
  263. }
  264. delete [] meshTextures;
  265. }
  266. }
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273. Mesh::Mesh()
  274. {
  275. meshData=new MeshData();
  276. }
  277.  
  278.  
  279.  
  280. Mesh::~Mesh()
  281. {
  282. meshData->referenceCount--;
  283. if(meshData->referenceCount==0)
  284. {
  285. meshData->mesh->Release();
  286. delete meshData;
  287. }
  288.  
  289. }
  290.  
  291.  
  292.  
  293. Mesh &Mesh::operator =(Mesh& sourceMesh)
  294. {
  295.  
  296. if(meshData!=sourceMesh.meshData)
  297. {
  298. meshData->referenceCount--;
  299.  
  300. if(meshData->referenceCount==0)
  301. {
  302. delete meshData;
  303. }
  304. meshData=sourceMesh.meshData;
  305. sourceMesh.meshData->referenceCount++;
  306. }
  307. return *this;
  308. }
  309.  
  310.  
  311.  
  312.  
  313. bool Mesh::LoadMesh(std::string FileName,LPDIRECT3DDEVICE9 theDevice)
  314. {
  315.  
  316. LPD3DXBUFFER D3DXMtrlBuffer;
  317. HRESULT hr= NULL;
  318. bool IsLoaded=false;
  319.  
  320. hr=D3DXLoadMeshFromX(FileName.c_str(),
  321. D3DXMESH_SYSTEMMEM,
  322. theDevice,
  323. NULL,
  324. &D3DXMtrlBuffer,
  325. NULL,
  326. (DWORD*)&meshData->numberOfMaterials,
  327. &meshData->mesh);
  328.  
  329.  
  330. if(hr==D3D_OK)
  331. {
  332. meshData->d3dxMaterials=(D3DXMATERIAL*)D3DXMtrlBuffer->GetBufferPointer();
  333. IsLoaded=true;
  334. }
  335. else
  336. {
  337. // MessageBox(0," Cannot find file to load mesh..... ",0,MB_OK);
  338.  
  339. }
  340.  
  341.  
  342. if(IsLoaded==true)
  343. {
  344. // load the mesh material
  345.  
  346. meshData->meshMaterials=new D3DMATERIAL9[meshData->numberOfMaterials];
  347.  
  348. if(meshData->meshMaterials==NULL)
  349. {
  350.  
  351. IsLoaded=false;
  352. }
  353. }
  354.  
  355.  
  356. if( IsLoaded==true)
  357. {
  358. // load mesh texture
  359. meshData->meshTextures=new LPDIRECT3DTEXTURE9 [meshData->numberOfMaterials];
  360.  
  361. if(meshData->meshTextures==NULL)
  362. {
  363. delete [] meshData->meshMaterials;
  364. IsLoaded=false;
  365. }
  366. }
  367.  
  368.  
  369. if(IsLoaded==true)
  370. {
  371.  
  372. for(int i=0;i<meshData->numberOfMaterials;i++)
  373. {
  374.  
  375.  
  376. meshData->meshMaterials[i]=meshData->d3dxMaterials[i].MatD3D;
  377. meshData->meshMaterials[i].Ambient=meshData->meshMaterials[i].Diffuse;
  378.  
  379.  
  380. if( meshData->d3dxMaterials[i].pTextureFilename!=NULL && (lstrlen(meshData->d3dxMaterials[i].pTextureFilename)>0))
  381. {
  382. if(FAILED(D3DXCreateTextureFromFile(theDevice,meshData->d3dxMaterials[i].pTextureFilename,&meshData->meshTextures[i])))
  383. {
  384. IsLoaded=false;
  385. // MessageBox(0,"Model Texture upload failed"," ",MB_OK);
  386. }
  387. }
  388.  
  389. else
  390.  
  391. {
  392. //MessageBox(0,"Cannot find texture file"," ",MB_OK);
  393. meshData->meshTextures[i]=NULL;
  394.  
  395. }
  396. }
  397.  
  398. D3DXMtrlBuffer->Release();
  399. }
  400.  
  401. return IsLoaded;
  402. }
  403.  
  404.  
  405.  
  406.  
  407. bool Mesh::RenderMesh(LPDIRECT3DDEVICE9 theDevice)
  408. {
  409.  
  410. bool IsRendered=true;
  411.  
  412. /* Meshes are divided into subsets, one for each material. Each
  413. mesh subset needs to be renderes individually. */
  414.  
  415. for(DWORD i=0;i<(DWORD)meshData->numberOfMaterials;i++ )
  416. {
  417.  
  418. // Set the material for this subset
  419. if ( FAILED(theDevice->SetMaterial(&meshData->meshMaterials[i])))
  420. {
  421. MessageBox(0,"SetMaterial()-Failed"," ",MB_OK);
  422. IsRendered=false;
  423. }
  424.  
  425. // Set the texture for this subset
  426. if (FAILED(theDevice->SetTexture(0,meshData->meshTextures[i])))
  427. {
  428. MessageBox(0,"SetTexture()-Failed"," ",MB_OK);
  429. IsRendered=false;
  430.  
  431. }
  432.  
  433. // Draw the mesh subset
  434. meshData->mesh->DrawSubset(i);
  435. }
  436.  
  437. return (IsRendered);
  438. }
  439.  
  440.  
  441. //////////////////////////////////////////////// class RigidBody implementation/////////////////////////////////////////////////
  442.  
  443.  
  444. RigidBody::RigidBody() :Physics()
  445. {
  446. modelMesh=new Mesh();
  447. }
  448.  
  449.  
  450.  
  451. RigidBody::~RigidBody()
  452. {
  453. delete modelMesh;
  454. }
  455.  
  456.  
  457.  
  458. bool RigidBody::Load(std::string ModelMesh_Filename,LPDIRECT3DDEVICE9 theDevice)
  459. {
  460.  
  461. bool IsLoaded=true;
  462.  
  463. if(ModelMesh_Filename.length()>0)
  464. {
  465. modelMesh->LoadMesh(ModelMesh_Filename,theDevice);
  466. }
  467. else
  468. {
  469. IsLoaded=false;
  470. //MessageBox(0,"No file was loaded",0,MB_OK);
  471. }
  472.  
  473. return IsLoaded;
  474. }
  475.  
  476.  
  477.  
  478. bool RigidBody::Render(LPDIRECT3DDEVICE9 theDevice)
  479. {
  480. D3DXMATRIX ModelLocalMatrix;
  481.  
  482. theDevice->GetTransform(D3DTS_WORLD,&ModelLocalMatrix);
  483. theDevice->SetTransform(D3DTS_WORLD,&WorldMatrix);
  484. modelMesh->RenderMesh(theDevice);
  485. theDevice->SetTransform(D3DTS_WORLD,&ModelLocalMatrix);
  486.  
  487. return true;
  488. }
  489.  
  490. //////////////////////////////////////////////////////class ground implementation\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  491.  
  492. // Ground::Ground() : Planes(){ }
  493. Ground::~Ground() { }
Add Comment
Please, Sign In to add comment