Advertisement
Guest User

Untitled

a guest
May 17th, 2024
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. #ifndef RIGIDBODY_CLASS_H
  2. #define RIGIDBODY_CLASS_H
  3.  
  4. #include <glm/glm.hpp>
  5. #include <glm/gtc/matrix_transform.hpp>
  6. #include <glm/gtx/rotate_vector.hpp>
  7. #include "model.h"
  8.  
  9. class Rigidbody {
  10. private:
  11. glm::vec3 position;
  12. glm::vec3 velocity;
  13. glm::vec3 acceleration;
  14. glm::vec3 force;
  15. float mass;
  16. glm::vec3 gravity;
  17. glm::vec3 rotation;
  18.  
  19. // Bounding box dimensions
  20. glm::vec3 boundingBoxMin;
  21. glm::vec3 boundingBoxMax;
  22. glm::vec3 colliderRotation;
  23.  
  24. float radius;
  25.  
  26. public:
  27. Rigidbody(glm::vec3 initialPosition, float initialMass, glm::vec3 initialGravity,
  28. Model& model, glm::vec3 initialRotation)
  29. : position(initialPosition), mass(initialMass), velocity(glm::vec3(0.0f)),
  30. acceleration(glm::vec3(0.0f)), force(glm::vec3(0.0f)), gravity(initialGravity),
  31. rotation(initialRotation), colliderType(ColliderType::BoundingBox) {
  32. // Get bounding box from the model
  33. boundingBoxMax = model.GetMaxBoundingBox();
  34. boundingBoxMin = model.GetMinBoundingBox();
  35. }
  36.  
  37. // Constructor for spherical collider
  38. Rigidbody(glm::vec3 initialPosition, float initialMass, glm::vec3 initialGravity,
  39. float initialRadius)
  40. : position(initialPosition), mass(initialMass), velocity(glm::vec3(0.0f)),
  41. acceleration(glm::vec3(0.0f)), force(glm::vec3(0.0f)), gravity(initialGravity),
  42. rotation(glm::vec3(0.0f)), radius(initialRadius), colliderType(ColliderType::Sphere) {}
  43.  
  44. void applyForce(glm::vec3 externalForce) {
  45. force += externalForce;
  46. }
  47.  
  48. void update(float deltaTime) {
  49. // Apply gravity
  50. force += mass * gravity;
  51.  
  52. // Apply Newton's second law: F = ma
  53. acceleration = force / mass;
  54.  
  55. // Update velocity
  56. velocity += acceleration * deltaTime;
  57.  
  58. // Update position
  59. position += velocity * deltaTime;
  60.  
  61. // Reset force for next update
  62. force = glm::vec3(0.0f);
  63.  
  64. }
  65.  
  66. glm::vec3 getPosition() const {
  67. return position;
  68. }
  69.  
  70. glm::vec3 getVelocity() const {
  71. return velocity;
  72. }
  73.  
  74. void setPosition(glm::vec3 newPosition) {
  75. position = newPosition;
  76. }
  77.  
  78. void setVelocity(glm::vec3 newVelocity) {
  79. velocity = newVelocity;
  80. }
  81.  
  82. float getMass() const {
  83. return mass;
  84. }
  85.  
  86. void setMass(float newMass) {
  87. mass = newMass;
  88. }
  89.  
  90. glm::vec3 getGravity() const {
  91. return gravity;
  92. }
  93.  
  94. void setGravity(glm::vec3 newGravity) {
  95. gravity = newGravity;
  96. }
  97.  
  98. glm::vec3 getRotation() const {
  99. return rotation;
  100. }
  101.  
  102. void setRotation(glm::vec3 newRotation) {
  103. rotation = newRotation;
  104. }
  105.  
  106. void setColliderRotation(const glm::vec3& newRotation) {
  107. colliderRotation = newRotation;
  108. }
  109.  
  110. float getRadius() {
  111. return radius;
  112. }
  113.  
  114. enum class ColliderType {
  115. BoundingBox,
  116. Sphere
  117. } colliderType;
  118.  
  119. // Methods to get the bounding box dimensions
  120. glm::vec3 getBoundingBoxMin() const {
  121. // Apply rotation to the min bounding box coordinates
  122. glm::vec3 rotatedMin = boundingBoxMin;
  123. rotatedMin = glm::rotateX(rotatedMin, colliderRotation.x);
  124. rotatedMin = glm::rotateY(rotatedMin, colliderRotation.y);
  125. rotatedMin = glm::rotateZ(rotatedMin, colliderRotation.z);
  126. return rotatedMin;
  127. }
  128.  
  129. glm::vec3 getBoundingBoxMax() const {
  130. // Apply rotation to the max bounding box coordinates
  131. glm::vec3 rotatedMax = boundingBoxMax;
  132. rotatedMax = glm::rotateX(rotatedMax, colliderRotation.x);
  133. rotatedMax = glm::rotateY(rotatedMax, colliderRotation.y);
  134. rotatedMax = glm::rotateZ(rotatedMax, colliderRotation.z);
  135. return rotatedMax;
  136. }
  137.  
  138. // Bounding box collision detection
  139. bool checkCollision(const Rigidbody& other) const {
  140. // Get the bounding box of the other Rigidbody
  141. glm::vec3 minB = other.getPosition() + other.getBoundingBoxMin();
  142. glm::vec3 maxB = other.getPosition() + other.getBoundingBoxMax();
  143.  
  144. // Check if this bounding box intersects with the other bounding box
  145. return (position.x + boundingBoxMax.x >= minB.x &&
  146. position.x + boundingBoxMin.x <= maxB.x &&
  147. position.y + boundingBoxMax.y >= minB.y &&
  148. position.y + boundingBoxMin.y <= maxB.y &&
  149. position.z + boundingBoxMax.z >= minB.z &&
  150. position.z + boundingBoxMin.z <= maxB.z);
  151. }
  152. };
  153.  
  154. #endif
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement