Help with SFML: Need smooth sprite movement

May 27, 2013 at 7:08pm
closed account (ypfz3TCk)
Hello, I need some help with sfml 2.0. I have been working on a small test program for 'space invaders'. The part i am looking at is moving the ship left / right and firing a missile.

Problem 1: When i press left-shift to fire a missile - if i am also pressing either the left or right button to move the ship at the same time, the ship's movement is not smooth - it seems to stop for a split second or so.

problem 2: In addition, if i try to move the ship while i fire a missile - sometimes the missile will not come from the centre point of the ship even though I have be careful to set the position of the missile to the centre of the ship's boundary rectangle. It seems as if there is a lag between the ship moving and the missile going up.

The effect I need is to get smooth movement of the ship at all times and it needs to be able to fire a missile from its centre with no time lag.

Now I have been trying to work it out and believe that the problem may be in the way i am getting the input - but i cannot seem to find the right way to do it.

Please look at my program and let me know if you can see my mistake. I have included the full test program:

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
103
104
105
106
107
108
109
110
// test program - a ship fires a missile

#include<SFML/Graphics.hpp>
#include<SFML/System.hpp>
int main()
{
sf::RenderWindow mywindow(sf::VideoMode(800,600), "test", sf::Style::Fullscreen);
mywindow.setVerticalSyncEnabled(true);

// initialize & map sprites
sf::Texture shippic;
shippic.loadFromFile("ship.png");
sf::Sprite ship;
ship.setTexture(shippic);
ship.setPosition(400,500);
sf::FloatRect shipsize; 
shipsize = ship.getLocalBounds();
float ship_centre_offset = shipsize.width / 2;
sf::Texture rocket_picture;
rocket_picture.loadFromFile("missile.jpg");
sf::Sprite missile;
missile.setTexture(rocket_picture);
missile.setScale(0.2f,0.2f);
sf::FloatRect rocketsize;
rocketsize = missile.getGlobalBounds();
float missile_width = rocketsize.width;
float missile_height = rocketsize.height; 
sf::FloatRect shiprect;
sf::Vector2f ship_position(0,0);
sf::Vector2f missile_position(0,0);

// time management 
sf::Time elapsed;
sf::Clock clock;

// other control variables

bool running = true;
bool firemissile = false;

while(mywindow.isOpen())
{
elapsed = clock.restart();

shiprect = ship.getGlobalBounds();
float global_top = shiprect.top;
float global_left = shiprect.left;
float global_centre = global_left + ship_centre_offset;
  	   
sf::Event event;
while (mywindow.pollEvent(event))
{
if((event.type == sf::Event::KeyPressed) && 
(event.key.code == sf::Keyboard::Escape))
mywindow.close();
}	

int keycode(0);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
keycode = 1;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
keycode = 2;	
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))
keycode= 3;
switch(keycode)
{
case 1:// move left
{
ship_position = ship.getPosition();
if(ship_position.x <= 0.0)
ship.setPosition(0.0,500);
else
ship.move(-0.130 * elapsed.asMilliseconds(),0);
break;
}
case 2: // move right
{
ship_position = ship.getPosition();
if(ship_position.x >= 700.0)
ship.setPosition(800-shipsize.width,500);
else
ship.move(0.130 * elapsed.asMilliseconds(),0);
break;
}
case 3: // fire missile
{
firemissile = true;
break; 
}
 } // end switch

if(firemissile)
{
missile.move(0, -200 * elapsed.asSeconds());
}
missile_position = missile.getPosition();
if(missile_position.y <= 0) 
{
firemissile = false;
missile.setPosition(global_centre - (missile_width / 2),500 - missile_height);		
}
mywindow.clear();
mywindow.draw(ship);
if(firemissile)
mywindow.draw(missile);
mywindow.display();
} // while window is open
 return EXIT_SUCCESS;
}//main


Many thanks
May 27, 2013 at 7:22pm
Problem that your move and fire missile cases are mutually exclusive.
And if shift is pressed it shadows all other buttons. Place lines 63-64 between lines 58-59 and you will find that you cannot fire missiles when moving at all.

Make it somewhat parallel: like several non-exclusive ifs
May 27, 2013 at 7:29pm
closed account (ypfz3TCk)
I do need the effect of firing missile when moving - so definitely don't what to disable ability to fire when moving.
May 27, 2013 at 7:45pm
I told that to illustrate problem with your code.
May 27, 2013 at 7:55pm
closed account (ypfz3TCk)
alright mate - i will try experiments with removing the switch statement.
thanks
Topic archived. No new replies allowed.