SFML - Moving to defined point

Hi

I have created an object that moves in a circle using sf::Transform and rotate .
This takes an angle in degrees and the center coordinates which to rotate about. Would I be able to set the speed it moves at then?
I want to create a bullet that travels from the player which is on the circle to the center. The center point is defined.
How do you always set the starting point of the bullet to the ship's position?
I have tried .setposition() but doesn't work.
How do I make the bullet always shoot to the center. I have tried using vector maths, but does not seem to want to work. Debugging the program, the correct values are being assigned and calculated to the variables but on screen it's in the incorrect position.

Any help will be much appreciated. I've added some of the code here as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  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 edited on
There's at least one bug: std::atan2() takes the y parameter first.

The angle between any point P and the bullet B is given by atan2(B.y - P.y, B.x - P.x); radians.
Topic archived. No new replies allowed.