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

Author Topic: SFML - Moving to distinct point  (Read 1182 times)

0 Members and 1 Guest are viewing this topic.

JasonMcG

  • Newbie
  • *
  • Posts: 10
    • View Profile
SFML - Moving to distinct point
« on: September 15, 2017, 11:35:18 pm »
HI

I have a player that moves in a circle. I used sf::transform and rotate which takes in the angle in degrees and the center to rotate about. I want to fire bullets from this circle to the center. The center point is predefined.
How do I go about doing this? I am trying to use vector maths but it is not working out. It moves but not to the correct location.
And also how do I map the bullet to the player each time the player shoots?
I've added some of my code for reference.

  void Player::turn(){
    float angle = 0.5;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
        transform.rotate(angle, {1920/2.f, 1080/2.f });
    }
}

void Shoot::update(sf::RenderWindow& window){
    float bulletX = _bullet.getPosition().x;
    float bulletY = _bullet.getPosition().y;
   
    float disX = _center.getPosition().x - bulletX;
    float disY = _center.getPosition().y - bulletY;
    float shot_RotationAngle = (atan2(disX, disY));
    _bullet.setRotation( (shot_RotationAngle * 180 / 3.14159265) );
   
    // Distance formula
    float length= sqrt(disX*disX + disY*disY);
       
    float newX = disX;
    float newX = disY;
   
    // Normalize the value
    newX/= length;
    newX/= length;

    bulletX += ((newX*0.1));
    bulletY += ((newX*0.1));

    _bullet.setPosition(bulletX, bulletY);
}
 
« Last Edit: September 16, 2017, 08:56:21 am by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML - Moving to distinct point
« Reply #1 on: September 16, 2017, 09:02:01 am »
Quote
    atan2(disX, disY)
atan2(disY, disX)

Quote
    float newX = disX;
    float newX = disY;
   
    // Normalize the value
    newX/= length;
    newX/= length;

    bulletX += ((newX*0.1));
    bulletY += ((newX*0.1));
A lot of "newX" that should be "newY".

Quote
    float bulletX = _bullet.getPosition().x;
    float bulletY = _bullet.getPosition().y;
   
    float disX = _center.getPosition().x - bulletX;
    float disY = _center.getPosition().y - bulletY;
Not really an issue, but...
auto dis = _center.getPosition() - _bullet.getPosition();
« Last Edit: September 16, 2017, 09:04:07 am by Laurent »
Laurent Gomila - SFML developer