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

Author Topic: bullet keep following mouse direction  (Read 1783 times)

0 Members and 2 Guests are viewing this topic.

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
bullet keep following mouse direction
« on: December 17, 2011, 08:47:04 pm »
Hello
I have a problem where the bullet keeps following the mouse instead of taking mouse angle and direction and keep going.

I have this code and someone fixed it for me but i there is a small problem. The guy who fixed it used Sprite.SetVector and the problem is there is no Vector function in sprite class.

can you guys please help.

My code
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);


Fixed code
Code: [Select]

// This code is called only when the bullet is fired
float bullet_x=Sprite[i].GetPosition().x;
float bullet_y=Sprite[i].GetPosition().y;
float angle_x=MouseX-bullet_x;
float angle_y=MouseY-bullet_y;
float vector_length=sqrt(angle_x*angle_x + angle_y*angle_y);

Sprite[i].SetVector(angle_x/vector_length, angle_y/vector_length);  // Each bullet stores a direction vector


// This code is called every update
float velocity_x = Sprite[i].GetVector().x * Speed;
float velocity_y = Sprite[i].GetVector().y * Speed;
Sprite[i].Move(velocity_x, velocity_y);


Hoe can I do this ?
Sprite.SetVector(angle_x/vector_length, angle_y/vector_length);  //
Sprite.GetVector().x

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
bullet keep following mouse direction
« Reply #1 on: December 18, 2011, 01:19:30 am »
You must store it on your own, out of the Sprite class, and use it from wherever you've stored. You can make a container class, that have a Sprite inside, and this vector as well.

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
bullet keep following mouse direction
« Reply #2 on: December 18, 2011, 07:59:36 am »
Quote from: "Tex Killer"
You must store it on your own, out of the Sprite class, and use it from wherever you've stored. You can make a container class, that have a Sprite inside, and this vector as well.


can please give me an example. I have never use Vectors in sfml.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
bullet keep following mouse direction
« Reply #3 on: December 18, 2011, 07:16:17 pm »
Code: [Select]
class MySprite {
    public:
        sf::Sprite sprite;
        float speedX;
        float speedY;
}


And then:

Code: [Select]
MySprite Sprite;
Sprite.speedX = [your calculations here];
Sprite.speedY = [your calculations here];
// ...
Sprite.sprite.Move(Sprite.speedX * Speed, Sprite.speedY * Speed);


You would probably have better ways of doing it, depending on your code design. Test it and make it yourself.

 

anything