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

Author Topic: [SOLVED]Smart Moving Problem  (Read 9692 times)

0 Members and 1 Guest are viewing this topic.

sjaustirni

  • Jr. Member
  • **
  • Posts: 95
    • View Profile
Re: Smart Moving Problem
« Reply #15 on: May 07, 2017, 11:36:04 am »
I would like to point out that ship velocity and ship rotation are 2 different things in the real world. So you should separate them in your model as well.

As you may have noticed, I am not a big fan of posting production-ready code. I will rather guide through the process, giving you hints and nudges to the right direction ;-)

Firstly, the ship rotation. You change it with Left and Right keys. For that purpose you can use for instance sf::Transformable::rotate(angle)
ship.rotate(dt*omega);
(Note that this is a bit of oversimplification, as in the real world you rotate yourself using your side thrusts or similiar, but for your purposes, I believe it suffices)

Secondly, there's ship velocity which you should store as a 2D vector. You change it with Up and Down keys. In code, this is achieved by adding the ship acceleration to its velocity every frame Up and Down keys are pressed down:
ship.velocity += ship.acceleration
I see what you're saying - how do you get ship.acceleration?
Out of the ship angle, obviously (using sf::Transformable::getRotation()).
OK, you say, but how exactly?
Well, this is when vector math kicks in. I'll leave you here with this SO post.

Note: GameBear posted de facto the same solution, I just hadn't noticed before he made me aware of it :)
« Last Edit: May 07, 2017, 06:52:12 pm by sjaustirni »

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: Smart Moving Problem
« Reply #16 on: May 08, 2017, 04:12:25 pm »
GameBear I have some questions ...

