Your solution sounds interesting, I'll give it a try, but I have a problem with it which is that it can't just take a direction, but an specific line based on the start point of the first sprite and the current location of a movable sprite which makes it more complex.
I'll add the sample now since I have finished extracting all the needed code:
struct LinearIndex
{
float i;
LinearIndex();
LinearIndex(const float x);
void setFunction(const float m, const float b);
float getM();
float getB();
private:
float m, b;
};
class Bullet
{ private:
sf::Vector2f Start;
float CurrentSpeed;
public:
sf::Sprite Bshot;
LinearIndex Line;
unsigned L_type;
};
That's it for class definitions.
void LinearIndex::setFunction(const float tilt, const float intersect)
{ m = tilt; b = intersect; }
Bullet::Bullet(sf::Vector2f begin, const float Speed, Shot_Type SType)
{
Start = begin; CurrentSpeed = Speed;
Type = SType; TransformTo = Undefined;
After_Effect = Transform = false; DistanceMoved = 0.0;
}
void Bullet::LinearAssignation(const sf::Vector2f vec)
{ if( (int)Start.x == (int) vec.x ) ///To avoid float comparisson annoyance.
{ Start.x -= 1; }
float m = ( (vec.y - Start.y) / (vec.x - Start.x) );
float b = ( vec.y - (m * vec.x) );
Line.setFunction(m, b);
Line.i = Start.x;
if (m > 0)
{ L_type = 1; }
else if (m < 0)
{ L_type = 3; }
}
void Bullet::LineToPoint()
{ if (!OutofBounds())
{
if(L_type == 2)
{ Bshot.move( 0.0, CurrentSpeed );
std::cout << "Velocidad: " << CurrentSpeed << std::endl;}
else
{
Bshot.setPosition(Line.i, Line.getM()*Line.i + Line.getB() );
if (L_type == 1)
{ Line.i += CurrentSpeed; } /**Here I know that for it to work with consistent speeds
for all points I have to multiply the speed with a function
that makes it all move at the same speed regardless of the point,
for example in the 399 case it works well when you multiply it by 0.05,
but I need something that calculates that value from the difference
between the two points “x” */
else if (L_type == 3)
{ Line.i -= CurrentSpeed; }
}
std::cout << "Coordinates: " << Bshot.getPosition().x << ", " << Bshot.getPosition().y << std::endl;
///For output generation.
}
}
And the main function:
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Bullet Testing");
sf::Event event;
sf::IntRect d;
sf::Image img; img.loadFromFile("Images\\Bullets\\HQtest.jpg");
img.createMaskFromColor(sf::Color::Black, 0);
sf::Texture Img;
Img.loadFromImage(img, d);
Img.setSmooth(true);
sf::Vector2i vec(400, 300);
Bullet shot5(399.f, 0.0, 1); shot5.Bshot.setTexture(Img, true);
/**The samples I took use shot5 with 0.0 and 399 as startlocations, 0.0 being sample1 and 399 being sample2*/
shot5.Bshot.setColor(sf::Color(50, 25, 208, 200));
Bullet shot6(400.f, 300.f, 3.f); shot6.Bshot.setTexture(Img, true);
shot6.Bshot.setColor(sf::Color(50, 208, 25, 200));
shot5.LinearAssignation(shot6.Bshot.getPosition());
while (window.isOpen())
{ window.clear();
shot5.LineToPoint();
window.draw(shot5.Bshot);
window.draw(shot6.Bshot);
window.display();
}
Return 0;
}
I know it's too long but I tried to cut it as much as possible without taking dependencies out. I am also open to something that might do the same thing but in a different way, your first suggestion is good , but maybe seeing the code might give you an idea of what exactly I am trying to do. I just chose the mathematical approach because I couldn't think of anything other than that. I will use this code with many bullet objects that shoot themselves in the linear function trajectory based on a reference movable sprite or some other point, yet the "visible" speed must be the same for any point given in the grid.
What I was trying to do when writing these methods was to focus on making it as easy to use as possible, since my final program will sometimes have to deal with 500 moving sprites or even more. I also know that I should make my Bshot sprite private, but for easy testing I am leaving it public for the time being.
My approach for trying to solve this is in the comments in a function in the code above, yet I don't know which formula may help me do this so I am stuck on someone knowing how to or having an idea of how to do something that reaches the same visual effect.