I'm trying to get an object to move towards a position (specifically the mouse position, but that's not important). I'm using this code:
[cpp]
void Object::MoveTowardsPoint(float x, float y, float myvelocity)
{
if(x > this->position.x)//to the right
{
this->velocity.x += myvelocity;
}
if(x < this->position.x)//to the left
{
this->velocity.x -= myvelocity;
}
if(y > this->position.y)//above
{
this->velocity.y += myvelocity;
}
if(y < this->position.y)
{
this->velocity.y -= myvelocity;
}
}
[/cpp]
This only works in an NES-style way, in that it can only go 0 degrees, 45 degrees, 90 degrees, etc.... Could someone recommend a better way?
[cpp]Vector2D ang(x, y);
ang.Normalize();
velocity = ang * myvelocity;[/cpp]
Something like this.
Yup.
First of all you can subtract one position (position of the object) from the other (position of mouse) this will give you a vector which represents the movement between the 2. If you added it to the position of the object the object would instantly go to the position of the mouse.
Now you want to move it though, not ping it straight there.
So, now, since it's a 2d vector we use pythagoras to find it's size; a^2 + b^2 = c^2, so sqrt((vector.x * vector.x) + (vector.y * vector.y)) will give you a number.
When you have this value (known as the magnitude of the vector) you divide vector.x by it (vector.x = vector.x / magnitude) and vector.y by it (vector.y = vector.y / magnitude) and that will give you a unit vector.
A unit vector is a vector with magnitude of 1, so it will have values which, when used in pythagoras will give C the value of 1.
So now you can add the unit vector to the position of the object every frame and it will move 1 pixel for every loop done.
If you want to move it faster than that then multiply the unit vector by the speed.
so you could do
object.position = object.position + (speed * unitvector)
There's no specific SFML code in there but it should be easy enough. Feel free to ask questions.
[editline]02:36PM[/editline]
[QUOTE=ZeekyHBomb;18882231][cpp]Vector2D ang(x, y);
ang.Normalize();
velocity = ang * myvelocity;[/cpp]
Something like this.[/QUOTE]
Ninjad. Except I went into the details and maths.
Thanks to both of you, but I have one problem. There is no "normalize" function, and I can't figure out which part of the math it relates to. Could someone point me in the right direction?
[QUOTE=MrBob1337;18882696]Thanks to both of you, but I have one problem. There is no "normalize" function, and I can't figure out which part of the math it relates to. Could someone point me in the right direction?[/QUOTE]
To 'Normalise' a vector you divide each component by the magnitude of the vector:
[code]
Vector2 Normalise(Vector2 vec)
{
float mag = sqrt(vec.x*vec.x + vec.y*vec.y);
float X = vec.x/mag;
float Y = vec.y/mag;
return Vector2(X,Y);
}
[/code]
Untested code.
It might also help to pass by reference and const it. [code]Normalise(const Vector2& vec) const;[/code] (But I'm not entirely sure on the syntax)
Ah, ok.
[QUOTE=MrBob1337;18882696]Thanks to both of you, but I have one problem. There is no "normalize" function, and I can't figure out which part of the math it relates to. Could someone point me in the right direction?[/QUOTE]
[quote=Me]When you have this value (known as the magnitude of the vector) you divide vector.x by it (vector.x = vector.x / magnitude) and vector.y by it (vector.y = vector.y / magnitude) and that will give you a unit vector.[/quote]
To normalise a vector is to turn it into a unit vector.
Sorry, you need to Log In to post a reply to this thread.