0

[ On my previous question I have explained the details ]

If you observe the image below, you will notice that there's a Bullet object, which originates from the point A(x1,y1), and the two other points which would be B and C. Now, when the Bullet is created, it gets the last known position of target and saves it. Then, it is supposed to travel to that point and further until it collides with any other object.

So, my question to you is: How do I make the Bullet object travel along the line between A & B and then continues in the same path unless stopped by another block? Imagine shooting at a target with a real gun; the bullet would travel as long as it could even if target moved itself. That's what I'm trying to achieve.
enter image description here

Talendar
  • 1,841
  • 14
  • 23
Feelsbadman
  • 1,163
  • 4
  • 17
  • 37

1 Answers1

0

To get the position of a bullet at a given time t travelling at some velocity v (which you can choose arbitrarily), we can follow these steps. I will assume that t=0 when the bullet is fired. (you can adjust to make this a dt (delta time) if you want too, it makes no difference)

  1. First find the (normalized) vector that will denote the direction of the bullet. This would be given by (1 / sqrt((x2-x1)^2 + (y2-y1)^2)) * (x2-x1, y2-y1). Create this vector once for when the bullet is "fired" and do not change it, as bullets stay on a constant trajectory. (You could maybe make the vector a member of the Bullet class to save it, just an idea)
  2. Next, multiply this by the scalar v. I should note that you can ignore this variable if you want as it can be accounted for in t, but from a physics perspective it makes intuitive sense to include.
  3. Finally, to get the position, simply multiply this vector by t where t is some time, and then add the original point (x1, y1) vector to this resulting vector. (Note that all of this is just 2d kinematics, there are many online resources if you want to explore this more.

This will yield the x and y positions of the bullet at a given time t. Then, as you update the x and y positions, you will need to implement some collision detection function/algorithm to detect if the bullet has hit something. Since I do not really know how you organized your code I cannot give you specific advice on the best way to do this, but here are some pointers.

First, I would recommend putting your objects that go on screen in a list/map. Also, I would recommend making your objects of simple shape (i.e. square, circle) so that it is relatively easy to check if a collision has occurred. Alternatively, you could make the collision a certain color, then check if the color value at the position of the bullet matches this color. If it does, then you know a collision has occurred. All of these are just ideas, but there are really many more (and probably slightly better) ways of doing this.

Looking at your previous question, I notice that you try to use angles. While it is certainly a valid approach to do this, I think this solution is relatively simply to understand and avoids the trigonometric aspect of it all.

Anyways, I hope I answered your question. Leave a comment/question if you have any further inquiries about this.

Alerra
  • 1,479
  • 11
  • 25