I am in need of help on what I think amounts to a type of amotrization problem. The problem is in a video game i am authoring
I have ships flying around in 3d space, changing directions randomly. Sometimes they fly through the camera, and I want to avoid this.
It is easy enough to have the ship at a position (a 3d point) travelling in a direction (normalized vector) at a speed and then pick a new direction where the camera is not in between the current position and a point infinitely distant in the new direction.
the problem is in the algo that turns the ship from direction 1 to direction 2. Sometimes in the course of the turn, the ship passes through the camera. So, I need to be able to determine, given the ships present direction, speed, and new direction if it will pass through the camera as it turns to the new direction.
A schematic of the problem is in the image below.
The big black circle is a point with a radius that repersents the camera.
The smaller red circle is a point with a direction that represents the ship. the green line is the current direction, and the black line is the new direction. In the course of turning the ship to the new direction, and moving it in each step, it sometimes passes through the camera as shown in the dotted blue line. What I need to do is find the speed at which to turn to avoid the camera, as in the dotted red line.
The turn is very simple.
The ship is a point defined by x,Y,z. The current direction and the desired direction are represented as normalized x,y,z vectors.
On each step, the ship moves by
point = point + current_direction* speed
and the direction updates by a linear interpolation on each step
current_direction = current_direction + (desired_direction - current_direction) * turnspeed.
What I would like to be able to do when it is time to turn the ship is to take the postion, current_direction, wanted_direction, turn_speed, and camera position and determine the turnspeed I'd need to avoid hitting the camera. Or, given the position, direction etc.. determine with a given speed how close the path will come to a given point.
Presently, i am just looping through the entire path & evaluating where the ship is relative to the camera on each step & incrementing the turnspeed if it gets too close, but I would like to save those CPU cycles if possible.
So, I guess to try to simplify:
while (current_direction - wanted_direction > epsilon)
position = current_direction * speed
current_direction = current_direction + (wanted_direction - current_direction) * turnspeed
Is there an equation to solve that will determine the closest "position" will ever get to another set position?