SFML community forums

Help => General => Topic started by: Sanction on November 30, 2014, 08:29:13 am

Title: How to set Valocity with Frame rate?
Post by: Sanction on November 30, 2014, 08:29:13 am
I'm trying to move mPlayer to Mouse Position when Clicked witch I have done..
but Now I'm trying to make the mPlayer move at a set speed over time.

How would do this with time? I can find my distance (MouseX - PlayerX) (MouseY - PlayerY) and set speed?
I'm new to game coding

My Code
(click to show/hide)
Title: Re: How to set Valocity with Frame rate?
Post by: Nexus on November 30, 2014, 11:08:11 am
First, you need to find a way of making game logic independent of the frame rate. A simple way is to use sf::RenderWindow::setFramerateLimit() and approximate the frame duration, more sophisticated ways include decoupling from rendering and logics, such as here (http://gafferongames.com/game-physics/fix-your-timestep/).

Concerning movement, you can basically use Euler integration. That means the position is updated by the velocity multiplied with the elapsed time in every frame.
// Convert mouse position to world coordinates
sf::Vector2i mousePos = ...; // get from sf::Event
sf::Vector2f target = window.mapPixelToCoords(mousePos);

// Compute player's movement
const float speed = ...;
sf::Vector2f position = ...; // current
sf::Vector2f direction = target - position;

// p += v * t
sf::Time dt = ...; // elapsed frame time, measure with sf::Clock
position += speed * thor::unitVector(direction) * dt.asSeconds();

thor::unitVector() computes a vector of unit length, see here (http://www.bromeon.ch/libraries/thor/v2.0/doc/_vector_algebra2_d_8hpp.html). In Thor, I've written a bunch of functions that simplify vector algebra, you can use them without building the library (i.e. header-only).
Title: Re: How to set Valocity with Frame rate?
Post by: Sanction on November 30, 2014, 08:30:03 pm
I'm following the SFML Game Development Book as I go, now I think I got my Fixed Delta Time correct this time. I think I might be able to finish this.

I just finished "C++ Through Game Programming" book(fourth Edition) about a month ago.. Working on SFML Game Development Book Learning and trying things as I go.

but how do I link to Thor Lib and also SFML Lib? & Will I have to make my own Thor Build with Cmake?

I have also seen SFGUI Will this work with SFML2.1? The forums seemed a little old.

Here is my Current code:
(click to show/hide)

 its a little messy right now but once I figure it out I can clean it up
Title: Re: How to set Valocity with Frame rate?
Post by: Nexus on November 30, 2014, 10:03:08 pm
but how do I link to Thor Lib and also SFML Lib? & Will I have to make my own Thor Build with Cmake?
You can build Thor yourself or use the nightly builds. In any case, please read the installation tutorial at my homepage which explains how to link Thor.
Title: Re: How to set Valocity with Frame rate?
Post by: Sanction on December 01, 2014, 03:34:08 am
There is no tutorial on how to use the nightly builds there. I did how ever try to build thor with cmake and got this error:

error C2679 : Binary '/' : no operator found which takes a right - hand operand of type 'sf::Time'

I'm still a newbie :(
Title: AW: How to set Valocity with Frame rate?
Post by: eXpl0it3r on December 01, 2014, 06:58:54 am
Use the nightly build just like an offical SDK, thus all the offical tutorials apply.

If you build Thor, you need to build SFML from source as well.
Title: Re: How to set Valocity with Frame rate?
Post by: Nexus on December 01, 2014, 11:29:32 am
error C2679 : Binary '/' : no operator found which takes a right - hand operand of type 'sf::Time'
Please search in the forum. A lot of people have this error because they don't read the instructions carefully and mix arbitrary versions.
Title: Re: How to set Valocity with Frame rate?
Post by: Sanction on December 01, 2014, 12:48:06 pm
Quote
Use the nightly build just like an offical SDK, thus all the offical tutorials apply.
I've never used a SDK that is self install.. only Android Development Kit for Java, some more information would be helpful.

I guess I'm confused on how to combine the two Libs.. I thought I could just take a nightly build and throw it into my current SFML build and link and go.. but the program wont link to that lib (#include "Thor" )
Title: Re: How to set Valocity with Frame rate?
Post by: Nexus on December 01, 2014, 01:24:55 pm
I've never used a SDK that is self install..
Nothing is self-install... There are either archives you can decompress and build with CMake, or distributions of binaries and headers.

some more information would be helpful.
For some reason we have written tutorials that explain every single step. There is also a FAQ that explains even more, and tons of forum threads with common problems.

As a programmer, you should really learn how to gather the required information on your own. Of course, you can still ask when things are unclear, but not before at least consulting the official sources. And if you do so, ask concrete and specific questions ;)
Title: Re: How to set Valocity with Frame rate?
Post by: Sanction on December 01, 2014, 10:40:36 pm
No I understand you guys get newbie questions all the time and trust me I try not to ask questions on the forums because its almost a slap in the face every time.
Title: Re: How to set Valocity with Frame rate?
Post by: Nexus on December 02, 2014, 11:06:33 am
I wrote my last post 1. to encourage you to read the resources like tutorials carefully and 2. to make you ask precise questions. Now I still don't know how we can help you, apart from reiterating what's already written in the tutorials.

Use either the master revisions of Thor and SFML or the latest nightly builds -- for both. If there are problems, describe exactly what you tried and at which point in the tutorial you're stuck. Give as much information as possible.
Title: Re: How to set Valocity with Frame rate?
Post by: Sanction on December 03, 2014, 12:20:48 am
Yes, Thanks, I will go back and have another go at it after I finish my assignments in Java. I really do appreciate all your help.
Title: Re: How to set Valocity with Frame rate?
Post by: Sanction on December 05, 2014, 05:19:15 am
I did it! it Works! I also included SFGUI so let the learning begin!