Thread: simple xy motion question...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    23

    simple xy motion question...

    hi there:

    i am stuck on a simple math problem that i wonder if anyone can point me to the right difrection.

    i what to write a algorithm that draws a object and have it move towards a known xy coordinate at contant speed. for example, if the object is at (x, y) and the target is (5, 3), how would i work out the path between them and code it?

    many thanks

    CHUN

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    A) Find out the change in location for every unit of time. The way in which to do that is very much dependant on how often you want to update the location and in what units you're measuring distance in. Some simple equations and perhaps trig would help you here (Pythagorean Theorem?)

    B) Once you have the necessary change in X and the necessary change in Y, just add these onto your current location, and redraw the object.

    If you would like a more detailed reply than this, you will need to provide more information and/or perhaps some source code of what you've gotten so far by yourself.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by chunlee
    hi there:

    i am stuck on a simple math problem that i wonder if anyone can point me to the right difrection.

    i what to write a algorithm that draws a object and have it move towards a known xy coordinate at contant speed. for example, if the object is at (x, y) and the target is (5, 3), how would i work out the path between them and code it?

    many thanks

    CHUN
    Let vx = velocity in the x direction and vy = velocity in the y direction.

    Then deltax = vx * deltat and deltay = vy *deltat, where deltax is the change in x coordinate during time deltat and deltay is the change in y coordinate during time deltat

    Let s = the speed, then vx = s * cos(theta) and vy = s * sin(theta), where

    cos(theta) = (5 - x)/sqrt((5 - x) *(5 - x) + (3 - y) * (3 - y)) and
    sin(theta) = (3 - y)/sqrt((5 - x) *(5 - x) + (3 - y) *( 3 - y))

    If speed and direction are constant, everything up to and including the calculations for vx and vy are done once at the beginning of the transition, then deltax and deltay are applied incrementally

    Does that get you started?

    Regards,

    Dave
    Last edited by Dave Evans; 12-06-2004 at 09:44 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Consider something along the lines of the Bresenham line drawing algorithm. You can use this method to "chart the path" of where your object needs to go.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    23
    hi there:

    thanks for your replies, it make sense but i am still finding it a bit hard to understand it as my math is kind of none existence;( i think i need to do more research on this.

    cheers

    CHUN

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    It's easier than that.

    Code:
     
    struct Vector2D
    {
    float x;
    float y;
    };
     
     
    void GetVectorTo(Vector2D Start, Vector2D End, Vector2D &Result)
    {
    float vector_x=Start.x-End.x;
    float vector_y=Start.y-End.y;
    float totaldist=sqrt((vector_x*vector_x)+(vector_y*vector_y));
    float oneOverDist=1.0f/totaldist;
    Result.x=vector_x*oneOverDist;
    Result.y=vector_y*oneOverDist;
    }
    For DirectX using the D3DX library:
    Code:
     
    void GetVectorTo(D3DXVECTOR2 Start,D3DXVECTOR2 End,&D3DXVECTOR2 Result)
    {
    D3DXVECTOR2 VectorFromStartToEnd;
    D3DXVec2Subtract(&VectorFromStartToEnd,&Start,&End);
    D3DXVec2Normalize(&Result,&VectorFromStartToEnd);
    }

    This function returns a normalized or unit vector from Start to End. All vectors will lie on a unit circle and thereby can be used to increment your object each frame.

    The use for this code is:

    Code:
     
    class Object2D
    {
    Vector2D Position;
    float Speed;
    float Angle;
    ... 
    ...
     
    };
     
     
    void Object2D::ComputeVectorToTarget(Object2D Target)
    {
    Vector2D Result;
     
    GetVectorTo(Position,Target.Position,Result);
    VelocityVector=Result;
    Angle=atan2(VelocityVector.x,VelocityVector.y);
     
    }
     
    void Object::Update(float _frameTimeDelta)
    {
    Position.x+=(VelocityVector.x*Speed)*_frameTimeDelta;
    Position.y+=(VelocityVector.y*Speed)*_frameTimeDelta;
    }
    So to get the vector from Object/Point A to Object/Point B

    1. Create a Vector V from Object A to Object B by subtracting them.
    2. Compute length of Vector V by using distance formula.
    3. Divide each element of Vector V by the distance you just computed - or in simpler terms normalize Vector V.

    I use oneOverDistance because dividing by distance is the same as multiplying by its reciprocal. A floating point multiplication is much faster than a floating point divide.

    The angle is computed by taking the arc tanget of y over x. The C math function atan2 will do this for you. And sorry for using a class and other C++ code on a C board but people will get over it.


    cos(theta) = (5 - x)/sqrt((5 - x) *(5 - x) + (3 - y) * (3 - y)) and
    sin(theta) = (3 - y)/sqrt((5 - x) *(5 - x) + (3 - y) *( 3 - y))
    Essentially the same thing with (5-x) being the x component of Vector V and (3-y) being the y component of Vector V.
    Last edited by VirtualAce; 12-07-2004 at 07:50 AM.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    23
    thanks guys, i think i am getting there

    cheers

    CHUN

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Simple Question from Stupid Person...
    By Monmouth3 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-02-2002, 08:47 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM