Slowing down movement

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
Culando
Posts: 12
Joined: Mon Jan 29, 2007 3:20 am

Slowing down movement

Post by Culando »

I'm working on a game and still getting used to this engine. I'm getting movement working, but it's way to freaking fast and whenever I try to slow it down, it just breaks. Here's the basic code I'm starting for a simple enemy that darts across the screen.

if(enemy_x < 50){
enemy->setPosition(core::vector3df(enemy_x + 1, 0, 0));
enemy_x += 1;
}else{
enemy->setPosition(core::vector3df(-30, 0, 0));
enemy_x = -30;
}

I've tried changing it from enemy_x + 1 to + .001, but that just breaks it. Makes the enemy model stop right smack in the middle of the screen for no reason. Is there any way to slow it down?
Phant0m51
Posts: 106
Joined: Mon Jan 15, 2007 6:07 am

Post by Phant0m51 »

You'll need to use a timer. There are plenty of examples, just use the search to find what you need.
Culando
Posts: 12
Joined: Mon Jan 29, 2007 3:20 am

Post by Culando »

Ok, I tried using a timer like you suggested, but it doesn't seem to be doing anything. TIMER_STEP is an enum value of 5000. I've set it higher but saw no change.

ITimer* myTimer = device->getTimer();
myTimer->start();

...................................

if(enemy_x < 50 && (myTimer->getTime() % TIMER_STEP == 0)){
enemy->setPosition(core::vector3df(enemy_x + 1, 0, 0));
enemy_x += 1;
}else{
enemy->setPosition(core::vector3df(-30, 0, 0));
}
fakin
Posts: 14
Joined: Wed Jan 24, 2007 9:02 pm
Location: zagreb, croatia

Post by fakin »

you can use

Code: Select all

ISceneManager->createFlyStraightAnimator(start, end, time_for_way, loop)
... this way you can calculate time needed needed for char to cross the path by the constant V;

for example... if char speed is 20, you calculate distance between two points, and then divide it by constant speed, which can be modified for some reason, for example if you're hurt or anything, and you get time needed to cross the way.

hope it helps

edit: example

Code: Select all

 ISceneNodeAnimator* anim = ISceneManager->createFlyStraightAnimator(from, to, calculate_time, false); // do not loop
// suppose node to move is named 'im_moving' then
im_moving->addAnimator(anim);
anim->drop;
Culando
Posts: 12
Joined: Mon Jan 29, 2007 3:20 am

Post by Culando »

I actually haven't gotten to the code in almost a week, too busy with work and such.

Ok, it's working now. Though I didn't know where exactly to put that code until I referred back to the movement tutorial. Now I know how all that code works too. It was a bit confusing at first, but I get it all now.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Frame rate independent movement...

Code: Select all

core::vector3df velocity(10.f, 0.f, 0.f);
irr::u32 then = device->getTimer()->getTime();

while(device->run())
{
  if (device->isWindowActive())
  {
    // calculate the elapsed time in fractional seconds since last call 
    irr::u32 now = device->getTimer()->getTime(); 
    irr::f32 elapsed = (now - then) / 1000.f; 
    then = now; 

    core::vector3df position = node->getPosition(); 

    // you might want to rotate the velocity vector into
    // object space...
    core::vector3df delta = velocity * time;
    position += (velocity * time);
    node->setPosition(position);

    // render
Post Reply