Rotating a 2D vector towards a point.

Hello there, this is my first thread, so please bear with me if I make any misstakes in ethics or in the code.

As the title states I need to rotate a 2d vector towards a point.
For rotation I'm using this method
float xt = (vec.x * cosf(angle)) - (vec.y * sinf(angle));
float yt = (vec.y * cosf(angle)) + (vec.x * sinf(angle));
sf::vector2f newvec(xt,yt);

This is what I have sofar:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
bool Rotate to target(gEntity& Circle, sf::Vector2f pos)
{
	sf::Vector2f VRadius(0,-Circle.getLocalBounds().height/2);
	//Gets the radius of the circle, which is the vector we want to rotate
	sf::Vector2f EyeDirRad(VRadius.x * cosf(48.67) - (VRadius.y * sinf(48.67)),VRadius.y * cosf(48.67) + VRadius.x * sinf(48.67));
	//I want a specific part of the circle to be centered towards the position.
	if(Circle.getRotation() >= 360) //getRotation returns the rotation in degrees of the Circle
	{
		Circle.rotate(-360);
	}
	else if(Circle.getRotation() < 0)
	{
		Circle.rotate(360);
	}
	//Rotation can be returned in values above 360 or below 0 so I compensate for that.
	double newradx = (EyeDirRad.x * cosf(Circle.getRotation())) - (EyeDirRad.y * sinf(Circle.getRotation()));
	double newrady = (EyeDirRad.y * cosf(Circle.getRotation())) + (EyeDirRad.x * sinf(Circle.getRotation()));
	sf::Vector2f newradius(newradx,newrady);
	//Radius of circle, rotated.
	sf::Vector2f distance(Circle.getPosition().x - (pos.x)),
		Circle.getPosition().y - (pos.y));
	//Distance between center of circle and the position
	double length1 = sqrt(distance.x*distance.x + distance.y*distance.y);
	double length2 = sqrt(newradius.x*newradius.x + newradius.y*newradius.y);
	double xnormlength1 = (distance.x)/length1;
	double ynormlength1 = (distance.y)/length1;
	//Normalization of distance vector
	double xnormlength2 = (newradius.x)/length2;
	double ynormlength2 = (newradius.y)/length2;
	//Normalization of the rotated radius
	double length2dotlength1  = xnormlength1*xnormlength2+ynormlength1*ynormlength2;
	//Dot product
	double angle = acosl(length2dotlength1);
	//Angle between the radius rotated and the center of the box
	Circle.rotate(angle*(180/PI));
}


however, if I use it the circle will just keep on rotating (in fairly large intervals, 30 degs - 180 or so degs) even if the distance is not changed at all.

Does anyone know whether this is me having done wrong in the math somewhere or just though completely wrong, then that would be helpful.
Your angles need to be in radians

to convert from degrees to radians use M_PI*degrees/180.0
in the calls to cosf/sinf?
Did that (replaced the calls in cosf/sinf to

(EyeDirRad.x * cosf(Circle.getRotation()*(180/PI))) - (EyeDirRad.y * sinf(Circle.getRotation()*(180/PI)));

Still doesn't work.I still just keeps on spinnig...
Topic archived. No new replies allowed.