Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: problem with view movement  (Read 2392 times)

0 Members and 1 Guest are viewing this topic.

Scipi

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
problem with view movement
« on: October 22, 2011, 08:48:43 pm »
Hello,

I am making an RPG using SFML and I'm running into this problem when it comes to views. I have this code:

Code: [Select]

/////////////
//ANIMATE
/////////////
 
void Character::Animate(sf::RenderWindow& App, Game& Game)
{
    float offset =  128 * App.GetFrameTime();
    if(PosX < MapX * 64)
        {
            PosX += offset;
            Game.View.Move(offset, 0);
        }
    else if(PosX > MapX * 64)
        {
            PosX -= offset;
            Game.View.Move(-offset, 0);
        }
    else if(PosY < MapY * 64)
        {
            PosY += offset;
            Game.View.Move(0, offset);
        }
    else if(PosY > MapY * 64)
        {
            PosY -= offset;
            Game.View.Move(0, -offset);
        }
    else if(PosX == MapX * 64 && PosY == MapY * 64)
        {
            Moving = false;
            //PosX ++;
            //MapX++;
            //PosY++;
            //MapY++;
            //cout << "Moving == false";
        }
    Timer++;
    if(Timer == 1 && Moving)
        Direction++;
    else if(Timer == 8 && Moving)
        Direction--;
    else if(Timer == 16 && Moving)
        Direction += 2;
    else if (Timer == 24 && Moving)
        Direction -= 2;
    if(Moving)
     cout << Timer << " ";
    //else if (Timer == 64)
    //cout << PosX << " " << PosY << " " << MapX << " " << MapY << "\n";
 
 
    return;
}


Which is called once a arrow key is pressed. However, the view is moving faster than the character though they ought to be moving at the same speed. Any ideas as to what's going wrong? Thanks! :D
I issa Cat Person =^w^=

Haikarainen

  • Guest
problem with view movement
« Reply #1 on: October 24, 2011, 03:52:53 pm »
I don't really get what that code is supposed to do, but how I see it is;
Get a velocity value,
Check if character is out of mapbounds (you should really set him to the bounds and not just subtract, to get a perfect value),
Animate some stuff using a frametimer.


Regarding the view anyhow, just make it follow the player after you moved the player like you wanted that whole frame!
First, have the code where you move the player, then, have this code moving the view towards the player;
Code: [Select]
// Set speed of view
ViewVelocity = 100 * Frametime;

// Do this for every Axis
if(Player.X < View.X){
   //  Will the view move too far , causing unsmooth movement?
   if(View.X - Player.X < ViewVelocity){
      // Calibrate
      View.X = Player.X;
   }else{
      // Else, subtract velocity from position.
      View.X -= ViewVelocity;
   }
}


Or just always set it to Player.X.

Regarding the animations; You should really use a sf::Clock for the animations, On fast computers you will have sonic animations, while on a 486 you will have stoner animations.

Scipi

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
problem with view movement
« Reply #2 on: November 01, 2011, 04:55:43 pm »
Noted, thanks.

I ended up using View.SetCenter on the characters coords plus halve the size of the sprite to get the center and it works perfectly. I did not know about the sf::Clock. What algorithm could I use to get what sprite should be displayed for each part of the animation?

The format I use now switches between three sprites in a vector and switches between them in the order of 0, 1, 0, 2 to get the animation.
I issa Cat Person =^w^=

 

anything