I'm studying to implement a pendulum motion simulation.
void SinglePendulum::setPendulum(double dt) {
// Apply gravity
auto acc = acceleration;
Vec3<double> gravity(0.0, -9.8, 0.0); // x = 0.0, y = -9.8, z = 0.0
acc += gravity * inverseMass; // get accelation
velocity += acc * dt; // get velocity
velocity *= pow(dampingCoeff, dt); // apply damping
variablePosition += velocity * dt; // gravity-affected weight
// Vector from reference point to end point after being affected by gravity
variableVector = variablePosition - pivotPosition; // a Position - b Position : b->a vector
// The angle between the reference vector and the current vector
double angle;
angle = standardVector.Dot(variableVector); // dot product the reference vector and the variable vector
double standVecLength = standardVector.Length(); // magnitude of reference vector
double variaVecLength = variableVector.Length(); // magnitude of variable vector
double finalAngle = acos(angle / (standVecLength * variaVecLength)); // get angle
// Apply position with pendulum motion
changePos.Set(sin(finalAngle) * length, cos(finalAngle) * length, 0.0); // apply x, y, z value
}
As far as I know, It is understood that after gravity, the pendulum motion can be implemented using the angle between the reference straight vector and the changed vector.
So I apply gravity first, I then calculated the vector from the reference point to the point affected by gravity.
And I did dot product with the reference vector and the vector of the changed position.
And I calculated the magnitude of each vector, and I calculated the angle using magnitude value and theta value.
And the angle is sin on the x-axis and cos on the y-axis, which is the pendulum motion.
However, the following results were found.
I have no idea which part I missed. Is there anything else I should do?
I looked at this document and tried to modify the code, but it didn't work properly either.
Edited code
void SinglePendulum::setPendulum(double dt) {
// The angle between the reference vector and the current vector
double theta;
theta = standardVector.Dot(variableVector); // dot product the reference vector and the variable vector
double standVecLength = standardVector.Length(); // magnitude of reference vector
double variaVecLength = variableVector.Length(); // magnitude of variable vector
double angle = acos(theta / (standVecLength * variaVecLength)); // get angle
Vec3<double> gravity(0.0, -9.8, 0.0); // x = 0.0, y = -9.8, z = 0.0
Vec3<double> forceX;
Vec3<double> forceY;
forceX.SetX(-9.8 * sin(angle)); // x = -9.8 * sin(angle), y = 0.0, z = 0.0
forceY.SetY(-9.8 * cos(angle)); // x = 0.0, y = -9.8 * cos(angle), z = 0.0
// Apply gravity
auto acc = acceleration;
acc += gravity * inverseMass; // get accelation
acc += forceX;
acc += forceY;
velocity += acc * dt; // get velocity
velocity *= pow(dampingCoeff, dt); // apply damping
variablePosition += velocity * dt; // gravity-affected weight
// Vector from reference point to end point after being affected by gravity
variableVector = variablePosition - pivotPosition; // a Position - b Position : b->a vector
}