SFML community forums

Help => General => Topic started by: JasonMcG on September 15, 2017, 11:35:18 pm

Title: SFML - Moving to distinct point
Post by: JasonMcG 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);
}
 
Title: Re: SFML - Moving to distinct point
Post by: Laurent 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();