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

Author Topic: Sprite rotation  (Read 3355 times)

0 Members and 1 Guest are viewing this topic.

lane

  • Newbie
  • *
  • Posts: 29
    • View Profile
Sprite rotation
« on: April 03, 2016, 02:15:08 am »
Hello! I am looking for a solution to have my sprite rotate correctly when I am going in a diagonal direction (holding up key and right key, down key and left key, etc.). I have a ship sprite the initially faces right, when I hit a single arrow key it will rotate appropriately to face that direction, but I can't find a way to get the diagonals to work.. I have tried an if statement with up key and right key then some logic to turn it, but it doesn't quite work. Here is my code for my ship's movement:
//Character movement
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
                        if(ship.getRotation() != 315){
                                        if(ship.getRotation() > 135){
                                                ship.rotate(2);
                                        }
                                       
                                        if(ship.getRotation() <= 135 || (ship.getRotation() >= 0 && ship.getRotation() < 315)){
                                                ship.rotate(-2);
                                        }
                                }
                               
                        }
                       
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
                                if(ship.getRotation() != 270){
                                        if((ship.getRotation() > 270) || (ship.getRotation() <= 90)){
                                                ship.rotate(-2);
                                        }
                               
                                        if((ship.getRotation() < 270) && (ship.getRotation() > 90)){
                                               
                                                ship.rotate(2);
                                        }
                                       
                                }
                                ship.move(sf::Vector2f(0,-speed[2]));
                        }
                       
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
                                if(ship.getRotation() != 90){
                                        if((ship.getRotation() > 90) && (ship.getRotation() <= 180)){
                                                ship.rotate(-2);
                                        }
                                       
                                        if((ship.getRotation() < 90) || (ship.getRotation() > 180)){
                                                ship.rotate(2);
                                        }
                                }
                                ship.move(sf::Vector2f(0,speed[2]));
                        }
                       
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
                                if(ship.getRotation() != 180){
                                        if(ship.getRotation() > 180){
                                                ship.rotate(-2);
                                        }
                                       
                                        if(ship.getRotation() < 180){
                                                ship.rotate(2);
                                        }
                                }
                                ship.move(sf::Vector2f(-speed[2],0));
                        }
                       
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
                                if(ship.getRotation() != 0){
                                        if((ship.getRotation() > 0) && (ship.getRotation() < 180)){
                                                ship.rotate(-2);
                                        }
                                       
                                        if(ship.getRotation() >= 180){
                                                ship.rotate(2);
                                        }
                                }
                                ship.move(sf::Vector2f(speed[2],0));
                        }

I have a feeling it is clashing with one of the other if statements, any suggestions on this?  :D

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
AW: Sprite rotation
« Reply #1 on: April 03, 2016, 07:18:18 pm »
You might want to look into what else (or else if) does. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lane

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Sprite rotation
« Reply #2 on: April 03, 2016, 09:02:29 pm »
You're right, lol!
Thank You! 8)

Quick question though: if I were to make this ship shoot something, how would I go about doing that? Would I have to position the sprite/picture where the ship's location is then draw it and move it according to the ships orientation?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Sprite rotation
« Reply #3 on: April 04, 2016, 12:25:46 am »
if I were to make this ship shoot something, how would I go about doing that? Would I have to position the sprite/picture where the ship's location is then draw it and move it according to the ships orientation?
Sounds like a good starting point. Try it! ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

lane

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Sprite rotation
« Reply #4 on: April 05, 2016, 05:52:39 am »
How do I go about moving the sprite according to the ship's rotation? Is there a formula for something like this? I have no target I'm shooting at, just open space. Any help on this would be AWESOME!

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Sprite rotation
« Reply #5 on: April 05, 2016, 06:11:53 am »
You can use cos and sin to get the direction vector.
sf::Vector2f direction;
direction.x = cos(angleInRadians);
direction.y = sin(angleInRadians);
Then you multiply the direction by your speed (speed[2])
sf::Vector2f movement = direction * speed[2];
And you move
ship.move(movement);

lane

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Sprite rotation
« Reply #6 on: April 06, 2016, 05:15:40 am »
That is Awesome! I think I'm starting to (very slightly) get vectors. How can I have that only update when I hit space? My issue is that when I shoot, it goes the direction I want, but now it is following my ship's rotation and moves with my ship if I move. I would like this bullet to go a specific distance and not move after 'shot' even though my ship will move after

lane

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Sprite rotation
« Reply #7 on: April 10, 2016, 02:45:36 am »
How would I be able to create a circle 'range' for my bullet? For example, I have a ship that shoots a bullet and I want the bullet to set its position back at the ship when it exceeds a circle shape. I know how to create the bounds for a square or rectangle, but I have no clue how to do that with a circle.

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Sprite rotation
« Reply #8 on: April 10, 2016, 03:46:17 am »
It's even easier than rectangles.

Think about how the size of a circle is determined. From there, can you figure out if a point is inside of or outside of the circle?

With the ship and the bullet, you know positions, and once you decide on a max distance from the ship for the bullet, you also know that.

lane

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Sprite rotation
« Reply #9 on: April 10, 2016, 04:17:01 am »
Quote
Think about how the size of a circle is determined. From there, can you figure out if a point is inside of or outside of the circle?

radius?? I'm lost  :-[

sf::CircleShape circle(100);

If I knew how to find that I would be fine...

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
AW: Sprite rotation
« Reply #10 on: April 10, 2016, 11:43:26 am »
If the distance between the point and the circle position is smaller or equal than the circles radius, it's inside the circle.
It's just simple math. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything