Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //my listener for gravity physics
- class GravityListener : public VEEventListener {
- VESceneNode* m_pObject = nullptr;
- VESceneNode* m_pPlane = nullptr;
- glm::vec3 f = glm::vec3(0, 0, 0);
- glm::vec3 v = glm::vec3(0, 0, 0);
- glm::vec3 w = glm::vec3(0, 0, 0);
- glm::vec3 l = glm::vec3(0, 0, 0);
- float m = 1;
- float width = 1;
- float height = 1;
- float depth = 1;
- glm::mat3 i = glm::mat3(
- glm::vec3(1.0f / 12.0f * m * (height * height + depth * depth), 0, 0),
- glm::vec3(0, 1.0f / 12.0f * m * (width * width + depth * depth), 0),
- glm::vec3(0, 0, 1.0f / 12.f * m * (width * width + height * height))
- );
- glm::vec3 m_pVertices[8] = {
- glm::vec3(0.500000, -0.500000, 0.500000),
- glm::vec3(0.500000, -0.500000, 0.500000),
- glm::vec3(-0.500000, 0.500000, 0.500000),
- glm::vec3(0.500000, 0.500000, 0.500000),
- glm::vec3(-0.500000, 0.500000, -0.500000),
- glm::vec3(0.500000, 0.500000, -0.500000),
- glm::vec3(-0.500000, -0.500000, -0.500000),
- glm::vec3(0.500000, -0.500000, -0.500000)
- };
- public:
- ///Constructor
- GravityListener(std::string name, VESceneNode* pObject, VESceneNode* pPlane) :
- VEEventListener(name), m_pObject(pObject), m_pPlane(pPlane) {
- glm::vec3 ax = glm::vec3(0, 0, 1);
- float angle = (float)rand();
- glm::mat3 rotation_matrix = glm::rotate(angle, ax);
- glm::vec3 origin = m_pObject->getPosition();
- m_pObject->setPosition(glm::vec3());
- m_pObject->multiplyTransform(rotation_matrix);
- m_pObject->setPosition(origin);
- for (glm::vec3& vertex : m_pVertices) {
- vertex = vertex * rotation_matrix;
- }
- };
- void onFrameStarted(veEvent event) {
- glm::vec3 a = f / m;
- v = v + event.dt * a;
- m_pObject->setPosition(glm::vec3(
- m_pObject->getPosition().x + event.dt * v.x,
- m_pObject->getPosition().y + event.dt * v.y,
- m_pObject->getPosition().z + event.dt * v.z)
- );
- glm::vec3 t = glm::cross(f, l);
- glm::vec3 angular_acceleration = glm::vec3();
- if (t.x != 0 || t.y != 0 || t.z != 0) {
- angular_acceleration = glm::inverse(i) * t;
- }
- w = w + event.dt * angular_acceleration;
- if (glm::length(w) != 0) {
- glm::mat3 rotation_matrix = glm::rotate(glm::length(w), glm::normalize(w));
- glm::vec3 origin = m_pObject->getPosition();
- m_pObject->setPosition(glm::vec3());
- m_pObject->multiplyTransform(rotation_matrix);
- m_pObject->setPosition(origin);
- for (glm::vec3& vertex : m_pVertices) {
- vertex = vertex * rotation_matrix;
- }
- }
- //collision detection and response
- for (glm::vec3& vertex : m_pVertices) {
- if (m_pObject->getPosition().y + vertex.y < 0) {
- m_pObject->setPosition(glm::vec3(
- m_pObject->getPosition().x,
- -vertex.y,
- m_pObject->getPosition().z
- ));
- glm::vec3 n = glm::vec3(0, 1, 0);
- float rest_coeff = 0.5;
- //velocity at collision
- glm::vec3 vc = v + glm::cross(w, vertex);
- //this works for velocity
- glm::vec3 j = -(1 + rest_coeff) * vc * n / (1 / m);
- //this should work for both theoretically
- //glm::vec3 j = (-(1 + rest_coeff) * va * n) / (1 / m + n * glm::cross(glm::inverse(i) * glm::cross(vertex, n), vertex));
- v = v + (j * n) / m;
- //w = w + glm::inverse(i) * glm::cross(vertex, j * n);
- }
- }
- }
- virtual bool onKeyboard(veEvent event) {
- if (event.idata3 == GLFW_RELEASE) {
- return false;
- }
- if (event.idata1 == GLFW_KEY_SPACE) {
- // 'activates' gravity
- f = glm::vec3(0, -9.81 * m, 0);
- return false;
- }
- return false;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement