Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9254581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:30:24+00:00 2026-06-18T11:30:24+00:00

I have asked this question previously and it was closed due to it not

  • 0

I have asked this question previously and it was closed due to it not being a programming question, this is entirely as I worded it wrong. I would like this to implemented into java. I am creating a little game and I have a photon torpedo which is being fired from a ship towards a target. Now as the speed of the torpedo is slow the ship will never hit any targets if they’re moving and I want to fix this. I have drew up multiple theories and mapped out lots of mathematical stuff to find out the best way to accomplish this and in the end I deduced the following:

  1. I find the time it takes for the photon torpedo to get to the target.
  2. I find how far the target will have traveled in the time it takes for the torpedo to arrive.
  3. I then find the distance between the new position of the target and the original ship.
  4. this then gives me the opportunity to use the Cosine rule (SSS) to find out the trajectory at which the bullet needs to be fired to have a much higher chance of hitting.

Here is a digram:

A diagram of my problem

Now the only problem that I need to rotate line a to the correct orientation as by default it’s parallel to line c which messes up the entire equation. Can anyone help with this? And also if you can think of a better way to find the new position suggestions are very welcome. My java game entity mechanic works as follows:

  1. Each entity has two Vectors which control movement. Position and Velocity. However, velocity is not tracked entirely properly as instead of it being a speed and a direction, to make things easier it’s an xSpeed and a ySpeed.
  2. The entities are all updates once per tick and the ship which shoots the torpedo must calculate the future position in this one tick and not over multiple ticks.

I ask this question not to be closed again, because this time I really need the answer to be implemented into Java.

This is the math i’ve tried so far:

        double dis = level.distanceBetween(photonTargetTop, this);

        double speed = 5;

        double time = dis / speed;

        double d1 = photonTargetTop.velocity.x * time;
        double d2 = photonTargetTop.velocity.y * time;

        double dis2 = level.distanceBetween(this, photonTargetTop.pos.x + d1, photonTargetTop.pos.y + d2);

        double dis3 = level.distanceBetween(photonTargetTop, photonTargetTop.pos.x + d1, photonTargetTop.pos.y + d2);

        double cosAngle = Math.pow(dis2, 2) + Math.pow(dis, 2) - Math.pow(dis3, 2) / 2 * dis2 * dis;

        double angle = Math.acos(cosAngle);

        EntityPhoton p = new EntityPhoton(this, level);
        p.rotation = angle;
        level.addEntity(p, pos);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:30:25+00:00Added an answer on June 18, 2026 at 11:30 am

    Let’s assume the target ship has no acceleration, meaning that it’s speed and direction is not changing.

    Let’s also assume that once fired, your torpedo has no acceleration. And it always goes at constant speed.

    Let’s also call (0,0) the point where your torpedo is fired.

    The ship describes a straight line. Choose a point on this line (if the ship is following a course that does not go through (0,0) you can find the closest point to the central position with some geometry that you can look up on wikipedia).

    Once you have chosen the position where you want to hit the enemy ship, you know the distance between (0,0) and that position, and given that the speed of the torpedo is always the same, you can also know when to fire the torpedo.
    Then you must also find the direction, meaning the values of speed to give on x and y, but that’s not so difficult.

    In general the problem is a system with multiple solutions, so presuming that the torpedo is faster than the target, there are infinite points where you can hit it, so you must use some heuristic to choose a point that’s convenient to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen this question asked previously but can not find a clear explanation
I have asked this question before but did not get the satisfied answer as
Though this question is asked previously but their is no workaround suggested. I have
I have asked a variation of this question previously for Hibernate. However, I am
I have a problem similar to this question which was previously asked but my
I have previously solved a similar problem in this question , where I asked
Sorry if this is a repeat of a question asked previously, but I have
I know this question has been asked previously and I have tried all the
Ok, I have previously asked a similar question to this but it was voted
I know that this question is being asked before but I did not get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.