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

Author Topic: Moving a point around a falling circle  (Read 2480 times)

0 Members and 1 Guest are viewing this topic.

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Moving a point around a falling circle
« on: December 14, 2020, 10:37:57 am »
Hello,

This is where my lack of knowledge of math affects me the most. So I have a regular circle that appears where you click the mouse and begins to fall using basic velocity and acceleration.

I have a line created in the middle of the circle to represent the angle the circle is falling. I can't seem to get the last point to rotate around the circle. At the moment I'm just trying to have it follow the circle's path just in general.

circle.setPosition(m_position.x - fOffsetX, m_position.y - fOffsetY);
circle.setRadius(radius);
circle.setOutlineThickness(0.5f);
circle.setOutlineColor(sf::Color::White);
circle.setFillColor(sf::Color::Transparent);
float angle = (atan2(circle.getPosition().y ,   circle.getPosition().x ) * 180) / 3.141;
circle.rotate(angle);

sf::Vector2f l1{ circle.getPosition().x + circle.getRadius() , circle.getPosition().y + circle.getRadius() };
sf::Vector2f l2{ circle.getPosition().x + circle.getRadius() * 2 , circle.getPosition().y + circle.getRadius() };

drawLine(l1, l2, sf::Color Yellow)
 

Without telling you every single cosf and sinf formula I configured (aka probably made up) for 'l2' can anyone just tell me off the top of their head how I would accomplish this? Perhaps explaining quickly why would be awesome.

Thanks, everyone have a great week and be safe.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Moving a point around a falling circle
« Reply #1 on: December 14, 2020, 10:45:58 am »
Hi

A point on circle is given by (pseudo-code):
p.x = circle_center.x + circle_radius * cos(angle_in_rad);
p.y = circle_center.y + circle_radius * sin(angle_in_rad);

According to how your 2D coordinates system is defined, you may have to negate and/or switch the sin/cos terms to get the right orientation.
Laurent Gomila - SFML developer

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Moving a point around a falling circle
« Reply #2 on: December 14, 2020, 12:53:10 pm »
Hey Laurent,

Appreciate the input. When I put it radians I get no change for some reason, which makes me think I'm not doing something right. As of now, I got it working a lot better. The ball bounces off the ground and "I THINK" the line is spinning around the ball based on the angle. When it lands and is static the 2nd line spasms out.

{
circle.setPosition(m_position.x , m_position.y);
circle.setRadius(radius);
circle.setOutlineThickness(0.5f);
circle.setOutlineColor(sf::Color::White);
circle.setFillColor(sf::Color::Transparent);
//circle.setOrigin(radius / 2.f, radius / 2.f);
/*
        p.x = circle_center.x + circle_radius * cos(angle_in_rad);
        p.y = circle_center.y + circle_radius * sin(angle_in_rad);
        sf::Vector2f p{ (circle.getRadius() / 2.f) + circle.getRadius() * cosf(fAngle),
                        (circle.getRadius() / 2.f) + circle.getRadius() * sinf(fAngle) };
*/

sf::Vector2f p1{ circle.getPosition().x + circle.getRadius(),
                 circle.getPosition().y + circle.getRadius()  };       
sf::Vector2f p2{ circle.getPosition().x + circle.getRadius() / 2.f + circle.getRadius(),
                 circle.getPosition().y + circle.getRadius() / 2.f + circle.getRadius()};
float angle = (atan2f(m_velocity.y, m_velocity.x) * 180) / 3.14159;
// angle = toRadians(angle);
const sf::Vector2f newPos{ radius * cosf(angle) + p2.x,
                           radius * sinf(angle) + p2.y };
               
engine->getWindow()->drawLine(p1, newPos, sf::Color::Yellow); // sf::Lines
engine->getWindow()->draw(circle);
}
 

Any additional help would be appreciated.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Moving a point around a falling circle
« Reply #3 on: December 14, 2020, 03:25:02 pm »
The circle center is not "position + radius / 2 + radius". It should be "position + radius" (given that "position" is the top-left corner -- ie. you don't call setOrigin).

And sin/cos functions definitely do not take angles in degrees.

I don't know how you can get something "working" with this code ;)
Laurent Gomila - SFML developer