-
intersection question
basically, i have 2 points (a and b).
example set up:
what im trying to do is, find a point on the line (any point between a and b). so far all ive managed to do is calculate the length of the line.
lets say the length is 5, how can i move 1 point from a towards b (on the line)?
i would google this but i have no idea what its called, ive never done this before. the reason i need to do it is because i have the player location (a) then i have the target location (b) and i need to move the camera from a to b at small intervals to give the 'fly over' look.
-
You could always use normalized vectors:
The amount you would need to move the camera on the x-axis would be camera.speed*(b.x-a.x)/length
for y-axis this is camera.speed*(b.y-a.y)/length
-
-
is the length the original length? or does it get updated every time the camera moves?
and whats a suitable # for the camera speed, im trying 0.01 and 10.0 and getting no movement.
-
the vectors dont have to be updated, the (b.x-a.x)/length is just constructing a vector betwean -1 and 1, and will remain the same during all the movement on the line. The only time you need to update the length is when either a or b changes (or in reality when b changes since, in my world when camera leaves point a it is on point p which is somewhere on the line betwean a and b, thus if you change b you need to recalculate everything as if your going from p to b).
For the speed, that depends on alot of stuff, nothing i can answer. What you need is a system where you translate the whole scene camera.x units on x-axis, camera.y units on y-axis and camera.z units on z-axis.
After that you need to implement where you update this using the formula i showed. When you move the camera you need to update camera.x and camera.y with something like:
Code:
camera.x += camera.speed*(b.x-a.x)/length;
camera.y += camera.speed*(b.x-a.x)/length;
of course now you dont want to do the speed*(b.x-a.x)/length all the time, just store that in another variable and use that one, to optimize some.
-
ok from what i understood, heres what i came up with:
Code:
float speed = 0.01f;
float x = xTo - xFrom;
float y = yTo - yFrom;
float length = (float)sqrt((x * x) + (y * y));
x = speed * (x / length);
y = speed * (y / length);
while (true)
{
if (xFrom != xTo)
xFrom += x;
if (yFrom != yTo)
yFrom += y;
if (xFrom == xTo && yFrom == yTo)
break;
}
this loop runs in another thread and only runs when the user clicks somewhere (xTo, yTo).
the camera moves in the opposite direction instead of towards the click.
-
ok try this instead then:
Code:
float speed = 0.01f;
float x = xTo - xFrom;
float y = yTo - yFrom;
float length = (float)sqrt((x * x) + (y * y));
x = speed * (x / length);
y = speed * (y / length);
while (true)
{
if (Position.x != xTo)
Position.x -= x; // changed to -=
if (Position.z != yTo)
Position.z -= y; // changed to -=
if (Position.x == xTo && Position.z == yTo)
break;
}
-
X goes in the right direction now, but it never stops.
and Y isnt really working, it just follows X.
i tried changing it to +Y and that didnt work either.
-
The problem with the camera never stopping might have something to do that you are using floats, which has very bad precision when so say you write
float foo = 1;
foo might actually be 0.999999
or 1.000001 or 1.0000000
so that might be cause for that problem.
As for the problem with the y not working, i cant really see why it doesnt work, shouldnt be any problem with it but im really really tired at the moment so i might miss something simple.
-
maybe it should be + or - depending on the direction it has to travel?
ill give that a try now and see if it works
-
no, that shouldnt matter..the vector will take care of that...say you have the coordinate system set up like this:
(0,0) is the center of the screen
positive x goes to the right, positive y goes up.
now say a is (-10, 20) and b is (30,10)
then length will be roughly 41.231056256
then if we add this in to the formula we get:
camera.x -= speed*(30-(-10))/41.231056256
camera.y -= speed*(10-20)/41.231056256
or if we simplify it:
camera.x -= speed*0,97014250014948731756303420178865
camera.y -= speed*-0,24253562503737182939075855044716
what we want to look are the numbers we multiply speed with, these are the normalized vectors. This means that if we want to move one unit in the direction towards the end point we need to move 0.9701425... units on the x-axis and -0.2425356... on the y-axis. And this is completely correct since the end point is to the right of the startingpoint and the end point is also below the startingpoint. so if you + or minus doesnt matter, i just did that instead of multiplying with -1 before you calculate the new camera endpoint.
-
looks like it should work, i have no idea why it doesnt
-
Use sin() and cos(), that should help you a lot!
Code:
a
|\
| \
| \
| A \
| \
| \
| \
| \
| \
c---------b
A = angle c-a-b;
Calculate the angle using tan() and ac, cb ( you can calculate that, right? )
When you have the angle it's very easy:
Lets sat that line ab = 5.
One point from a to b on x axsis should be x = cos A * 1;
And for y, y = sin A * 1;
-
you do not want to use such a solution for this simple task! that would be deadslow!
-