PDA

View Full Version : modeling gravity



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



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:


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.:



void draw()
{
static GLfloat speed = 0;
GLfloat now = glfwGetTime();

speed -= now;
...

michaelglaz
04-09-2015, 03:32 PM
i'm trying to model a ball being dropped from 200 meters at an acceleration of -9.8m/s^2. This is my ball:



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:


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.:



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.



static GLfloat last = 0;
GLfloat delta;
GLfloat now = glfwGetTime();

delta = now - last;

velocity += -9.8*delta;

distance += velocity*delta;

last = now;