Thread: Incrementing along a vector

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    28

    Incrementing along a vector

    Hi again,

    Im making a program in c++ using openGL (glut) but having a little trouble figuring out how i would increment along a certain path...

    I have quite a few functions that require it like the one below, where i have the Point (contains x,y,z components) of the waypoint i need to head towards, and i have the angle to that waypoint from the current x,y,z position. Plus i can get the current position Point by just calling body->Get_Position(); if i need too.

    But im just not sure how i would set the incrementation Point's x,y,z to send to the Set_Position function.

    I would take a guess and say that if its walking and the angle rotates the view so its facing the waypoint, i would just increment the -z value of the current Point position by WALK_SPEED ? (WALK_SPEED is a #define)

    but im not sure. Could anyone please give me some advice?

    Thanks,

    Code:
    void AI::Patrol() {
        
    	body->set_Walking(true);
    	body->set_Running(false);
    	body->set_Still(false);
    
    	Point increment;
            Point wp = Next_Waypoint();
    	float angle = Get_Position_Angle(wp);
            float range = Get_Range(wp);
    
    	//How to get the incrementation in step?
    
    	//increment.x = ?
    	//increment.y = ?
    	//increment.z = ?
    
    	//WALK_SPEED ?
    	//RUN_SPEED ?
    
    	body->Set_Position(increment);
    	body->Set_Yaw(angle);
    }

    (i get the angle through this function, which i hope is correct)
    Code:
    float AI::Get_Position_Angle(Point pos) {
          
          float angle;
          
    	  //Get the float angle between NPC position and parametre position
          angle = atan2(body->Get_Position().z-pos.z, body->Get_Position().x-pos.x)*(180 / PI);
          angle -= body->Get_Yaw();
          
          return angle;
    }

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    28
    please could someone give me some advice on this? :P
    /beg

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Okay, here's some code for how you handle things; note of course you'd need to define the operators +, -, and *, and the normalize function:

    Vector position; // This should be set with the current position
    Vector destination; // The x,y,z, coordinates of the destination

    Vector path = destination - position; // Subtract to get a vector representing the path we are walking
    double stepsize = 10.0; // How many units we should move in one step
    Vector step = path.normalize() * stepsize; // Here, normalize() returns a new vector that is the normalized (i.e. length 1) version of path.

    position = position + step; // Update the location
    Last edited by Cat; 11-01-2006 at 02:48 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM