Results 1 to 2 of 2

Thread: modeling gravity

  1. #1
    Member Newbie
    Join Date
    Jun 2014
    Posts
    38

    modeling gravity

    i'm trying to model a ball being dropped from 200 meters at an acceleration of -9.8m/s^2. This is my ball:

    Code :
    void generate_vertices()
    {
      for(int i=0; i < NUM_VERTICES; i++)
      {
        GLfloat angle = 2*M_PI/NUM_VERTICES * i;
     
        GLfloat x = 10 * cos(angle);
        GLfloat y = 10 * sin(angle);
     
        vertices.push_back(x);
        vertices.push_back(y);
      }  
    }


    Here's my draw() function:
    Code :
    void draw()
    {
      static GLfloat speed = 0;
      GLfloat now = glfwGetTime();
     
      now *= -9.8;
     
      speed += now;
     
      glm::mat4 model;
      glm::mat4 view;
      glm::mat4 projection;
      view = glm::translate(view, glm::vec3(100.0f, 200.0f, 0.0f));
      view = glm::translate(view, glm::vec3(0.0f, speed, 0.0f));
     
      glClear(GL_COLOR_BUFFER_BIT);
     
      // view = glm::translate(view, glm::vec3(0.0f, 1.0f, 0.0f));
      // projection = glm::perspective(45.0f, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f);
      projection = glm::ortho(0.0f, 200.0f, 0.0f, 200.0f, -1.0f, 1.0f);
      // Get their uniform location
      GLint modelLoc = glGetUniformLocation(program, "model");
      GLint viewLoc = glGetUniformLocation(program, "view");
      GLint projLoc = glGetUniformLocation(program, "projection"); 
      // Pass them to the shaders
      glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
      glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
      // Note: currently we set the projection matrix each frame, but since the projection matrix rarely changes it's often best practice to set it outside the main loop only once.
      glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
     
      glBindVertexArray(vao);
      glDrawArrays(GL_LINE_LOOP, 0, NUM_VERTICES);
      glBindVertexArray(0);
    }

    But this makes the ball drop way too fast. When I change the code to the following it seems to drop at a more realistic rate. So obviously I'm coding something wrong.:

    Code :
    void draw()
    {
      static GLfloat speed = 0;
      GLfloat now = glfwGetTime();
     
      speed -= now;
     ...

  2. #2
    Member Newbie
    Join Date
    Jun 2014
    Posts
    38

    Answering my own question.

    Quote Originally Posted by michaelglaz View Post
    i'm trying to model a ball being dropped from 200 meters at an acceleration of -9.8m/s^2. This is my ball:

    Code :
    void generate_vertices()
    {
      for(int i=0; i < NUM_VERTICES; i++)
      {
        GLfloat angle = 2*M_PI/NUM_VERTICES * i;
     
        GLfloat x = 10 * cos(angle);
        GLfloat y = 10 * sin(angle);
     
        vertices.push_back(x);
        vertices.push_back(y);
      }  
    }


    Here's my draw() function:
    Code :
    void draw()
    {
      static GLfloat speed = 0;
      GLfloat now = glfwGetTime();
     
      now *= -9.8;
     
      speed += now;
     
      glm::mat4 model;
      glm::mat4 view;
      glm::mat4 projection;
      view = glm::translate(view, glm::vec3(100.0f, 200.0f, 0.0f));
      view = glm::translate(view, glm::vec3(0.0f, speed, 0.0f));
     
      glClear(GL_COLOR_BUFFER_BIT);
     
      // view = glm::translate(view, glm::vec3(0.0f, 1.0f, 0.0f));
      // projection = glm::perspective(45.0f, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f);
      projection = glm::ortho(0.0f, 200.0f, 0.0f, 200.0f, -1.0f, 1.0f);
      // Get their uniform location
      GLint modelLoc = glGetUniformLocation(program, "model");
      GLint viewLoc = glGetUniformLocation(program, "view");
      GLint projLoc = glGetUniformLocation(program, "projection"); 
      // Pass them to the shaders
      glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
      glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
      // Note: currently we set the projection matrix each frame, but since the projection matrix rarely changes it's often best practice to set it outside the main loop only once.
      glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
     
      glBindVertexArray(vao);
      glDrawArrays(GL_LINE_LOOP, 0, NUM_VERTICES);
      glBindVertexArray(0);
    }

    But this makes the ball drop way too fast. When I change the code to the following it seems to drop at a more realistic rate. So obviously I'm coding something wrong.:

    Code :
    void draw()
    {
      static GLfloat speed = 0;
      GLfloat now = glfwGetTime();
     
      speed -= now;
     ...
    Figured it out with the help of my friend who has a degree in physics.

    Code :
      static GLfloat last = 0;
      GLfloat delta;
      GLfloat now = glfwGetTime();
     
      delta = now - last;
     
      velocity += -9.8*delta;
     
      distance +=  velocity*delta;  
     
      last = now;

Similar Threads

  1. Falling fruit using gravity
    By Muzna Khan in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 10-19-2013, 06:03 AM
  2. Gravity
    By UnIcron in forum OpenGL: Basic Coding
    Replies: 10
    Last Post: 02-17-2003, 04:55 AM
  3. gravity effecting y velocity
    By Josh Kasten in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 02-07-2003, 09:56 PM
  4. opengl and gravity
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 01-28-2003, 07:43 AM
  5. Gravity
    By vos in forum OpenGL: Basic Coding
    Replies: 10
    Last Post: 06-10-2002, 10:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean