I have a sprite that is supposed to stay in place when I move the camera around the level, but when I try to move it my sprite floats out of position while camera is moving. How can I fix this? I tried to do 'fix your time step' thing but something tells me I am doing it the wrong way.
Here are the screenshots that I cropped out to illustrate the problem.
This one is the correct position when camera is not moving: https://dl.dropboxusercontent.com/u/118608180/correct_position.png
And these are the screenshots what happens depending on which side i choose to move the camera:
https://dl.dropboxusercontent.com/u/118608180/incorrect_position_1.png (if i move camera up-left)
https://dl.dropboxusercontent.com/u/118608180/incorrect_position_2.png (if i move camera down)
https://dl.dropboxusercontent.com/u/118608180/incorrect_position_3.png (if i move camera right)
And here is a releveant portion of the code I used:
float dt = 0.0f;
// loop until a WM_QUIT message is received
msg.message = static_cast<UINT>(~WM_QUIT);
while(msg.message != WM_QUIT)
{
sf::Event Event;
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// Win32 part
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else // SFML part
{
// start frame clock
timer.start();
RenderView.clear(sf::Color(64, 64, 64));
// check mouse position
Mouse = sf::Mouse::getPosition(RenderView);
// set camera move speed
cam->SetCamSpeed(1);
dt = timer.msec;
while(RenderView.pollEvent(Event))
{
if(Event.type == sf::Event::KeyPressed)
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) // move camera up
{
cam->CamOffsetY -= cam->CamSpeed * dt;
StandardView.move(0.0f, -(cam->CamSpeed * dt));
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) // move camera down
{
cam->CamOffsetY += cam->CamSpeed * dt;
StandardView.move(0.0f, cam->CamSpeed * dt);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) // move camera left
{
cam->CamOffsetX -= cam->CamSpeed * dt;
StandardView.move(-(cam->CamSpeed * dt), 0.0f);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) // move camera right
{
cam->CamOffsetX += cam->CamSpeed * dt;
StandardView.move(cam->CamSpeed * dt, 0.0f);
}
}
}
// find map tile that has mouse cursor on it
for(unsigned int i = 0; i < spr.size(); i++)
{
if(Mouse.x + cam->CamOffsetX > spr[i].getGlobalBounds().left &&
Mouse.x + cam->CamOffsetX < spr[i].getGlobalBounds().left + spr[i].getGlobalBounds().width &&
Mouse.y + cam->CamOffsetY > spr[i].getGlobalBounds().top &&
Mouse.y + cam->CamOffsetY < spr[i].getGlobalBounds().top + spr[i].getGlobalBounds().height)
{
ValidMarker = 1;
marker.setPosition(spr[i].getPosition().x, spr[i].getPosition().y);
break;
}
else
ValidMarker = 0;
}
for(unsigned int i = 0; i < spr.size(); i++)
RenderView.draw(spr[i]); // draw grid sprites
RenderView.setView(StandardView);
if(ValidMarker == 1) // draw marker if mouse is hovering over tiles
RenderView.draw(marker);
RenderView.draw(CharacterPawn);
RenderView.display();
// stop frame clock
timer.stop();
}
}