Moving sprite in all directions?

I've been following a tutorial on how to create a basic c++ game of pong, however I want to change the code so that the paddle can go anywhere rather than just left or right.

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
  #include "StdAfx.h"
#include "Playertank.h"
#include "Game.h"


Playertank::Playertank() :
_velocity(0),
_maxVelocity(600.0f)
{
	Load("images/tank.png");
	assert(IsLoaded());

	GetSprite().SetCenter(GetSprite().GetSize().x /2, GetSprite().GetSize().y / 2);

}


Playertank::~Playertank()
{
}

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

float Playertank::GetVelocity() const
{
	return _velocity;
}

void Playertank::Update(float elapsedTime)
{

	if(Game::GetInput().IsKeyDown(sf::Key::A))
	{
		_velocity-= 3.0f;
	}
	if(Game::GetInput().IsKeyDown(sf::Key::D))
	{
		_velocity+= 3.0f;
	}

	if(Game::GetInput().IsKeyDown(sf::Key::S))
	{
		_velocity= 0.0f;
	}

	if(_velocity > _maxVelocity)
		_velocity = _maxVelocity;

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


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

	if(pos.x  < GetSprite().GetSize().x/2
		|| pos.x > (Game::SCREEN_WIDTH - GetSprite().GetSize().x/2))
	{
		_velocity = -_velocity; // Bounce by current velocity in opposite direction
	}
	
	GetSprite().Move(_velocity * elapsedTime, 0);
}


This is the code where I believe it can be changed but for the life of me I don't understand where to change it. Ideally I would want to use the 'W' key to go forward and the 'A' to rotate left and 'D' to rotate right. Can anyone give me a hint or point me in the direction of a tutorial to help?

If you need any more information or code please ask!

Thank you :)
Last edited on
http://www.filedropper.com/tankgamehelp

Here's the full file, at the moment All I've made are a couple menus and two independently controlled paddles, but ideally I want to move them all around the screen as opposed to just left and right.
If you want to do this, you'll need a basic bit of trignomotery. Luckily for you, this is only a 2D game, so almost no thinking is required :)

Anyway: You would need a variable for the rotation angle as well as your velocity. When 'A' is being pressed, you add to the angle, and when 'D' is being pressed, you subtract. Now, onto the trig bit: If you have a triangle, and an angle, imagine this as your rotation and 'a' as your y velocity and 'b' as your x velocity:
  |\_
a |  \_
  |____\
     b

Now, we know that 'a' and 'b' are the same, and they will be _velocity. So we can very easiily find our total velocity now with trigonometry:
1
2
3
// (EDIT) _rot is in radians
GetSprite().Move(_velocity * elapsedTime * std::sin(_rot),
                 _velocity * elapsedTime * std::cos(_rot));

Hope this helps!

EDIT:
Of course, you will need to do a couple of other things too, like rotate the sprite, and process bouncing and whatever, but you should be able to work out (Hint: you have the rotation of the object).
Last edited on
Wow thank you so much! I'll edit this post with the finished code when I'm done :)
Sorry, I just noticed an error in my example in my last post - I got the sin and the cos mixed up :)
1
2
GetSprite().Move(_velocity * elapsedTime * std::cos(_rot),
                 _velocity * elapsedTime * std::sin(_rot)); 


In case you don't understand why, in the picture aboce assume that _rot is the bottom right of the triangle. sin(_rot) is the opposite over the hypotenuse, i.e. a / 1 = a (assuming hypotenuse' length is 1 for simplicity, as is normally done). cos(_rot) is the adjacent over the hypotenuse, i.e. b / 1 = b. If your code wasn't working, this would be why.
Topic archived. No new replies allowed.