SFML community forums

Help => General => Topic started by: lane on April 03, 2016, 02:15:08 am

Title: Sprite rotation
Post by: lane 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
Title: AW: Sprite rotation
Post by: eXpl0it3r on April 03, 2016, 07:18:18 pm
You might want to look into what else (or else if) does. ;)
Title: Re: Sprite rotation
Post by: lane 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?
Title: Re: Sprite rotation
Post by: Hapax 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! ;)
Title: Re: Sprite rotation
Post by: lane 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!
Title: Re: Sprite rotation
Post by: G. 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);
Title: Re: Sprite rotation
Post by: lane 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
Title: Re: Sprite rotation
Post by: lane 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.
Title: Re: Sprite rotation
Post by: dabbertorres 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.
Title: Re: Sprite rotation
Post by: lane 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...
Title: AW: Sprite rotation
Post by: eXpl0it3r 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. ;)