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

Author Topic: noob question: velocity [solved]  (Read 2062 times)

0 Members and 1 Guest are viewing this topic.

chmmr

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
noob question: velocity [solved]
« on: November 24, 2012, 11:23:31 pm »
Hi i am making a platformer.  I want my character to have some velocity and be able to measure his velocity.  To measure the velocity I use the formula

(current position - position at last frame) / framerate

I set the framerate equal to 30, and the current position is just sprite.GetPosition().x but how can I find the position of x in the previous frame.

I'm sorry if this question is too easy or has been asked before but i looked a lot of places and couldn't find it.
« Last Edit: November 29, 2012, 12:59:10 am by chmmr »

cf

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: noob question: velocity
« Reply #1 on: November 24, 2012, 11:47:17 pm »
This seems like a somewhat strange/backwards way to go about it. The sprites don't move by themselves, so somewhere in you're code you know both where they were (since you moved them) and how much they moved from that place (again, since you moved them) -- and you could remember both these pieces of information.

In a platformer, you're usually controlling the velocity of objects (and moving them accordingly) -- not measuring it after the fact.

As you've discovered, only knowing the current position and the framerate (with no other information about how the movement happened) is insufficient to known the previous position.

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: noob question: velocity
« Reply #2 on: November 25, 2012, 12:08:33 am »
In games with movement and velocity, you either need to store position and velocity OR current position and previous position. (The latter is used in something called Verlet integration.) So, the simple answer, is that when you update currentPosition, first copy it into lastPosition. You can then measure velocity.

Code: [Select]
struct Guy {
  sf::Vector2f currentPosition, lastPosition;
};

chmmr

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: noob question: velocity
« Reply #3 on: November 25, 2012, 01:26:51 am »
I guess it is sort of backwards.  I'll try creating the current/previous position, and see how that works.

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: noob question: velocity
« Reply #4 on: November 25, 2012, 06:16:02 am »
There's more than one method to it in the end, I use polar vectors because I like the way they work mathematically, You just calculate an angle and increase/decrease the given radius with events. In the case of a platformer you can use that by making it work with your physics simulator to give your game the control over what the player can and cannot do in different circumstances.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

chmmr

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: noob question: velocity
« Reply #5 on: November 29, 2012, 12:54:37 am »
I got some working code but it's a little wonky, in a fun sort of way.  I'm posting it here so I can remember it later.

        // collision with ground
        if (guy.sprite.GetPosition().y > 520)
        {
            guy.sprite.SetY(520);
            airtime.Reset();
           
        }
        if (guy.sprite.GetPosition().y < 520 && hasJumped == false)
            guy.sprite.Move(0, 10 * airtime.GetElapsedTime());
        if (guy.sprite.GetPosition().y < 520 && hasJumped == true)
            guy.sprite.Move(0, -10 / airtime.GetElapsedTime() + 10 * airtime.GetElapsedTime() * airtime.GetElapsedTime());
        if (App.GetInput().IsKeyDown(sf::Key::Up) && guy.sprite.GetPosition().y > 519.5)
        {
            hasJumped = true;
            guy.sprite.Move(0, -1);
        }
 

[attachment deleted by admin]

chmmr

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: noob question: velocity
« Reply #6 on: November 29, 2012, 12:58:29 am »
sorry for the double post, this is the code that works, without being wonky.

[attachment deleted by admin]

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10916
    • View Profile
    • development blog
    • Email
AW: noob question: velocity [solved]
« Reply #7 on: November 29, 2012, 09:10:50 am »
I advise you to use SFML 2, because SFML 1.6 haan't received a commit in over 2.5y, has few nasty bugs and lacks a lot of nice features (VertexArray, RenderTexture...). ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything