Similization
Member
Hello everybody, i wanted to make clever bullet. The bullet, knowing the speed of the player and the direction of his movement, with the condition that the player does not change either the speed or the direction. Must calculate the angle at which it will fly out to intersect with the player
So i know, how to do it in words. But there are still some troubles with code.
The time is the same and we can write 2 formulas:
1) distance_from_object_to_intersection point(s1) / object_speed == distance_from_bullet_to_intersection point(s2) / bullet_speed;
2) distance_between_bullet_and_object(s3) = s1^2 + s2^2 - 2*s1*s2*cos(angle_between_s1_and_s2)
If we use coordinates we will have sth like this:
A(x, y) - intersection point
B(x1, y1) - initial object coordinates
C(x2, y2) - initial bullet coordinates
v - object speed
w - bullet speed
ab - distance between object and intersection point
ac - distance between bullet and intersection point
cos(ab, ac) = ( (x-x1)*(x-x2) + (y-y1)*(y-y2) ) / sqrt((x - x2)^2 + (y - y2)^2) / sqrt((x - x1)^2 + (y - y1)^2)
1) ab/v == ac/w -> sqrt((x - x1)^2 + (y - y1)^2)/v == sqrt((x - x2)^2 + (y - y2)^2)/w
2) bc^2 = ab^2 + ac^2 - 2*ab*ac*cos(ab, ac) ->
(x1 - x2)^2 + (y1 - y2)^2) = (x - x1)^2 + (y - y1)^2) + (x - x1)^2 + (y - y1)^2) - 2*sqrt((x - x2)^2 + (y - y2)^2)*sqrt((x - x1)^2 + (y - y1)^2)*( (x-x1)*(x-x2) + (y-y1)*(y-y2) ) / sqrt((x - x2)^2 + (y - y2)^2) / sqrt((x - x1)^2 + (y - y1)^2) ->
x1*x2 + y1*y2 = -x^2 + x*x1 - y^2 + y*y1 + x*x2 + y*y2 + x^2 -x*x1 - x*x2 +x1*x2 + y^2 - y*y1 - y*y2 + y1*y2 ->
0 = 0
Ugly formulas...
This way it's pretty hard to find intersection point coordinates. Is there any easier way to do what i want ?
So i know, how to do it in words. But there are still some troubles with code.
The time is the same and we can write 2 formulas:
1) distance_from_object_to_intersection point(s1) / object_speed == distance_from_bullet_to_intersection point(s2) / bullet_speed;
2) distance_between_bullet_and_object(s3) = s1^2 + s2^2 - 2*s1*s2*cos(angle_between_s1_and_s2)
If we use coordinates we will have sth like this:
A(x, y) - intersection point
B(x1, y1) - initial object coordinates
C(x2, y2) - initial bullet coordinates
v - object speed
w - bullet speed
ab - distance between object and intersection point
ac - distance between bullet and intersection point
cos(ab, ac) = ( (x-x1)*(x-x2) + (y-y1)*(y-y2) ) / sqrt((x - x2)^2 + (y - y2)^2) / sqrt((x - x1)^2 + (y - y1)^2)
1) ab/v == ac/w -> sqrt((x - x1)^2 + (y - y1)^2)/v == sqrt((x - x2)^2 + (y - y2)^2)/w
2) bc^2 = ab^2 + ac^2 - 2*ab*ac*cos(ab, ac) ->
(x1 - x2)^2 + (y1 - y2)^2) = (x - x1)^2 + (y - y1)^2) + (x - x1)^2 + (y - y1)^2) - 2*sqrt((x - x2)^2 + (y - y2)^2)*sqrt((x - x1)^2 + (y - y1)^2)*( (x-x1)*(x-x2) + (y-y1)*(y-y2) ) / sqrt((x - x2)^2 + (y - y2)^2) / sqrt((x - x1)^2 + (y - y1)^2) ->
x1*x2 + y1*y2 = -x^2 + x*x1 - y^2 + y*y1 + x*x2 + y*y2 + x^2 -x*x1 - x*x2 +x1*x2 + y^2 - y*y1 - y*y2 + y1*y2 ->
0 = 0
Ugly formulas...
This way it's pretty hard to find intersection point coordinates. Is there any easier way to do what i want ?