Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Tutorial for vectors?  (Read 4706 times)

0 Members and 1 Guest are viewing this topic.

Lardos

  • Newbie
  • *
  • Posts: 2
    • View Profile
Tutorial for vectors?
« 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

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: Tutorial for vectors?
« Reply #1 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Tutorial for vectors?
« Reply #2 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.
« Last Edit: April 27, 2012, 11:49:26 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lardos

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Tutorial for vectors?
« Reply #3 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

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Tutorial for vectors?
« Reply #4 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: