Sign in to follow this  
Tommy Boy

Move Object towards another object? Correctly.

Recommended Posts

Tommy Boy    111

Hi everybody

 

I'm trying to move one object towards position where I have clicked. for example

 

if (Unicorn->getX() < ClickedPosX)
{
Unicorn->setX( Unicorn->getX() + 5);
}
 
if (Unicorn->getX() > ClickedPosX)
{
Unicorn->setX( Unicorn->GetX() - 5);
}
 
if (Unicorn->getY() > ClickedPosY)
{
Unicorn->setY( Unicorn->getY() + 5);
}
 
if (Unicorn->getY() < ClickedPosY)
{
Unicorn->setY( Unicorn->getY() - 5);
}
 
This works but doesn't go straight to that point, here is a picture representing what happens when game runs
 
bj8w1u.jpg
 
I know this happens because I am adding 5 which is the speed. I don't know how to calculate what to add, so he would walk towards that direction. I'm guessing I have to find angle between those two points and somehow use that?
 
Thanks guys, this is not homework or anything just doing it for fun and learning.

Share this post


Link to post
Share on other sites
Tommy Boy    111

Sorry this is probably going to be stupid question, but how do I normalize 2D vector?

Is it dividing the X and Y by the length of the line?

Share this post


Link to post
Share on other sites
Tommy Boy    111

Otherwise some linear algebra will be useful. If you want to move at a specific speed, make a vector from the current position to the desired position and normalize it, that will give you a unit vector in the direction towards the target and you can just multiply it by the desired speed to move the object. 

 

Got it working using that tactic :) Thank you Brother bob

Share this post


Link to post
Share on other sites
Kaptein    2224

the easiest thing is to make positions floats, then you can move by a fractional amount as well as integral, which is what happens when you move slowly

its never a good idea to use integral values for positions, unless perhaps you internally have precision, but render using integers

 

anyways,

first define a 2D vector:

class vec2
{
public:
    float x, y;
    
    vec2() : x(0), y(0) {} // initialize (x, y) to (0, 0)
    vec2(float v) : x(v), y(v) {}     // (x, y) to (v, v)
    // initialize both
    vec2(float X, float Y) : x(X), y(Y) {}
    
    float length() const
    {
        // length in euclidian distance:
        return sqrtf(x*x + y*y);
    }
    
    void normalize()
    {
        // make a vectors length be equal to exactly 1:
        float L = length();
        if (L != 0.0)
        {
            x /= L; y /= L;
        }
    }
};

of course, this 2D vector (vec2) is severely lacking in pretty much all aspects of what it should be able to do

however,

let's say you have 2 points in space (x1, y1)   and   (x2, y2)

both of these are vectors:

vec2 v1 = vec2(x1, y1);    // or vec2 v1(x1, y2) if you want

vec2 v2 = vec2(x2, y2);

 

then, the direction-vector pointing exactly towards a point (x2, y2) is

the normalized vector of the difference between v2 and v1:

vec2 v3 = vec2(v2.x - v1.x, v2.y - v1.y);

v3.normalize();

 

now we have a vector that points towards our point.

to move towards the new point at a certain speed, multiply the new vector by the speed, or "scaling factor" if you will

float speed = 0.5f;

v3.x *= speed;

v3.y *= speed;

 

now we will move the unicorn (was it?) towards the new point at our determined speed:

unicorn.x += v3.x;

unicorn.y += v3.y;

 

hope this helps

i wrote all the code directly in here, so it might be flawed

 

oops, i see you already got it working tongue.png

Edited by Kaptein

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this