SFML moving the camera problem

Okay, so I'm fairly new to sfml, and I don't quite know how all of it works. I watched a few tutorials and have built on from it. I know some basic c++ and wanted to try my hand at making a basic game, so I'd have some experience. I'll include all 3 of my referenced code, as it may be of some help. My main problem is, in my PlayerCharacter class, I use an update function so the velocity of the character is updated whenever the value elapsedTime is counted. It works for the character, but I wanted a camera that would follow the character once the sprite reaches a 164x164 square in the middle, but the * elapsedTime function won't work for it. The camera moves when butting other operators infront of elapsedTime, but not in the way one would want. What should I do to allow it to work right, and update on time? I included three blocks of code because I feel the problem can be solved somewhere in there.

PlayerCharacter class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "stdafx.h"

PlayerCharacter::PlayerCharacter() :
_velocityX(0),
_velocityY(0),
_maxVelocity(200.0f)
{
	Load("images/Player.png");
	assert(IsLoaded());

	GetSprite().setOrigin(GetSprite().getScale().x / 2, GetSprite().getScale().y / 2);
}

PlayerCharacter::~PlayerCharacter()
{
}

void PlayerCharacter::Draw(sf::RenderWindow & rw)
{
	VisibleGameObject::Draw(rw);
}

float PlayerCharacter::GetVelocity() const
{
	return _velocityX;
	return _velocityY;
}

void PlayerCharacter::Update(float elapsedTime)
{

	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A))
	{
		_velocityX -= 2.0f;
	}

	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D))
	{
		_velocityX += 2.0f;
	}

	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::W))
	{
		_velocityY -= 2.0f;
	}

	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::S))
	{
		_velocityY += 2.0f;
	}

	if (_velocityX > 0)
	{
		_velocityX -= 0.25f;
	}

	if (_velocityX < 0)
	{
		_velocityX += 0.25f;
	}

	if (_velocityY > 0)
	{
		_velocityY -= 0.25f;
	}

	if (_velocityY < 0)
	{
		_velocityY += 0.25f;
	}

	if (_velocityX > _maxVelocity)
		_velocityX = _maxVelocity;

	if (_velocityX < -_maxVelocity)
		_velocityX = -_maxVelocity;

	if (_velocityY > _maxVelocity)
		_velocityY = _maxVelocity;

	if (_velocityY < -_maxVelocity)
		_velocityY = -_maxVelocity;


	sf::Vector2f pos = this->GetPosition();

	GetSprite().move(_velocityX * elapsedTime, _velocityY * elapsedTime);

	camera.reset(sf::FloatRect(0, 0, Game::SCREEN_WIDTH, Game::SCREEN_HEIGHT));
	camera.setCenter(Game::SCREEN_WIDTH / 2, Game::SCREEN_HEIGHT / 2);

	if(VisibleGameObject::GetWidth() <= ((Game::SCREEN_WIDTH / 2) -164) 
		|| VisibleGameObject::GetWidth() >= ((Game::SCREEN_WIDTH / 2) +164)
		|| VisibleGameObject::GetHeight() <= ((Game::SCREEN_HEIGHT / 2) + 164)
		|| VisibleGameObject::GetHeight() >= ((Game::SCREEN_HEIGHT / 2) - 164)
		)
	{
		camera.move(_velocityX * elapsedTime, _velocityY * elapsedTime);
	}

	Game::_mainWindow.setView(camera);
}


VisibleGameObject class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "stdafx.h"

VisibleGameObject::VisibleGameObject()
	: _isLoaded(false)
{
}

VisibleGameObject::~VisibleGameObject()
{
}

void VisibleGameObject::Load(std::string filename)
{
	if (_image.loadFromFile(filename) == false)
	{
		_filename = "";
		_isLoaded = false;
	}
	else
	{
		_filename = filename;
		_sprite.setTexture(_image);
		_isLoaded = true;
	}
}

void VisibleGameObject::Draw(sf::RenderWindow & renderWindow)
{
	if (_isLoaded)
	{
		renderWindow.draw(_sprite);
	}
}

void VisibleGameObject::Update(float elapsedTime)
{
}

void VisibleGameObject::setPosition(float x, float y)
{
	if (_isLoaded)
	{
		_sprite.setPosition(x, y);
	}
}

sf::Vector2f VisibleGameObject::GetPosition() const
{
	if (_isLoaded)
	{
		return _sprite.getPosition();
	}
	return sf::Vector2f();
}

float VisibleGameObject::GetHeight() const
{
	return _sprite.getScale().y;
}

float VisibleGameObject::GetWidth() const
{
	return _sprite.getScale().x;
}

sf::Rect<float> VisibleGameObject::GetBoundingRect() const
{
	sf::Vector2f size = _sprite.getScale();
	sf::Vector2f position = _sprite.getPosition();

	return sf::Rect < float >
		(position.x - size.x/2,
		position.y - size.y/2,
		position.x + size.x/2,
		position.y + size.y/2
		);
}

sf::Sprite & VisibleGameObject::GetSprite()
{
	return _sprite;
}

bool VisibleGameObject::IsLoaded() const
{
	return _isLoaded;
}


GameObjectManager Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "stdafx.h"

GameObjectManager::GameObjectManager()
{
}

GameObjectManager::~GameObjectManager()
{
	std::for_each(_gameObjects.begin(), _gameObjects.end(), GameObjectDeallocator());
}

void GameObjectManager::Add(std::string name, VisibleGameObject*gameObject)
{
	_gameObjects.insert(std::pair<std::string, VisibleGameObject*>(name, gameObject));
}

void GameObjectManager::Remove(std::string name)
{
	std::map<std::string, VisibleGameObject*>::iterator results = _gameObjects.find(name);


	if (results != _gameObjects.end())
	{
		delete results->second;
		_gameObjects.erase(results);
	}
}

VisibleGameObject* GameObjectManager::Get(std::string name) const
{
	std::map<std::string, VisibleGameObject*>::const_iterator results = _gameObjects.find(name);
	if (results == _gameObjects.end())
		return NULL;
	return results->second;
}

int GameObjectManager::GetObjectCount() const
{
	return _gameObjects.size();
}

void GameObjectManager::DrawAll(sf::RenderWindow & renderWindow)
{
	std::map<std::string, VisibleGameObject*>::const_iterator itr = _gameObjects.begin();
	while (itr != _gameObjects.end())
	{
		itr->second->Draw(renderWindow);
		itr++;
	}
}

void GameObjectManager::UpdateAll()
{
	std::map<std::string, VisibleGameObject*>::const_iterator itr = _gameObjects.begin();
	float timeDelta = clock.restart().asSeconds();

	while (itr != _gameObjects.end())
	{
		itr->second->Update(timeDelta);
		itr++;
	}
}


Sorry for all the code, I just don't know where my problem would be, and where I would have to fix it, so, thank you to all who are willing to help!
Last edited on
Topic archived. No new replies allowed.