Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using C++ with sfml library problem

8 views
Skip to first unread message

nvangogh

unread,
May 12, 2013, 2:11:05 PM5/12/13
to
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

Geoff

unread,
May 12, 2013, 3:02:45 PM5/12/13
to
On Sun, 12 May 2013 18:11:05 GMT, nvangogh <nvan...@invalid.net>
wrote:

[snip]

>"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.

You stated you wanted to move 500 pixels in 2 seconds but your formula
uses 400 pixels. You should be using -250 there.

Even more, you should probably generalize the equation to read:
// missile.move uses dx/dy * dt
dx = 0; // go straight up
dy = missile.velocity * elapsed.asSeconds();
missile.move(dx, dy);

... and set missile.velocity in your initialization of that object.

nvangogh

unread,
May 12, 2013, 3:38:25 PM5/12/13
to
Its ok i have found a solution now. Thanks

nvangogh

unread,
May 12, 2013, 3:44:08 PM5/12/13
to
The image started at y 100 and moves a further 500 pixels to top of the
window. Thank you your right.
I have a solution that someone posted now that i can use to figure out
how to program the speed of other sprites in the program.
thanks for your help.
0 new messages