First, why did you use m_Key and didn't used Keyboard::isKeyPressed(Keyboard::Key) ?
Second, I tried your code and the spaceship only rotate, but if I press up nothing happens.
PS: I am wrong or you mistake Val with Vel ?
PPS : I wrote the code with my hands( I would be a hypocritical if I say I didn't use your code,I wrote it with my things like m_spaceship to SSpaceship(the sprite) and others) and I wrote Vel not Val, it is right ?
Guess Who's Back ?

GameBear

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
    • Email
Re: Smart Moving Problem
« Reply #17 on: May 08, 2017, 04:42:24 pm »
Ah, you are correct, Val should be Vel..
About keyboard, that's a preference from my side, I find it faster to type over multiple usages.

Please, can you copy pace your code Here.   I'll see if I can spot the error..just tried the code myself, and it works fine :)
« Last Edit: May 08, 2017, 05:02:37 pm by GameBear »
string message = "some random dude";
cout << "I'm just " << message;

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: Smart Moving Problem
« Reply #18 on: May 08, 2017, 05:08:50 pm »
Here is my code... I have another question : When I press Up the spaceship goes to right instead of ahead(it is oriented to up(north) from start) and the if I press Up the spaceship continues to go, it doesn't stops, why ?
#include <SFML/Graphics.hpp>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
using namespace sf;
int main()
{
    float m_xVel = 0, m_yVel = 0, m_accel = 0.001;
    Keyboard m_Key;
    int x = 0;
    int y = 0;
    const float PI = 3.14159265;
    RenderWindow window(VideoMode(800,600),"Endless");
    window.setFramerateLimit(60);
    window.setVerticalSyncEnabled(1);
    Texture TSpaceship;
    if(!TSpaceship.loadFromFile("Spaceship.png"))
    {
        cout<<"Error"<<endl;
    }
    TSpaceship.setSmooth(1);
    Sprite SSpaceship;
    SSpaceship.setTexture(TSpaceship);
    SSpaceship.setOrigin(0,0);
    SSpaceship.setPosition(400,300);
    while(window.isOpen())
    {
        Event event;
        while(window.pollEvent(event))
        {
            if(event.type == Event::Closed)
            {
                window.close();
            }
        }
        if (m_Key.isKeyPressed(Keyboard().Right)) {
                SSpaceship.setRotation(SSpaceship.getRotation() + 1);
        }
        if (m_Key.isKeyPressed(Keyboard().Left)) {
                SSpaceship.setRotation(SSpaceship.getRotation() - 1);
        }
        if (m_Key.isKeyPressed(Keyboard().Up)) {
                m_xVel += (cos(SSpaceship.getRotation() / 180 * 3.1415)) * m_accel;
                m_yVel += (sin(SSpaceship.getRotation() / 180 * 3.1415)) * m_accel;
        }
        SSpaceship.setPosition(SSpaceship.getPosition().x + m_xVel, SSpaceship.getPosition().y + m_yVel);
        window.clear();
        window.draw(SSpaceship);
        window.display();
    }
}
 
« Last Edit: May 08, 2017, 05:16:22 pm by Boanc »
Guess Who's Back ?

GameBear

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
    • Email
Re: Smart Moving Problem
« Reply #19 on: May 08, 2017, 05:56:43 pm »
Okay, I just tested your code, and it works as it should.

But I think i know where you get confused.

360 or 0 degrees (Start position) is horizontal facing right like this arrow -->
or said in another way, if your ship image is facing north, you should rotate it 90 degrees so it faces east in the image file.

Secondly, as mentioned in my post, this is an asteroids like movement, the longer you hold UP, the faster the ship will go.

look at this code part:


        if (m_Key.isKeyPressed(Keyboard().Up)) {
                m_xVel += (cos(SSpaceship.getRotation() / 180 * 3.1415)) * m_accel;
                m_yVel += (sin(SSpaceship.getRotation() / 180 * 3.1415)) * m_accel;
        }
        SSpaceship.setPosition(SSpaceship.getPosition().x + m_xVel, SSpaceship.getPosition().y + m_yVel);



each time the UP key is pressed, m_xVel and m_yVel is changed.
what happens from the start is this,
-the ship is standing still (m_xVel = 0 and m_yVel = 0) and is rotated 360 degrees.
- UP is pressed, and because of this, the system adds a value to m_xVel and m_yVel (that is the cos and sin code up there) for simplicitys sake, lets say they change to m_xVel = 1, and m_yVel = 0;
-Then m_xVel and m_yVel is added to the ships current position. moving the ship på 1 on the X axis.
-Each update from now on, m_xVel and m_yVel will be the same, and will be added to the ships position, moving it at 1 per update.
-IF UP is pressed again, m_xVel and m_yVel will again get a new value, this value will be added to the old value.
so if whe have not rotated, the new values will be m_xVel = 2, and m_yVel = 0;
-The ship will now be updated and moved twice as fast in the X direction (2 instead of 1)
-IF we then rotate 90 degrees clock vice (the ship facing down) and press UP again. the m_xVel and m_yVel will be changed again, but this time, it would be yVel that gets 1 added to it.
so the nev values are now m_Xvel = 2, and m_yVel = 1.
-The ship is now updated, and moves +2 X and +1 Y (or East-South-East if you will)

Things you should try to make it more easy for yourself to see.
Try to change the value of m_accel to 0.01 or even 0.1.

After the if Key pressed but before SSpaceship.setPosition
Try and add:

      m_xVel = m_xVel * 0.99;
      m_yVel = m_yVel * 0.99;

This will "Stop" the space ship after some time.
See what happens if you write * 0.5 or * 0.999 instead...

Try to rotate your image in all directions in the editor.
What you should notes is that ite does not effect the code at all, just the "looks"

After SSpaceship.setPosition
try to add:
      if (SSpaceship.getPosition().x > 800) SSpaceship.setPosition(0, SSpaceship.getPosition().y);
      else if (SSpaceship.getPosition().x < 0) SSpaceship.setPosition(800, SSpaceship.getPosition().y);
      if (SSpaceship.getPosition().y > 600) SSpaceship.setPosition(SSpaceship.getPosition().x, 0);
      else if (SSpaceship.getPosition().y < 0) SSpaceship.setPosition(SSpaceship.getPosition().x, 600);

This will keep the space ship looping inside the window.

Edit, lastly, I notes that you have two values you never use, int x and int y.
and that you have made a definition of PI.
If you look at your code, there is a place where an approximation of PI is used, if you wish, you could just as well go ahead and and exchange that approximation for your own PI, that would both be more correct mathematical, but also more easy to write in the future.
« Last Edit: May 08, 2017, 06:07:11 pm by GameBear »
string message = "some random dude";
cout << "I'm just " << message;

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: Smart Moving Problem
« Reply #20 on: May 10, 2017, 05:13:16 pm »
Ok, I've finally finished the code, ;D and I modified the acceleration(if it is bigger it makes the car move faster, logically) , the vel(I really don't know why it does like that, I mean why the difference between 0.99 and 0.999 or 0.5, the longer(bigger) the number is the faster it stops), the MOST IMPORTANT thing is to rotate the spaceship to RIGHT(rotate it from the .png(I mean rightclick on the photo and click rotate clockwise,DON'T do it with rotate() function this won't work).I will post my code here and explain it for all newbies like me :P.
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <math.h>
#include <vector> //I tried to this many times so I don't know if you need this.
using namespace std;
using namespace sf;
int main()
{
    float m_xVel = 0, m_yVel = 0, m_accel = 0.1;  //Setting the vel and acceleration
    Keyboard m_Key;
    RenderWindow window(VideoMode(800,600),"Endless");  //Creating the window
    window.setFramerateLimit(60);  //Limiting the framelimit to 60
    window.setVerticalSyncEnabled(1); //Enable the VSync
    Music music;
    if(!music.openFromFile("Music.ogg"))
    {
        cout<<"Error"<<endl;
    }
    music.play();     //Playing some nice music
    music.setLoop(1);     //If it ends it will start again
    music.setVolume(10);  //I don't know but .setVolume() doesn't work for me, I mean it doesn't changes the volume of the music
    Texture TStars;                   //Setting the Texture for the Background
    if(!TStars.loadFromFile("Stars.png"))
    {
        cout<<"Error"<<endl;
    }
    TStars.setSmooth(1);     //Smoothing it to look nicer
    Sprite SStars;  
    SStars.setTexture(TStars); //Setting the Texure to the Sprite
    Texture TSpaceship; //Setting the Texture
    if(!TSpaceship.loadFromFile("Spaceship.png"))
    {
        cout<<"Error"<<endl;
    }
    TSpaceship.setSmooth(1);  //Smoothing it
    Sprite SSpaceship;
    SSpaceship.setTexture(TSpaceship);      //Setting the Texure to the Sprite
    SSpaceship.setOrigin(0,0);    //Setting the Origin
    SSpaceship.setPosition(400,300);   //Setting the position in the middle of the window
    while(window.isOpen())
    {
        Event event;
        while(window.pollEvent(event))
        {
            if(event.type == Event::Closed)
            {
                window.close();     //Checking if the User clicks "X", if so the window will close
            }
        }
        if (m_Key.isKeyPressed(Keyboard().Right)) {
                SSpaceship.setRotation(SSpaceship.getRotation() + 1); //If right is pressed the spaceship will rotate to right
        }
        if (m_Key.isKeyPressed(Keyboard().Left)) {
                SSpaceship.setRotation(SSpaceship.getRotation() - 1);   //If left is pressed the spaceship will rotate to left
        }
        if (m_Key.isKeyPressed(Keyboard().Up)) {
                m_xVel += (cos(SSpaceship.getRotation() / 180 * 3.1415)) * m_accel;
                m_yVel += (sin(SSpaceship.getRotation() / 180 * 3.1415)) * m_accel;
//If up is pressed the spaceship will go ahead at the same angle
        }
        m_xVel = m_xVel * 0.99;  //Stop the ship after some time
        m_yVel = m_yVel * 0.99;
        SSpaceship.setPosition(SSpaceship.getPosition().x + m_xVel, SSpaceship.getPosition().y + m_yVel); //Setting the Position(almost the most important thing here,without it the spaceship won't move)
        if (SSpaceship.getPosition().x > 800 + 74)
        SSpaceship.setPosition(0, SSpaceship.getPosition().y);
       else if (SSpaceship.getPosition().x < 0 - 74 )
        SSpaceship.setPosition(800, SSpaceship.getPosition().y);
       if (SSpaceship.getPosition().y > 600 + 84)
       SSpaceship.setPosition(SSpaceship.getPosition().x, 0);
       else if (SSpaceship.getPosition().y < 0 - 84)
        SSpaceship.setPosition(SSpaceship.getPosition().x, 600);
//This keeps the spaceship looping inside the window, I think it's called world-wrapping
        window.clear();  //Clear the window
        window.draw(SStars);  //Drawing the Stars(Background)
        window.draw(SSpaceship); //Drawing the Spaceship(Player)
        window.display();  //Display it
    }
}
 
Guess Who's Back ?

GameBear

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
    • Email
Re: Smart Moving Problem
« Reply #21 on: May 10, 2017, 05:41:03 pm »
Great work :)
Just looked it over, and it works great.

