[SFML 2.1] How do I move the sprite depending on rotation

Hey how's it goin fellow programmers.
So heres the problem I have.
This is how I want my sprite movement to work: if my sprite is faced upwards and i press W, it will move up. If my sprite is faced to the right and i press W, it will go right. etc. // It doesn't work like that right now and I have no idea how to do it.
The sprite's rotation works fine.

1
2
3
4
5
6
7
8
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
{
	playerSprite.rotate(-0.08 * dt);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::E))
{
	playerSprite.rotate(0.08 * dt);
}

 
playerSprite.setOrigin(texture.getSize().x / 2, texture.getSize().y / 2); // This is applied in another place. it works fine. 


and here is my movement code(just pasted W and S, because A and D are not necessary atm..):
1
2
3
4
5
6
7
8
9
10
11
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
if(havePlayersCollided == false)
	playerSprite.move(0, -speedForDelta * dt);
//std::cos(3.14159265 * playerSprite.getRotation() / 180.f) < I found this code in a old thread, but I have no idea how it works nor what to do with it.
			}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
	if(havePlayersCollided == false)
		playerSprite.move(0, speedForDelta * dt);
}


So yeah.
My question is how do I move the character depending on the rotation?
/*
Also, you might see the "if(havePlayersCollided == false) {playerSprite.move(yadiyada)}" . yes i dont need the haveplayerscollided function because it doesnt work the way I want it to. I might make a thread for it in the future, but right now I need to get the rotation movement fixed before I move onto the collision detection between players.
*/
My question is how do I move the character depending on the rotation?

Keep track of the direction that the player is facing, which it appears that you already do, and apply a transform to determine the new coordinates instead of moving them by a fixed offset.

SIDE NOTE: IMHO, the only games that really work with this kind of a control scheme are space shooters like Asteroids. Just my two cents.
1
2
3
float x = sinf(playerSprite.getRotation() * PI / 180) * dt.asSeconds();
float y = -cosf(playerSprite.getRotation() * PI / 180) * dt.asSeconds();
playerSprite.move(x, y);
Last edited on
JRHolmes, that code doesn't work and I don't know why. I have the value dt as a float in the class, but in the main class it is a sf::Time value.(and it is asMilliseconds) I made another value called float dt2 and I made it asSeconds value. Its restarting all the time tho, maybe I should make a new value just so it wouldn't restart?
1
2
float x = sinf(playerSprite.getRotation() * 3.14159265 / 180) * dt2;
float y = -cosf(playerSprite.getRotation() * 3.14159265 / 180) * dt2;

1
2
3
4
5
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
	if(havePlayersCollided == false)
		playerSprite.move(x, y);
}


as for what you said Computegeek01, I don't understand quite how the Transform class works and how to determine the new coordinates.(I would really like to know how to determine the new coordinates, so I can apply better collision to the game like not moving forward the opponent when the new coordinates intersect with them)
I saw something like this on Sfml tutorial page:
1
2
3
4
5
sf::Transform t;
t.translate(10, 100);
t.rotate(90);
t.translate(-10, 50);
t.scale(0.5f, 0.75f);

I have no idea whats going on there tho. Why are there 2 translates there, what do they do and how does it all work together.

Any help would be appreciated. :)
I'm not sure how you have your project set up, but dt in my example is of type sf::Time and is passed through to the appropriate function every frame.
Its restarting all the time tho, maybe I should make a new value just so it wouldn't restart?

Not sure what you mean by this.

Well i use the dt value for movement, so it restarts alot every second. I think it was clock.restart() or smth(it restarts in the main.cpp but i use the same thing in player.cpp) , i will look up later sometime when i get access to my computer
The Aurora Library contains some functions which make calculations with sf::Vector much easier if I remember correctly (I used it in a project and had the same type of movement):
http://www.bromeon.ch/libraries/aurora/index.html
Well dt(delta time) should restart, because you are using its value for updating logic independent of the frame rate. If I remember correctly, clock.restart() returns the elapsed time and sets back to 0. If you set dt to the value clock.restart() returns each game loop and pass this as a sf::Time (not as a float) to your function, and see what happens.
Topic archived. No new replies allowed.