# Getting the time or delta time for touches

Getting the time or delta time for touches
0.0 0

#1

I am trying to get the velocity of a touch.
I read that you have to create a variable that keeps track of time to do this.
I noticed that ccTouchesMoved isn’t called every frame though, and this causes a few jumps in speed.
Here’s is how I came to that conclusion:

``````void Game::update(float dt)
{
time += 1.0f;
}
void Game::ccTouchesBegan(CCSet* touches, CCEvent* event)
{
time = 0.0f;
}
void Game::ccTouchesMoved(CCSet* touches, CCEvent* event)
{
if(time != 1.0f)
{
CCLog("%f", time);
}
time = 0.0f;
}
``````

I am calling a function in ccTouchesMoved and I need delta time as a parameter.
Does anyone know of a way I can do this (other than calling the function from update)?

#2

Why do you need it in ccTouchesMoved? Can you explain a bit your scenario?

It would be great if you can do the same in ccTouchEnd, when user releases its finger.

#3

It’s just moving by velocity.
Here is what my code currently looks like without calling from ccTouchesMoved (summarized):

``````void Game::update(float dt)
{
currentPosition = currentTouch->getLocation();
this->move(currentPosition, lastPosition, dt);
lastPosition = currentPosition;
}
void Game::move(CCPoint currentPosition, CCPoint lastPosition, float dt)
{
if(currentPosition != lastPosition) //if touch moved
{
//drag-like motion
CCPoint point = ccpSub(currentPosition, lastPosition);
velocity = ccpDistance(currentPosition, lastPosition) / dt;
velocityAngle = atan2(point.y, point.x);
}else{ //if touch began, ended, or not touching
if(velocity == 0) //skip if nothing is going to happen
{
return;
}
//velocity motion
float x = velocity * dt * cos(velocityAngle);
float y = velocity * dt * sin(velocityAngle);
//decrease velocity
velocity -= drag * dt; //drag = 600, velocity of 600px/s will reach 0 in 1s
if(velocity < 0)
{
velocity = 0;
}
}
}
void Game::ccTouchesBegan(CCSet* touches, CCEvent* event)
{
CCSetIterator it;
for( it = touches->begin(); it != touches->end(); it++)
{
//touchID check code goes here
currentTouch = (CCTouch*)(*it);
currentPosition = currentTouch->getLocation();
lastPosition = currentPosition;
}
}
void Game::ccTouchesMoved(CCSet* touches, CCEvent* event)
{
CCSetIterator it;
for( it = touches->begin(); it != touches->end(); it++)
{
//touchID check code goes here
currentTouch = (CCTouch*)(*it);
}
}
void Game::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
CCSetIterator it;
for( it = touches->begin(); it != touches->end(); it++)
{
//touchID check code goes here
currentTouch = (CCTouch*)(*it);
currentPosition = currentTouch->getLocation();
lastPosition = currentPosition;
}
}
``````

This code works fine but it’s not perfect enough for me.
What ends up happening is that occasionally, there are 2 frame updates before there is a call to ccTouchesMoved or vice versa (a touch update that skips a frame update).
This causes jitters in movement.
The same thing happens if I create a variable to keep track of time which is incremented in update and call move from ccTouchesMoved using time and previous time.
The same 2 frame updates 1 touch update or 1 frame update 2 touch updates problem persists.
I know that if I were using a call from ccTouchesMoved instead of update, this wouldn’t be a problem (from experience with corona sdk and unity).

Is there any better way to manage time?