about why the ship stops when you multiply m_yVel and m_xVel with 0.99 or something:
as you know, the two Vel is the velocity, or speed of the object if you will.
if the objects xVel is 10, and you then multiply it by 0.9, it becomes 9.
next time you multiply it, it will become 8.1
then it will become 7,29, then 6,561.
the reason is that each time, you multiply vel by 0.9, its the same as removing 10% of the speed, multiplying by 0.99 is removing 1%, and multiplying by 1 is removing nothing. basically multiplying by 1 means keep 100%, 0,5 means keep 50% and 0.25 means keep 25%

I also notes you write this line:
#include <vector> //I tried to this many times so I don't know if you need this.
 
Best way to figure that out is to just delete the line and see if anything happens, if it does, place it right back in ;)
string message = "some random dude";
cout << "I'm just " << message;

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: Smart Moving Problem
« Reply #22 on: May 11, 2017, 05:35:53 pm »
Thank you all ! You are the BEST ;)! Oh, and about #include <vector> I didn't had codeblocks opened to verify that and I was too lazy to open it  ;D .

PS:Thanks for all the help, sorry I confused you for a while :P.
PPS: GameBear and I am a fan of Dragon Ball too,I love your "logo". :)
Guess Who's Back ?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Smart Moving Problem
« Reply #23 on: May 12, 2017, 12:14:39 am »
I also notes you write this line:
#include <vector> //I tried to this many times so I don't know if you need this.
 
