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

Author Topic: Bullet positioning (updated)  (Read 2141 times)

0 Members and 1 Guest are viewing this topic.

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Bullet positioning (updated)
« on: November 29, 2014, 09:25:45 am »
Let's say I have a character and I rotate it.
How do I determine where to exactly spawn the bullet?
update:(comments)
« Last Edit: December 06, 2014, 06:57:17 am by BeautiCode »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
AW: Bullet positioning
« Reply #1 on: November 29, 2014, 10:31:40 am »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Bullet positioning
« Reply #2 on: December 06, 2014, 04:16:34 am »
I still don't quite understand it. Here's my code:
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/CircleShape.hpp>
int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 600), "Shooter");
        sf::CircleShape rs;
        sf::RectangleShape sh;
        rs.setRadius(50);
        sh.setSize(sf::Vector2f(5, 5));
        sh.setFillColor(sf::Color::Red);
        rs.setPosition(800.0 / 2, 600.0 / 2);
        rs.setOrigin(rs.getLocalBounds().width / 2.0f, rs.getLocalBounds().height / 2.0f);
        while (window.isOpen())
        {
                sf::Event e;
                window.clear(sf::Color::Black);
                while (window.pollEvent(e))
                {
                        switch (e.type)
                        {
                        case sf::Event::KeyPressed:
                        {
                                switch (e.key.code)
                                {
                                case sf::Keyboard::Left:
                                {
                                        rs.setRotation(rs.getRotation() - 1);
                                }break;
                                case sf::Keyboard::Right:
                                {
                                        rs.setRotation(rs.getRotation() + 1);
                                }
                                }
                        }break;
                        case sf::Event::Closed:
                        {
                                window.close();
                        }break;
                        }
                }
                sh.setPosition(cos(rs.getRotation()) + rs.getPosition().x, sin(rs.getRotation()) + rs.getLocalBounds().height + rs.getPosition().y);
                window.draw(rs);
                window.draw(sh);
                window.display();
        }
}
 
I'm trying to make the bullet rotate with the circle just to get a grip on the concept. But the bullet is just sitting there shaking when I rotate it.
« Last Edit: December 07, 2014, 11:34:58 am by eXpl0it3r »

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Bullet positioning (updated)
« Reply #3 on: December 06, 2014, 01:00:59 pm »
Hint: SFML uses degree and cos wants radian.
SFML / OS X developer

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Bullet positioning (updated)
« Reply #4 on: December 07, 2014, 01:16:43 am »
I added:
Code: [Select]
const float PI = 3.14159265358979323846;And changed:
Code: [Select]
sh.setPosition(cos(rs.getRotation() * PI / 180) + rs.getPosition().x, sin(rs.getRotation() * PI / 180) + rs.getPosition().y + rs.getLocalBounds().height);But problem hasn't changed much.


TheFloatingBrain

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Bullet positioning (updated)
« Reply #5 on: December 07, 2014, 06:04:14 am »
Think about the numbers and what is happening to them in a loop. It may also be good to get a good grasp on vectors and trigonometry.


Right triangle
           \
            \
             \/
             
             |\
opposite ->  | \  <-hypotenuse
             |__\
               ^
               | adjacent
 


sin - takes in an angle, and returns a ratio that can exist in the sides, if you multiply this ratio by the length of the hypotenuse, you get the length of the side, sin = opposite / hypotenuse

cos - like sin, cos takes in an angle, and returns a ratio that can exist in the sides, again, if you multiply this ratio by the length of the hypotenuse, you get the length of the side, sin = adjacent / hypotenuse

tan - opposite / adjacent

sin^-1 or asin - this does the opposite of sin, it takes in a ratio and spits out an angle in radians

cos^-1 or acos - this does the opposite of cos, it takes in a ratio and returns an angle in radians

tan^-1 or atan - the opposite of tan adjacent / opposite
 

Think of them like little programming functions.

Now think of these in a graph, and where the numbers are going, like so
f( x ) = x^2, x@1 = 1, x@2 = 4, x@3 = 9...
Then think of what you are going to do with those numbers, so think for example of what cos/sin do, then use that data in a mathematical formula, and think of that mathematical formula along a graph, and try to figure out if those numbers are what you want, or what you want to do with them, and if you are devising a formula to work well with them.

I don't know if you know trig or how good you are with math, I hope I gave you relevant advice!

Peace!
« Last Edit: December 07, 2014, 11:34:43 am by eXpl0it3r »