Thread: linear question

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    288

    intersection question

    basically, i have 2 points (a and b).

    example set up:

    Code:
    a
      \
       \
        \
         \
          b
    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.
    Last edited by X PaYnE X; 12-02-2005 at 12:10 PM.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    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
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    thanks ill try that now

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    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.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    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.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    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.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    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;
    }
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    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.

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    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.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    maybe it should be + or - depending on the direction it has to travel?

    ill give that a try now and see if it works

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    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.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    looks like it should work, i have no idea why it doesnt

  13. #13
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    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;
    Last edited by ElastoManiac; 12-02-2005 at 02:27 PM.
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    you do not want to use such a solution for this simple task! that would be deadslow!
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  15. #15
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    prehaps...
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linear Programming
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 12-20-2007, 09:53 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 07:31 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM