move object to------

Blitz3D Forums/Blitz3D Programming/move object to------

Jack(Posted 2012) [#1]
I'm not real good at explaining but hopefully I can get this done so it is a to be understood.

Using just 2 dimensions(I do want to end up in 3d however) lets consider object#1 located at x=500 and y=300.

Object #2 located at x=450 and y=400.

Object#2 is moving at rate of 1 and pointing up.(y decreasing).

I want (in this case) to have object#1 move at rate of 1 to intersept object#2 as soon as possible by moving x(decreasing) and y(increasing)

I thought that by pointing object#1 at object#2 and move each 1 move factor they would intersect. But this doesn't happen. The object#1 will go below object#2(y position) before turning and following object#2.

Thought maybe this had to do with object centers but I just used a very small sphere object and the same thing happens.

Would appreciate any help.


Yasha(Posted 2012) [#2]
That's because pointing an entity (by whatever means) points it at where the other entity is now. After both have moved, that target has also changed. The following entity must necessarily follow a curved path, because it's changing its destination from frame to frame.

To move it in a straight line, it needs to know the target entity's position, orientation and speed, and work out a flightpath for the other entity. It can then work out an intersection point on the path ahead of the entity, and point at this position, not at where the entity is now.

There are a whole bunch of intersection routines in the archives, so this ought to be covered there.


Jack(Posted 2012) [#3]
Thanks Yasha..I had previously looked in the archives and the things that seemed to apply are over my head I guess.

To some degree I understand vectors;which I assume is what is needed to solve the problem; but to get a point of intersection between the two objects leaves me in the dark.

I could probably do something by "brute force" but that is not really a good idea.


Kryzon(Posted 2012) [#4]
This is solved with physics, trigonometry and analytical geometry. I'll try to be as intuitive as possible, but you'll have to read the following more than once until it makes sense.
It should take a while to explain.
Put on some lounge music.
Deep breath.


Facts:

- Object A and B can have different speeds (even if in the end you give them both the same speed values).

- Object A is travelling by a fixed direction\angle of travel and speed. Object B is trying to intercept it with the shortest travel possible, with a fixed speed as well.

- Object A has a fixed direction\angle of travel and a starting position: from this we can abstract an infinite line which would be object A's path.

The thing is, depending on the position and speeds you assign to each of the two objects, object B might not reach the travelling object A at all.
If B is placed too far away and with a slower speed than that of A it'll never intercept it, as A will always be ahead.
In case A is not interceptable then you can return 'FALSE' or whatever, and tell the user object B will never intercept A.
So we need to first test for the interceptability of A; if it's interceptable by B then we proceed to compute the shortest path B has to travel in order for this to happen.

How do we test the interceptability of A? there are two ways; it depends on whether B is 'behind' or 'ahead' of A.
First of all, compute the shortest distance from B to A's path. This shortest distance is the smallest segment that goes from B and intersects with object A's path. This intersection is the point in A's path that's closest to B.

• If this point is behind A, then object B is behind A:


• If this point is ahead of A, then object B is ahead of A:


• If object B is behind A, we can test A's interceptability simply by comparing speeds: if object B is slower or as fast as A, object B will never reach A as A already has an advantage and will only "gain" on B.

• If object B is ahead of A, do this:
Calculate the time taken for B to travel its shortest distance to A's path.
Calculate the time taken for A to travel from its current point to the point in A's path that intersects B's shortest distance.
We are figuring out "who gets there first", at that yellow dot that is the point where B is closest to A's path:

Under these conditions, if B arrives there first it means it CAN intercept A. If, however, A arrives at that point first, B will never be able to intercept it - this is because B is not capable of intercepting A even through the shortest route it could possibly take (which is the closest distance from B to A's path). If this is so, you can return that 'FALSE' and move on.

In case B arrives first (or at the same time as A): what matters is the time B takes to arrive there. If B arrives there faster (= takes less time), then we take this time advantage it has over A and use it to find the exact closest point of interception.
To do this, we take this time advantage B has and multiply it by B's speed (finding out how much distance B can walk along this time advantage). We add this distance we calculated to the shortest distance from A's path to B.
This will give us the specific distance object B needs to travel towards A's path in order to intercept object A.

• To find the actual point to travel to we need to abstract some things:


• Using Pythagoras' theorem on that right-triangle:

This right triangle is comprised of...
- B's shortest distance to A's path;
- Interception distance (shortest distance from B + distance travelled by advantage time over A);
- An unknown distance which is the point of interception.

interceptDistance^2 = shortDistance^2 + unknown^2
unknown^2 = interceptDistance^2 - shortDistance^2
unknown = Sqr(interceptDistance^2 - shortDistance^2)

Once we have that unknown distance, we project from the point where B is closest to A's path, towards A, by that unknown distance. This is the interception point.
Aim object B at this point, make B and A start moving by their speeds and directions. They should eventually meet there.
Phew.

---------------------
I'll put this in code later on and see if it actually works (it did in my head at least).


Jack(Posted 2012) [#5]
Thanks Kryzon.....

Quite an explanation. I really appreciate the length of time you took to illustrate the problem. And you are right,I will have to read it more than once.Much more often.

I'll get on this today and will enjoy the time spent.


Jack(Posted 2012) [#6]
Thanks so much Kryzon. That took some time for you.
I'll get with this today and let you know how I make out.


Jack(Posted 2012) [#7]
Kryzon..............Were you able to find time to code this.
I have not quite been able to work through that.