Advertisement

Aiming ahead of an moving object

Started by February 14, 2002 10:24 AM
8 comments, last by cybrgeek 23 years ago
I''m coding a 2D space shooter, and I want the AI ships to aim based on where their projectile will hit the players ship based on the players ship''s current speed and the speed of the projectile. Keep in mind that the ships are moving on both the x and y axis and the projectile can only move on the x axis. If the AI ship is in a position that the player ship would move into the projectile it should shoot then. Any help would be appreciated.
What exactly is the problem? The enemy ships know how fast you''re moving, and they know how fast their bullets travel. That''s all you need to make them aim ahead.

- Pete
Advertisement
Right, I was hoping somebody had some equations I could use.
Given that the projectile can only move along the x axis, you want to find the intersection of two infinite lines:

1) a line parallel to the x axis ---- really this is the line the projectile follows
2) a line that begins at the current position of the player's ship and points in the direction the player's ship is moving.

Computing the intersection of two 2-dimensional lines is a very basic geometry problem. Search the forum archives if you need specific details and equations.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Edited by - grhodes_at_work on February 14, 2002 2:52:15 PM
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Pre-made equations... No. However I do have an idea of what you can do to get the result.

Use a dot product to get the projection of the player''s vector into the enemy ship''s vector (accounting for the speed of the weapon''s projectile of course), then correct the angle and speed of your enemy ship to make the projection a 1:1 ratio. Adjust the number of times per second this equation is run, that way the difficulty is varitable rather than perfect and you are not constantly changing the result based on usert input.
"Who are you, and how did you get in here?""I''m the locksmith, and I''m the locksmith."
Why do people get stuck for 2 mins then go looking for someone to solve the whole problem? Do some research, people! Break the task down into smaller parts!

- Pete
Advertisement
And you probably thought all those problems in high school about trains leaving New York and Boston were a waste of time!
LOL, sadly my math teacher last year didn''t know how to answer the question with the trains colliding.

To better clarify my question I want to find out at what point two moving objects will collide, if at all, given a constant speed along the x and y axis. The speed along the X and Y axis may be different for both objects if that is important.
quote:
Original post by cybrgeek
The speed along the X and Y axis may be different for both objects if that is important.


Its not important if you treat the problem as the intersection of two arbitrary lines in the xy plane.

Here''s a quick and informal example of solving systems of linear equations. I''m guessing your teacher will be clueless about this, . Again, you should know that this is VERY elementary for game math, .

Just draw a line along the direction of the projectile (starting point and direction), and another along the direction of the ship (starting point and direction). You will have two equations:

projectile.x = dt * projectile.x_speed + projectile.starting_x;projectile.y = dt * projectile.y_speed + projectile.starting_y; 


and

ship.x = dt * ship.x_speed + ship.starting_x;ship.y = dt * ship.y_speed + ship.starting_y; 


where dt = time - time_the_projectile_was_fired

and the starting_x and starting_y for both the ship and projectile are equal to the objects'' x and y positions at the time the projectile was fired.

You''re looking to solve for dt for the following situation:

projectile.x = ship.x 


and

projectile.y = ship.y 


Pick projectile.x = ship.x and solve it:

dt * ship.x_speed + ship.starting_x = dt * projectile.x_speed + projectile.starting_x 


Or, to simplify, do the algebra:

dt * (ship.x_speed - projectile.x_speed) = projectile.starting_x - ship.starting_x 


*Or* to simplify and actually solve for dt (what we want):

dt = (projectile.starting_x - ship.starting_x) / (ship.x_speed - projectile.x_speed); 


Note that if ship.x_speed == projectile.x_speed then you cannot do this and you have to solve the .y equations. And if ship.x_speed == projectile.x_speed AND ship.y_speed == projectile.y_speed then there cannot be an intersection (the ship and projectile are moving parallel to each other at the same speed).

If dt is negative, the object will never intersect (they are moving away from each other), but if they were moving in the opposite direction they would ultimately collide.

Once you have a positive value for dt, just calculate both ship.y and projectile.y. If they are equal (or close within some bounding area) then you have an intersection.

Good luck!

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Thanks a lot! I talked with another teacher, and spent some time figuring it all out, and it turned out to be simple. All I have to do is find what point the two lines that the objects travel intersect at, then figure out the time it takes each object to reach that point, and if that time is the same then they are colliding. Then just add simple checks like parallel lines, object direction, and object size.

This topic is closed to new replies.

Advertisement