Update a model position

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
pedrosal
Posts: 5
Joined: Tue Sep 20, 2005 12:01 pm

Update a model position

Post by pedrosal »

Hello,

I'm having this very strange behaviour and I can't understand why...

I have a Unit class that uses a IAnimatedMeshSceneNode to represent the model. In my engine I call UpdateWorld to allow every unit to change its state according to the current action it is performing.

I set the velocity of the Unit to a given value, calculate a time span and use a x = x0 + vt to update the position.

The first update works. The model gets deslocated from the initial position according to the velocity vector but after that it just stays freezed.

Here is the code:

void CUnit::Update(void)
{
u32 timespan;
u32 currentTime = myGame->GetDevice()->getTimer()->getTime();
timespan = currentTime - m_lastTime;

timespan /= 1000; // now in seconds...

// x = x0 + v0*t

m_WorldPosition = m_WorldPosition + m_Velocity * (f32)timespan;

this->setWorldPosition(m_WorldPosition);
...
}


void setWorldPosition(core::vector3df value)
{
m_WorldPosition = value;

float yy = m_WorldPosition.Y;
float terrain_yy;

// verify if model is going under the ground :)!

if(myGame)
{
terrain_yy = myGame->GetTerrainHeightAtPoint(m_WorldPosition, 1000.0f);

if(yy <= terrain_yy)
yy = terrain_yy;
}

m_WorldPosition.Y = yy;


if(model)
{
model->setPosition(this->m_WorldPosition);
model->updateAbsolutePosition();
}
}

What am I doing wrong?

Thanks,
Pedro
Elise
Posts: 48
Joined: Tue Jul 19, 2005 6:30 am
Contact:

Post by Elise »

Try changing Update to:

Code: Select all

void CUnit::Update(void) 
{ 
  f32 timespan; 
  u32 currentTime = myGame->GetDevice()->getTimer()->getTime(); 
  timespan = (f32) (currentTime - m_lastTime) / 1000.0f; 

  m_lastTime = currentTime; // If you haven't done this elsewhere

  // timespan /= 1000; // now in seconds... 

  // x = x0 + v0*t 

  m_WorldPosition = m_WorldPosition + m_Velocity * timespan; 

  this->setWorldPosition(m_WorldPosition); 
  ... 
}
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

Code: Select all

 u32 timespan; 
 ...
 timespan /= 1000; // now in seconds... 
timespan is an unsigned 32 bit int, which will be 0 if timespan is less than 1000. Also, you must set m_lastTime when CUnit is created, or it will jump forward lots on the first frame
pedrosal
Posts: 5
Joined: Tue Sep 20, 2005 12:01 pm

Post by pedrosal »

Thanks.

I already was doing the update of lasttime in the constructor. About the rounding error resulting from separating the math in severall lines I will try your proposal.

But anyway the model does not move at all except when it loops the first time in the while(device->run()) loop :). So it should not be rounding or math problems...
Pedro
Guest

Post by Guest »

maybe because you call "update();" in the loop and everytime you call the update function you create two local variables again ( "f32 timespan" and "u32 currentTime")
Guest

Post by Guest »

Hei all,

I just found the f... bug! No more debugger driven develop :D!!!

In the first time the loop runned because the engine was loading all the stuff the time span between the creation of the object and the first update allowed the xxx/1000 to be above zero. On a normal render loop the span is so small that dividing by 1000 makes it zero ;)!

Just posted the solution to help anyone who might enconter the same problem...

Thanks to everyone!
Post Reply