Reputation: 2684
int i = 0;
void Car_Animation::update(float delta_time)
{
glm::tvec3<float> tar = { 0,0,0 };
tar = curve->curve_points_pos[(i+1) % (curve->points_pos.size())] -
curve->curve_points_pos[(i) % (curve->points_pos.size())];
m_model_mat = glm::translate(m_model_mat, tar);
i++;
}
The above function gets called in a while loop where delta_time is the difference between the previous frame and current frame;
while (!glfwWindowShouldClose(window))
{
animation->update(delta_time);
}
Right now, in the update
function, I am translating my model to a set of points in the curve->points_pos
. curve->points_pos
contains an array of points close to each other which gives an illusion of the model moving continuously.But the problem is, I want to modulate the speed of the model. How could I do it? I have delta_time
. Can I use that? I am using only glfw, glew and glm
, not using glut
.
Upvotes: 1
Views: 201
Reputation: 100632
Correct me if I'm wrong, but it looks like you currently have:
* curve->curve_points_pos
, which is an array of locations that you want your model to pass through;
* m_model_mat
which is the current transformation matrix for your model; and
* i
, which is the current index into curve->curve_points_pos
.
The model then takes integral steps from one entry in curve->curve_points_pos
to the next, although you apply them as offsets from each other, rather than taking them as absolutes.
In that case what you basically have is a function of time, f(t)
evaluated at integral steps of t
and stored in curve->curve_points_pos
.
So, first: stop thinking in terms of updating m_model_mat
, and calculate it entirely per position. E.g.
m_model_mat = glm::translate(glm::mat4(1.f),
curve->curve_points_pos[(i) % (curve->points_pos.size())]);
If you have some other transform you also want to apply — e.g. if you want to move it away from the origin, compose that as an additional step.
Second: convert i
to a floating point number, and interpolate between entries in curve->curve_points_pos
. So:
const int i_integral = int(i) % curve->points_pos.size();
const int next_i_integral = int(i+1) % curve->points_pos.size();
const float i_fractional = fmodf(i, 1.f);
const auto curve_point =
curve->curve_points_pos[i_integral] * (1.f - i_fractional) +
curve->curve_points_pos[next_i_integral] * i_fractional;
m_model_mat = glm::translate(glm::mat4(1.f), curve_point);
There are a few bits to unpack there:
i
is now a floating point number, so it may describe a position between entries in curve->points_pos
;curve_point
calculation then models that logic.Aside observations:
Upvotes: 1