Hi,
I have tried asking this question in the sfml forum - but so far have not
had any clear answers. I am hoping that you will be able to help me. The
problem might be with my interpretation of the documentation.
I am trying to implement a version of space invaders game in c++ using the
sfml library. Once i have working code, the plan is to redesign it into a
proper object oriented program. I am doing this to learn about
implementing my own classes, virtual functions and inheritance.
Now, I have broken the problem down to a level that (hopefully) you might
be able to help - even though you are not concerned at all with sfml.
The part of my program that i am having trouble with is to do with
controlling the movement of a sprite. This is a small test program that
moves my sprite - when the left shift key is pressed. The problem is that
there is no control over the speed of movement.
code:
-------
// an implementation of a missile.
#include <iostream>
#include<SFML/Graphics.hpp>
#include<SFML/System.hpp>
int main()
{
sf::RenderWindow mywindow(sf::VideoMode(800, 600, 32), "missile testing");
mywindow.setVerticalSyncEnabled(true);
// create sprite sf::Texture missilepic;
missilepic.loadFromFile("/home/game/images/missile.png");
sf::Sprite missile;
missile.setTexture(missilepic);
missile.setPosition(400,500);
missile.setScale(0.01f,0.02f);
sf::FloatRect missilesize;
sf::Vector2f position(400,500);
bool running = true;
while(mywindow.isOpen())
{
sf::Event event;
while (mywindow.pollEvent(event))
{
if((event.type == sf::Event::KeyPressed) &&
(event.key.code == sf::Keyboard::Escape))
mywindow.close();
if((event.type == sf::Event::KeyPressed) &&
(event.key.code == sf::Keyboard::LShift))
{
// fire 1 missile! - PROBLEM IS HERE while(position.y
>= 0)
{
missile.move(0, -1);
--position.y;
}
}
}
mywindow.clear(); mywindow.draw(missile); mywindow.display();
} // while window is open
return EXIT_SUCCESS;
}//matches main -----------
If you happened to run this program (you will need sfml 2.0 and adjust the
line: missilepic.loadFromFile("/home/game/images/missile.png"); to point
to your image.) you will see that the sprite moves up the screen at a fast
rate.
I want my sprite to take 2 seconds to traverse the distance of 500 pixels.
You would think that the equations relating to time, distance & speed
would help. That is: 1. speed = distance / time, 2. distance = speed *
time, 3. time = distance / speed. So I have looked at sfml documentation
to see exactly how to achieve this. There are three relevant classes:
1. sf::Clock. This has two member functions. getElapsedTime() and restart
().
2. sf::Time. Measured in microseconds, milliseconds and seconds.
3. sf::Sprite - the 'missile' is a sprite and it uses the member function
'move()' to move it. The documentation says about move( ):
"void move (float offsetX, float offsetY)
Move the object by a given offset."
I am confused about HOW to factor in time into the call to move( ). I have
looked at code other people have used and they use expressions like this:
Sprite.move( 0,-1 * 50 * dt.asSeconds());
The latest advice i had was that
"So you already have everything. Your speed here is 2.
If you want your missile to travel 400px in 2 seconds, give it a speed of
400 / 2 = 200. (200px/s)
missile.move(0, -200 * elapsed.asSeconds());
It will travel a "defined number of pixels (400px) in 2 seconds"
But this does not work - it leaves the sprite halfway up the y axis.
---------------------------------
I think the problem is that people who have (kindly) tried to help have
used code formulas that are copied and so cant really explain how this
works. I am not very good at maths and thats why i hoped someone might be
able to help me out here.
(You can see the full thread in sfml forums here:
http://en.sfml-dev.org/forums/index.php?topic=11456.msg79919#msg79919
and the sfml class decriptions:
http://www.sfml-dev.org/documentation/2.0/annotated.php )
Thanks