I have a circuit in my driving simulator in cpp and I want to know how to rotate the circuit around the origin, I tried to dig into some trigonometry because i know this is what will help me but I'm at a point where I'm stuck.
For your information, the circuit is stored inside a vector of pair (std::vector<pair<double,double> roads).
Here's what my example circuit looks like (red one), and what i want to do are the two other iteration (yellow and green).
and this is an example of what is stored inside roads
x : 170, y : 0
x : -170, y : 0`
x : 170, y : 0
x : -170, y : 0
x : 170, y : -0.69632
x : -170, y : -0.69632
x : 170, y : -5.57056
x : -170, y : -5.57056
x : 170, y : -18.8006
x : -170, y : -18.8006
x : 170, y : -44.5645
x : -170, y : -44.5645
x : 170, y : -0.69632
x : -170, y : -0.69632
x : 170, y : -5.57056
x : -170, y : -5.57056
x : 170, y : -18.8006
x : -170, y : -18.8006
x : 170, y : -44.5645
x : -170, y : -44.5645
...
For going foward, backward, straft to the left or the right, it's quite easy as i only need to decrease/ increase all x/y as i go in one direction, but the problem comes when i want to rotate. I know that, in theory I need to make sqrt(x² y²) to find the distance from the origin (aka the driver) and apply a rotation in consideration of that distance (as a ratio, the lesser the distance is the lesser it needs to rotate).
But for now, I'm stuck and I don't know how to do that, can anyone provide my some sort of explanation on how to do this ?
CodePudding user response:
I did a google for "math to rotate point around an origin." This is the first hit I got:
https://academo.org/demos/rotation-about-point/
It does a good job explaining the math. You don't show your code, but if you are just rotate around the origin but a certain number of degrees (or radians), it's pretty simple math for each point.
CodePudding user response:
you just get to apply a 2d rotation matrix (https://en.wikipedia.org/wiki/Rotation_matrix). This should work, you just get to iterate at each point:
#include <math.h>
void rotate(float &x,float &y,float teta)
{
float newX = x*cos(teta) y*sin(teta);
float newY = x*sin(teta)-y*cos(teta);
x = newX;
y = newY;
}
remember that this teta angle is measured in radians not degrees.