Well i don't have much movement code to show, as i just have implemented very basic stuff but:
void Player::validateMovement(){
//ckeck left corners
mCurrentTilePosition = convertToTilePosition(sf::Vector2i(getCollisionBox().left, getCollisionBox().top));
mFirstNewTilePosition = convertToTilePosition(sf::Vector2i(getCollisionBox().left + getMovementVector().x , getCollisionBox().top/* + getMovementVector().y*/)); //top left
mSecondNewTilePosition = convertToTilePosition(sf::Vector2i(getCollisionBox().left + getMovementVector().x , getCollisionBox().top + getCollisionBox().height/* + getMovementVector().y*/)); //bottom left
//collision
if(mCurrentRoomTiles[mFirstNewTilePosition.x][mFirstNewTilePosition.y].getCollideID() == 1 || mCurrentRoomTiles[mSecondNewTilePosition.x][mSecondNewTilePosition.y].getCollideID() == 1){
setMovementVector(sf::Vector2f(0.0, getMovementVector().y));
}
//doors
else if(mCurrentRoomTiles[mFirstNewTilePosition.x][mFirstNewTilePosition.y].getTileID() == 2 || mCurrentRoomTiles[mSecondNewTilePosition.x][mSecondNewTilePosition.y].getTileID() == 2){
if( (mFirstNewTilePosition.x == 0 && mFirstNewTilePosition.y == 4 ) || (mSecondNewTilePosition.x == 0 && mSecondNewTilePosition.y == 4)) mTeleportTo = "Left";
else if( (mFirstNewTilePosition.x == 7 && mFirstNewTilePosition.y == 0 )) mTeleportTo = "Up";
else if( mSecondNewTilePosition.x == 7 && mSecondNewTilePosition.y == 8) mTeleportTo = "Down";
}
//ckeck top corners
mCurrentTilePosition = convertToTilePosition(sf::Vector2i(getCollisionBox().left, getCollisionBox().top));
mFirstNewTilePosition = convertToTilePosition(sf::Vector2i(getCollisionBox().left /*+ getMovementVector().x*/ , getCollisionBox().top + getMovementVector().y)); //left top
mSecondNewTilePosition = convertToTilePosition(sf::Vector2i(getCollisionBox().left /*+ getMovementVector().x*/ + getCollisionBox().width , getCollisionBox().top + getMovementVector().y)); //right top
//collision
if(mCurrentRoomTiles[mFirstNewTilePosition.x][mFirstNewTilePosition.y].getCollideID() == 1 || mCurrentRoomTiles[mSecondNewTilePosition.x][mSecondNewTilePosition.y].getCollideID() == 1){
setMovementVector(sf::Vector2f(getMovementVector().x, 0.0));
}
//doors
else if(mCurrentRoomTiles[mFirstNewTilePosition.x][mFirstNewTilePosition.y].getTileID() == 2 || mCurrentRoomTiles[mSecondNewTilePosition.x][mSecondNewTilePosition.y].getTileID() == 2){
if( (mFirstNewTilePosition.x == 7 && mFirstNewTilePosition.y == 0 ) || (mSecondNewTilePosition.x == 7 && mSecondNewTilePosition.y == 0)) mTeleportTo = "Up";
else if( (mFirstNewTilePosition.x == 0 && mFirstNewTilePosition.y == 4 )) mTeleportTo = "Left";
else if( (mSecondNewTilePosition.x == 14 && mSecondNewTilePosition.y == 4)) mTeleportTo = "Right";
}
.
.
.
}
void MovableObject::moveObject(){
if(mMovementVector.x != 0 || mMovementVector.y != 0){
this->move(mMovementVector.x, mMovementVector.y);
setCollisionBox(sf::FloatRect(mCollisionBox.left + mMovementVector.x, mCollisionBox.top + mMovementVector.y, mCollisionBox.width, mCollisionBox.height));
setCollisionBoxShapePosition(sf::Vector2f(mCollisionBox.left, mCollisionBox.top));
}
}
The first Method is basically a part of my (current) collisiondetection for collisions with the room. It basically sets the movementvector.x and/or .y to 0 if the movement would cause a collision. (not the optimal way, in future i want to adjust it depending on the distance to the collisionobject to move right up to it)
The second method moves the playersprite, collisionbox and my visualization (ofc just for development).
the movementvector is set via 8 else if comparisons:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
setMovementVector(sf::Vector2f(-getMovementSpeed(), 0.0f));
setNextAnimation(3);
}
(getMovementspeed() just returns a double value)
the code is not yet made to work without the inbuild frame limiter and will let the player cross the whole room in 1sec without it, but that is a fixable problem.
but i get those framedrops in the menu as well (without much to draw) even though i don't know if it stutters there.
(the stutter, to describe it better) is a really short break in a continous movement(with pressed key)
i actually had someone else test it (quite a powerfull i5 as cpu and 8gigs of ram, quite a bit stronger than my machine). He "felt" no stuttering but had the same framedrops. But i guess he just did not notice as my cpu is below 10% with the built in limiter, which makes me think cpu performance is not an issue.