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?
Slowing down movement
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));
}
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));
}
you can use
... 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
ISceneManager->createFlyStraightAnimator(start, end, time_for_way, loop)
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;
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.
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.
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