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

Author Topic: Movement along rotation is drifting away  (Read 355 times)

0 Members and 1 Guest are viewing this topic.

jj_starts

  • Newbie
  • *
  • Posts: 1
    • View Profile
Movement along rotation is drifting away
« on: July 20, 2023, 06:05:16 pm »
Hi all,

I have a gun, bullet and enemy.

The gun is rotated to the enemy correctly, the bullet is rotated from the guns rotation. If I draw a line from the bullet's position to the enemy's position it lines up perfectly. However when I move the bullet towards the enemy it 'drifts' away from its rotated position


 
    bullet_x_pos = gun.getPosition().x;
    bullet_y_pos = gun.getPosition().y;

    gun.rotate(gun_angle); //gun angle is correct, lines up perfectly

    bullet.rotate(gun_angle);
   
    bullet.setPosition(bullet_x_pos, bullet_y_pos);//have reversed set position and rotate for bullet, same result



float bullet_velocity = 0.2;

//called everyframe- does not work as expected
void update_bullet()
{
 
    bullet.move(cos(bullet.getRotation() * (3.14159265 / 180)) * bullet_velocity,
        -sin(bullet.getRotation() * (3.14159265 / 180)) * bullet_velocity);

}


//second attempt, exact same result as first update_bullet() as above
void update_bullet()
{
 
    bullet.move(-bullet_velocity,-bullet_velocity);//

}

 



I have attached two images one where the bullet(in blue) starts and where it ends, you can see it drifts off line.
I want the bullet to follow the red line.

All help appreciated


Hapax

  • Hero Member
  • *****
  • Posts: 3364
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Movement along rotation is drifting away
« Reply #1 on: July 22, 2023, 05:48:53 pm »
What is the bullet's current origin? Is it the centre, still the default top-left or something else?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything