Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Math question (2d trig)

This thread is locked; no one can reply to it. rss feed Print
 1   2 
Math question (2d trig)
DanielH
Member #934
January 2001
avatar

Here is the link again.

Going down the list line by line:

1 through 5 are adjustable. You can manually change or drag the points around.

1. p1 is player position
2. p2 is bullet position
3. A is angle player is traveling (relative to the line p1 to p2 not the x axis)
4. v1 is player speed
5. v2 is bullet speed

make an imaginary triangle
6. j is horizontal distance p2 to p1
7. k is vertical distance p2 to p1
8. l is absolute distance p2 to p1

9. F is angle from x axis to the line p2 to p1

10 I meant to remove (it is duplicated at line 18)

11. calculate B - using the similar triangle method I described earlier (law of sines)
12. calculate C
13. calculate c of smaller similar triangle where a = pv and b = bv and using C (law of cosines)
14. calculate time t based of the two similar c's, where l is c from big triangle

Next 2 lines 15 and 16 I calculate collision point using both A and B
(A + F) is absolute angle to x axis of player to collision
(180 - B + F) is absolute angle to x axis of bullet to collision
I have 90 + 90 - (B - F) so I could visualize it in my head while I was doing it. Starting at 90 how much to add 90 - (B - F).

{"name":"613192","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/0\/e07b47d697607193b4119438b8663fd2.png","w":576,"h":538,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/0\/e07b47d697607193b4119438b8663fd2"}613192

15. calculate point p3 = player + time * speed * angle
16. calculate point p4 = bullet + time * speed * angle

this part of that is where we get bv.x and bv.y
bv.x = bv * cos(180 - B + F)
bv.y = bv * sin(180 - B + F)

p4.x = bullet.x + time * bv.x
p4.y = bullet.y + time * bv.y

17, 18 and 19 are lines drawn between the points with limits put in

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I don't see which triangles are similar. Can you let me know?

I finally gave up and tried the equation I provided to Michael based on his formula and it works perfectly. I now have a little tank shooting down an airplane. I don't know if I'll develop it further, but at least now I have proof of concept.

DanielH
Member #934
January 2001
avatar

2 triangles.

The "big" triangle has 3 sides.

Side a is a vector where start is player and end is collision
Length a = time * player speed

Side b is a vector where start is bullet and end is also collision.
Length b = time * bullet speed

Side c is a vector where start is player and end is bullet

We don't know time so we can't calculate a or b. We can calculate c with distance formula.

"Small" triangle is a similar triangle where all sides lengths are divided by time
a = player speed
b = bullet speed
c = unknown

In my example I used trig to calculate small c. Then take ratio of big c to little c to calculate time.

Michael Weiss
Member #223
April 2000

Wow! I have been away for a while and I come back to find all this cool discussion.

When I have time I will see if I can wrap my head around these new concepts.

Thank you all for working on my little math problem.
I find the discussion fascinating....

I was pretty sure there were multiple ways to get the answer.

 1   2 


Go to: