SFML community forums

Help => General => Topic started by: Lardos on April 25, 2012, 09:02:14 pm

Title: Tutorial for vectors?
Post by: Lardos on April 25, 2012, 09:02:14 pm
Hello everyone,
Fristly sorry for my english (I'm from germany).
I have big problems with the Vector2 class in sfml 2.0. I don't know how to use it and I can't find any tutorial about this topic. For example:

I got a simple sprite and I want to make this sprite moving around (with my arrow keys). So I need the current x and y position + a value (e.g. 10), when I hit one of the arrow keys.

I found this in the documentation:

const Vector2f& sf::Transformable::getPosition   ()    const

How do I have to use it?

maybe like that?

Sprite.getPosition(sf::Vector2f(0, 0));

But this will not work because when I do this, I set the Position to 0, 0...

So, how can I get the current x and as next the current y position?

Many greetings from germany,
Lardos
Title: Re: Tutorial for vectors?
Post by: Lo-X on April 25, 2012, 09:18:12 pm
const Vector2f& sf::Transformable::getPosition   ()    const

How do I have to use it?

maybe like that?

Sprite.getPosition(sf::Vector2f(0, 0));
no
But this will not work because when I do this, I set the Position to 0, 0...
no
So, how can I get the current x and as next the current y position?
const Vector2f& sf::Transformable::getPosition   ()    const
=)

I think you have to read mode about how to program in C++ (books, ...)
The above header class method means :
The method getPosition() of sf::Transformable class
returns a sf::Vector2f that is constant
and this method is constant

So you have to deal with sf::Vector2f that is returned. Example :

Code: [Select]
sf::Vector2f position = Sprite.getPosition();
// Pos X :
position.x
// Pos.Y :
position.y

And better :

Code: [Select]
// Pos X:
Sprite.getPosition().x;
// Pos Y:
Sprite.getPosition().y
Title: Re: Tutorial for vectors?
Post by: eXpl0it3r on April 25, 2012, 09:18:37 pm
Edit: too slow...

Welcome to the community!

Is your problem just about sf::Vector2 or do you have problems in understand some basics of C++ in general?

You can get and set the position in the following manner.
sf::Vector2f vec = sprite.getPosition(); // Get the position
// Do your calculations
vec.x += 5.f;
vec.y += 1.f;
sprite.setPosition(vec); // Set the position
 

I'd highly suggest to read more about C++ before you dig deeper into SFML.
Title: Re: Tutorial for vectors?
Post by: Lardos on April 25, 2012, 09:31:13 pm
Thank you for the fast answers!

I did never work with sfml before and I was suprised about the Vector funktion (firstly I thought that there ist something like getPositionX() and getPositionY().

But with your examples I have unterstand, how I have to use this Vectors.
Thank you a lot!

Lardos
Title: Re: Tutorial for vectors?
Post by: Nexus on April 27, 2012, 05:56:26 pm
The concept of vector algebra is fundamental for game programming, maybe you should inform yourself also about the mathematical backgrounds. Wikipedia might be a good starting point.