Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Geometry/Trigonometry Question

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Geometry/Trigonometry Question

    Hello everyone,

    Well this is probably more of a math question than programming, but still I can't get it right and I don't think math forums would be the best place either.

    I'm programming a simple 2D tank game for my entertainment.

    You can basically consider tanks and bullets to be located at points on an X,Y axis.

    I am trying to make an enemy tank shoot my bullets out of the sky. Let's just say that both our bullets travel at the same speed.

    So math wise, I am looking for a direction the enemy tank can shoot, that will intersect my bullet's path and collide with my bullet.

    My understanding is that if the bullets travel the same speed, the distance between my bullet and the intersection point, and the enemy's bullet and same intersection point, must be the same for the bullets to hit each other, rather than just cross paths.

    What I have been trying to do is convert my bullet's path to the format y = mx + b, then find another linear equation that meets the criteria to hit my bullet.

    I'm not sure how to do this. I've tried a couple different ways, and neither have worked. One at least shot somewhat close, but my more recent attempt produced weird, fail results.

    I've attached a picture that hopefully makes this a lot more clear to understand what I'm trying to find.

    Ideally finding the x,y point of intersection to shoot at would be best, but any help finding the line or angle to shoot in would be great.

    Thanks for your time!
    Ben
    Attached Images Attached Images


  2. #2
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Wink Re: Geometry/Trigonometry Question

    The bullet you shot above, just went over my head.

    I am feeling lucky that there is something more weird and dangerous than coding.

    Goldest
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Geometry/Trigonometry Question

    PB = PB1 = PB2 // fundamental definition of "collision"
    PB1 = P1 + V1 t // assuming both bullets are fired at the exact same time
    PB2 = P2 + V2 t

    P1 + V1 t = P2 + V2 t
    x1 i + y1 j + xdot1 t i ydot1 t j = x2 i + y2 j + xdot2 t i ydot2 t j
    S^2 = xdot1^2 + ydot1^2 = xdot2^2 + ydot2^2 // assuming both bullets travel at the same speed

    we have 3 equations and 3 unknowns (xdot2, ydot2, and t):
    x1 + xdot1 t - x2 - xdot2 t = 0
    y1 + ydot1 t - y2 - ydot2 t = 0
    xdot1^2 - xdot2^2 - xdot2^2 - ydot2^2 = 0

    (some long boring math)

    t = (x1^2 + y1^2 + x2^2 + y2^2 - 2 * (x1 * x2 - y1 * y2)) / 2 / (xdot1 * (x2 - x1) + ydot1 * (y2 - y1))

    Rather than go through the trouble of solving for the other two variables, you can find the intersection point PB from the first set of equations, then compute the angle you need to fire at using the intersection point and the location of tank2.

  4. #4
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Geometry/Trigonometry Question

    I'm sorry to say I didn't understand your math, but I've gotten farther along now

    Here's where I'm at now:

    I've made a triangle on a 2D graph.

    I know two points on the triangle (the x, y pair).

    I know all three angles of the triangle, and all 3 side lengths of the triangle.

    How do I find the (x, y) pair for the third point on the triangle?
    Attached Images Attached Images

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Location
    Voorschoten, the Netherlands
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Geometry/Trigonometry Question

    Quote Originally Posted by helloworld922 View Post
    (some long boring math)

    t = (x1^2 + y1^2 + x2^2 + y2^2 - 2 * (x1 * x2 - y1 * y2)) / 2 / (xdot1 * (x2 - x1) + ydot1 * (y2 - y1))

    Rather than go through the trouble of solving for the other two variables, you can find the intersection point PB from the first set of equations, then compute the angle you need to fire at using the intersection point and the location of tank2.
    Isn't that too much math? Both bullets travel on the equal sides of an isosceles and meet each other on the line which indicates the direction of the enemby bullet. If T and E are the points of the tank and enemy respectively, find the point in the middle == (T+E)/2. Draw a line perpendicular to the line ET and find the intersection with the line describing the line traveled by the enemy bullet. That'll be the point of impact of both bullets.

    kind regards,

    Jos