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

Author Topic: Moving sprite in direction of rotation.  (Read 12937 times)

0 Members and 2 Guests are viewing this topic.

Predator106

  • Newbie
  • *
  • Posts: 43
    • View Profile
Moving sprite in direction of rotation.
« on: August 03, 2008, 06:25:19 pm »
Hello, I was wondering, because I cannot figure out how to get this to work correctly, when I was using XNA the rotation it had was not in degrees but i believe went from 0 to 6 or something, and was a floatational value (I know, it's the dumbest thing I've ever heard). But now that SFML uses 360 float (much better), it appears that the method I had used previously, does not work. I don't know if I did something wrong with the XNA-SFML conversion, but it is completely irregular (i.e. I will turn a small amount and it will reverse (or random) to a direction).

This is the code I had used in XNA.....

Code: [Select]

/*if (Keyboard.GetState().IsKeyDown(Keys.W) |
                                                                         Keyboard.GetState().IsKeyDown(Keys.S))
                                                                         {
                                                                         //Had to swap the cos, with the sin, and make the cos negative
                                                                         //otherwise it would be walking the left side of the player,
                                                                         //which just wouldn't make any sense.
                                                                         PlayerVelocityAdditive.Y = -(float) Math.Cos(Player_CurrentRotation);
                                                                         PlayerVelocityAdditive.X = (float) Math.Sin(Player_CurrentRotation);
                                                                         PlayerVelocityAdditive *= Player_VelocityAccelerationMultiplier;
                                                                         //modelVelocityAdd.X *= PlayerVelocityAccelerationMultiplier;
                                                                         if (Keyboard.GetState().IsKeyDown(Keys.W))
                                                                             {
                                                                             Player_CurrentVelocity += PlayerVelocityAdditive;
                                                                             }
                                                                         else //else its S
                                                                             {
                                                                             Player_CurrentVelocity -= PlayerVelocityAdditive;
                                                                             }
                                                                         }*/

//...............
//                Player_CurrentPosition += Player_CurrentVelocity;//add the current velocity, to the current pos.





And this is the code that I have converted to SFML....

Code: [Select]

  sf::Vector2f Car_Velocity_Additive ;
                            Car_Velocity_Additive.y =  -(float) cos (Sprite.GetRotation() )*3;
                            Car_Velocity_Additive.x = (float) sin(Sprite.GetRotation() )*3;
                            if (App.GetInput().IsKeyDown (sf::Key::W))
                            {
                                          //Car_Velocity.y += 10;
                                          Car_Velocity += Car_Velocity_Additive;

                            }

                            if (App.GetInput().IsKeyDown (sf::Key::S))
                            {
                                          // Car_Velocity.y -= 10;
                                          Car_Velocity -= Car_Velocity_Additive;
                            }
                            //Sprite.Move (0, -100 * ElapsedTime);
                            Sprite.Move (Car_Velocity.x *ElapsedTime, Car_Velocity.y*ElapsedTime);



Hopefully I didn't make a mistake in copy-paste. But anyways, I don't know why it's not working... I've tried swapping cos/sin, and using a negative in a diff place (or not at all(or both places)). Very confused I am(heh).

Anyways, hopefully you guys can help me, as this is really annoying, I do recall having the same problem with XNA, but it was because of me needing to swap the math functions or something to that degree.

I'm not entirely sure why we use these math functions, or how they do exactly (or should) what they do, however.

Daazku

  • Hero Member
  • *****
  • Posts: 896
    • View Profile
Moving sprite in direction of rotation.
« Reply #1 on: August 03, 2008, 06:38:03 pm »
Sprite.SetRotation(Sprite.GetRotation()+1);

Just check the doc and the tutorials...
Pensez à mettre le tag [Résolu] une fois la réponse à votre question trouvée.
Remember to add the tag [Solved] when you got an answer to your question.

Predator106

  • Newbie
  • *
  • Posts: 43
    • View Profile
Moving sprite in direction of rotation.
« Reply #2 on: August 03, 2008, 07:26:51 pm »
Huh?
No, I'm not trying to rotate my sprite... I've already figured that out, I want the sprite to move forward in the direction that it is rotated. Thus requiring trig functions...

Daazku

  • Hero Member
  • *****
  • Posts: 896
    • View Profile
Moving sprite in direction of rotation.
« Reply #3 on: August 03, 2008, 08:08:20 pm »
Sorry:

