Physics Simulation C++ implementation . Double Pendulum problem [closed]

I have a physics simulation that I wrote using the xpbd method. I can’t understand where I made a mistake. I am trying to simulate a double pendulum, but the system is constantly losing energy and the pendulum stops over time.I think the problem is in the length constraint.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>void solveForConstraints(float dt) { // For every lenght constarint
Vector2Custom diffVector = end_p->pos - start_p->pos ;
double currentLen = diffVector.Length();
//double epsilon = 1e-12;
double weird_a = inverseStiffnes / (dt * dt);
if (currentLen != originalLen) {
double lambda = 0;
double lambda_delta;
//cout << " --------------" << endl;
for (int k = 0; k < 8; k++)
{
diffVector = end_p->pos - start_p->pos;
currentLen = diffVector.Length();
Vector2Custom C_dist = diffVector.Normalize() * (currentLen - originalLen);
double C = currentLen - originalLen;
double w1 = start_p->invMass;
double w2 = end_p->invMass;
if ((!start_p->isFixed) && (!end_p->isFixed)) { // w1 / (w1+ w2) * (l-l0 ) * diffvectorNormalized
Vector2Custom delta_C1 = (start_p->pos - end_p->pos).Normalize();
Vector2Custom delta_C2 = (end_p->pos - start_p->pos).Normalize(); // -delta_C1
lambda_delta = (-C - weird_a * lambda) / (w1 + w2 + weird_a);
lambda += lambda_delta;
//cout << "l:" << lambda << endl;
//cout << "l_delta:" << lambda_delta << endl;
//lambda = (-C) / (w1 + w2 + weird_a + epsilon);
Vector2Custom delta_x1 = delta_C1 * lambda_delta * w1;
Vector2Custom delta_x2 = delta_C2 * lambda_delta * w2;
start_p->pos = start_p->pos + delta_x1;
end_p->pos = end_p->pos + delta_x2;
}
else if (!start_p->isFixed) {
Vector2Custom delta_C1 = (start_p->pos - end_p->pos).Normalize();
lambda_delta = (-C - weird_a * lambda) / (w1 + weird_a );
lambda += lambda_delta;
Vector2Custom delta_x1 = delta_C1 * lambda_delta * w1;
start_p->pos = start_p->pos + delta_x1;
}
else if (!end_p->isFixed) {
Vector2Custom delta_C2 = (end_p->pos - start_p->pos).Normalize();
lambda_delta = (-C - weird_a * lambda) / (w2 + weird_a );
lambda += lambda_delta;
Vector2Custom delta_x2 = delta_C2 * lambda_delta * w2;
end_p->pos = end_p->pos + delta_x2;
}
}
}
}
</code>
<code>void solveForConstraints(float dt) { // For every lenght constarint Vector2Custom diffVector = end_p->pos - start_p->pos ; double currentLen = diffVector.Length(); //double epsilon = 1e-12; double weird_a = inverseStiffnes / (dt * dt); if (currentLen != originalLen) { double lambda = 0; double lambda_delta; //cout << " --------------" << endl; for (int k = 0; k < 8; k++) { diffVector = end_p->pos - start_p->pos; currentLen = diffVector.Length(); Vector2Custom C_dist = diffVector.Normalize() * (currentLen - originalLen); double C = currentLen - originalLen; double w1 = start_p->invMass; double w2 = end_p->invMass; if ((!start_p->isFixed) && (!end_p->isFixed)) { // w1 / (w1+ w2) * (l-l0 ) * diffvectorNormalized Vector2Custom delta_C1 = (start_p->pos - end_p->pos).Normalize(); Vector2Custom delta_C2 = (end_p->pos - start_p->pos).Normalize(); // -delta_C1 lambda_delta = (-C - weird_a * lambda) / (w1 + w2 + weird_a); lambda += lambda_delta; //cout << "l:" << lambda << endl; //cout << "l_delta:" << lambda_delta << endl; //lambda = (-C) / (w1 + w2 + weird_a + epsilon); Vector2Custom delta_x1 = delta_C1 * lambda_delta * w1; Vector2Custom delta_x2 = delta_C2 * lambda_delta * w2; start_p->pos = start_p->pos + delta_x1; end_p->pos = end_p->pos + delta_x2; } else if (!start_p->isFixed) { Vector2Custom delta_C1 = (start_p->pos - end_p->pos).Normalize(); lambda_delta = (-C - weird_a * lambda) / (w1 + weird_a ); lambda += lambda_delta; Vector2Custom delta_x1 = delta_C1 * lambda_delta * w1; start_p->pos = start_p->pos + delta_x1; } else if (!end_p->isFixed) { Vector2Custom delta_C2 = (end_p->pos - start_p->pos).Normalize(); lambda_delta = (-C - weird_a * lambda) / (w2 + weird_a ); lambda += lambda_delta; Vector2Custom delta_x2 = delta_C2 * lambda_delta * w2; end_p->pos = end_p->pos + delta_x2; } } } } </code>
void solveForConstraints(float dt) { // For every lenght constarint
    Vector2Custom diffVector = end_p->pos - start_p->pos ;
    double currentLen = diffVector.Length();
    //double epsilon = 1e-12;
    double weird_a = inverseStiffnes / (dt * dt);

    
    if (currentLen != originalLen) {


        double lambda = 0;
        double lambda_delta;

        //cout << " --------------" << endl;
        for (int k = 0; k < 8; k++)
        {

            diffVector = end_p->pos - start_p->pos;
            currentLen = diffVector.Length();

            Vector2Custom C_dist = diffVector.Normalize() * (currentLen - originalLen);

            double C = currentLen - originalLen;
            double w1 = start_p->invMass;
            double w2 = end_p->invMass;

            

            if ((!start_p->isFixed) && (!end_p->isFixed)) { // w1 / (w1+ w2) * (l-l0 ) * diffvectorNormalized

                Vector2Custom delta_C1 = (start_p->pos - end_p->pos).Normalize();
                Vector2Custom delta_C2 = (end_p->pos - start_p->pos).Normalize(); // -delta_C1

                
                lambda_delta = (-C - weird_a * lambda) / (w1 + w2 + weird_a);
                lambda += lambda_delta;
                //cout << "l:" << lambda << endl;
                //cout << "l_delta:" << lambda_delta << endl;
                
                //lambda = (-C) / (w1 + w2 + weird_a + epsilon);

                Vector2Custom delta_x1 = delta_C1 * lambda_delta * w1;
                Vector2Custom delta_x2 = delta_C2 * lambda_delta * w2;

                start_p->pos = start_p->pos + delta_x1;
                end_p->pos = end_p->pos + delta_x2;


            }
            else if (!start_p->isFixed) {

                Vector2Custom delta_C1 = (start_p->pos - end_p->pos).Normalize();

                lambda_delta = (-C - weird_a * lambda) / (w1 + weird_a );
                lambda += lambda_delta;



                Vector2Custom delta_x1 = delta_C1 * lambda_delta * w1;

                start_p->pos = start_p->pos + delta_x1;


            }
            else if (!end_p->isFixed) {

                Vector2Custom delta_C2 = (end_p->pos - start_p->pos).Normalize();



                lambda_delta = (-C - weird_a * lambda) / (w2 + weird_a );
                lambda += lambda_delta;

                Vector2Custom delta_x2 = delta_C2 * lambda_delta * w2;

                end_p->pos = end_p->pos + delta_x2;


            }
        }
    }
}

I’m trying to simulate a double pendulum where I can move the center point . The simulation works fine and behaves normaly, but its speed decreases over time. Help me.

New contributor

Hubble is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật