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

Author Topic: bullet keeps following the mouse  (Read 1325 times)

0 Members and 1 Guest are viewing this topic.

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
bullet keeps following the mouse
« on: July 31, 2011, 09:44:33 pm »
hay guys

i'm trying to make a bullet to keep moving towards the mouse position and keep moving till it crosses the screen or hit an enemy. i dont want it to stop at the mouse position when it reach it.

the problem is the bullet travels from the player to the mouse position and when it reaches the mouse position is stops and if i move the mouse the bullet keeps following the mouse, which is not what i want.

here is the code can you guys please help
here is a site that does exactly what i want check it out
http://www.benoitfreslon.com/actionscript-throw-bullets-to-mouse-direction

Code: [Select]

float Speed = 10;

float BulletX = Sprite[i].GetPosition().x;
float BulletY = Sprite[i].GetPosition().y;


float AngleX = MouseX - BulletX;
float AngleY = MouseY - BulletY;

float vectorLength = sqrt(AngleX*AngleX + AngleY*AngleY);

float DirectionX = AngleX / vectorLength;
float DirectionY = AngleY / vectorLength;

float VelocityX = DirectionX * Speed;
float VelocityY = DirectionY * Speed;


Sprite[i].Move (VelocityX, VelocityY);


Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
bullet keeps following the mouse
« Reply #1 on: July 31, 2011, 10:14:43 pm »
I think what you're looking for is sf::Vector2:
http://www.sfml-dev.org/documentation/2.0/classsf_1_1Vector2.php