Quote
cos    function

<cmath>

Compute cosine

Returns the cosine of an angle of x radians

Parameters

x
    Floating point value representing an angle expressed in radians. .


Code: [Select]

#include <cmath>

#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    // Create main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Test");

    sf::Shape shape = sf::Shape::Line(0, 0, 200, 0, 10, sf::Color::Red);
    shape.SetPosition(300, 300);
    shape.SetCenter(100, 5); //Its important to set the center :D

    // Start game loop
    while (window.IsOpened())
    {
        window.Clear();
        // Process events
        sf::Event event;
        while (window.GetEvent(event))
        {
            // Close window : exit
            if (event.Type == sf::Event::Closed)
            {
                window.Close();
            }
            else if (event.Type == sf::Event::KeyPressed)
            {
                if (event.Key.Code == sf::Key::Escape)
                {
                    window.Close();
                }
                else if (event.Key.Code == sf::Key::Up)
                {
                    shape.Move( cos(shape.GetRotation()*3.14159265/180)*3, sin(shape.GetRotation()*3.14159265/180)*-3 );
                }
                else if (event.Key.Code == sf::Key::Down)
                {
                    shape.Move( cos(shape.GetRotation()*3.14159265/180)*-3, sin(shape.GetRotation()*3.14159265/180)*3);
                }
                else if (event.Key.Code == sf::Key::Left)
                {
                    shape.SetRotation(shape.GetRotation()+1);
                }
                else if (event.Key.Code == sf::Key::Right)
                {
                    shape.SetRotation(shape.GetRotation()-1);
                }
            }
        }
        window.Draw(shape);
        window.Display();
    }

    return EXIT_SUCCESS;
}


PS: Long life to std::cout :D. (I saw that the result value was absolutly wrong using cos/sin with cmath vs window calc.)
Pensez à mettre le tag [Résolu] une fois la réponse à votre question trouvée.
Remember to add the tag [Solved] when you got an answer to your question.

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Re: Moving sprite in direction of rotation.
« Reply #4 on: August 03, 2008, 11:20:57 pm »
Quote from: "Predator106"
Hello, I was wondering, because I cannot figure out how to get this to work correctly, when I was using XNA the rotation it had was not in degrees but i believe went from 0 to 6 or something, and was a floatational value (I know, it's the dumbest thing I've ever heard). But now that SFML uses 360 float (much better), it appears that the method I had used previously, does not work. I don't know if I did something wrong with the XNA-SFML conversion, but it is completely irregular (i.e. I will turn a small amount and it will reverse (or random) to a direction).


Without looking at anything else, you need to read about radians and degrees.  XNA is not 0-6 it's 0 to 2 * pi or ~6.28.  This is actually much more mathematically convenient and "correct" (at a lower level) than degrees (0 to 360).

Edit:  Almost any math library including std C expects numbers to be in radians, not degrees.  95% that's your problem.

Daazku

  • Hero Member
  • *****
  • Posts: 896
    • View Profile
Re: Moving sprite in direction of rotation.
« Reply #5 on: August 03, 2008, 11:34:44 pm »
Quote from: "quasius"
Quote from: "Predator106"
Hello, I was wondering, because I cannot figure out how to get this to work correctly, when I was using XNA the rotation it had was not in degrees but i believe went from 0 to 6 or something, and was a floatational value (I know, it's the dumbest thing I've ever heard). But now that SFML uses 360 float (much better), it appears that the method I had used previously, does not work. I don't know if I did something wrong with the XNA-SFML conversion, but it is completely irregular (i.e. I will turn a small amount and it will reverse (or random) to a direction).


Without looking at anything else, you need to read about radians and degrees.  XNA is not 0-6 it's 0 to 2 * pi or ~6.28.  This is actually much more mathematically convenient and "correct" (at a lower level) than degrees (0 to 360).

Edit:  Almost any math library including std C expects numbers to be in radians, not degrees.  95% that's your problem.


You know you can read previous post before answer.... I said that was a radian / degree problem...
Pensez à mettre le tag [Résolu] une fois la réponse à votre question trouvée.
Remember to add the tag [Solved] when you got an answer to your question.

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Re: Moving sprite in direction of rotation.
« Reply #6 on: August 04, 2008, 09:29:32 pm »
Quote from: "Daazku"
You know you can read previous post before answer.... I said that was a radian / degree problem...


I provided information that you did not.

 

anything