Best way to figure that out is to just delete the line and see if anything happens, if it does, place it right back in ;)
I disagree with this advice. Something you include may include it for you but then if, in the future, you stop needing the other thing, you may still need it. It's best to explicitly include it if you need it regardless of if its already included elsewhere.

You include "<vector>" if you wish to use a vector. That is, if you have a std::vector in your code, you should include it.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

GameBear

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
    • Email
Re: Smart Moving Problem
« Reply #24 on: May 12, 2017, 01:34:59 pm »
I also notes you write this line:
#include <vector> //I tried to this many times so I don't know if you need this.
 
Best way to figure that out is to just delete the line and see if anything happens, if it does, place it right back in ;)
I disagree with this advice. Something you include may include it for you but then if, in the future, you stop needing the other thing, you may still need it. It's best to explicitly include it if you need it regardless of if its already included elsewhere.

You include "<vector>" if you wish to use a vector. That is, if you have a std::vector in your code, you should include it.

I guess i did not conveyed what i meant properly.
I meant that if you do not know if the code "as is" need the inclusion. that is, if your current code uses that inclusion.
The fastes way to figure that out is to just delete the line, that will tell you if it is used.

*Also, this advice only counts for inclusions. doing the same to other inline code may not show it self as an error right away...
string message = "some random dude";
cout << "I'm just " << message;

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: Smart Moving Problem
« Reply #25 on: May 14, 2017, 08:03:00 am »
Ok let's make light, you don't need to include "#include <vector>" and I wrote that comment because I was too lazy to check if you need it or not.And I've included it in the first place because I tried to do the "smart moving" with vectors(I tried this from a tutorial).
Guess Who's Back ?

 

anything