Manage marble’s insertion in a curve queue C++ / Gestion insertion bille suivant une courbe C++

I am trying to create a reproduction of the game Zuma in C++ with SFML. I have created a chain of points and made a VertexArray with LineStrip. Each frame, I update the position of each marble using a factor and an index for the point. The factor is used to place the marble between the saved index and the next point. I also have a list of marbles:

vector<Marble> billes;

struct Marble {
    Vector2f coord;
    float size;
    Color color;
    float speed;
    int pathPosition;
    float factor;
    float angle;
};

Here’s the main loop for updating the marbles:

for (size_t i = 0; i < billes.size(); ++i) {
    float distance = calculerDistanceEntrePoints(chemin[billes[i].pathPosition], chemin[billes[i].pathPosition + 1]);
    float vitesseAjustee = vitesse / distance;
    billes[i].coord = interpolerPoints(chemin[billes[i].pathPosition], chemin[billes[i].pathPosition + 1], billes[i].factor);

    if (billes[i].factor >= 1.0f) {
        billes[i].factor = 0.0f;
        billes[i].pathPosition += 1;
        if (billes[i].pathPosition >= chemin.size() - 1) {
            billes.erase(billes.begin() + i);
            --i; // To avoid skipping the next marble
        }
    } else {
        billes[i].factor += (vitesseAjustee * 10) * dt;
    }

    float distanceRestante = (taille / 55) / calculerDistanceEntrePoints(chemin[0], chemin[1]);
    if (i == billes.size() - 1 && billes[i].factor + 0.05 >= distanceRestante * 2 && nombreBilles < 50) {
        billes.push_back(newMarble(chemin[0], taille / 55, 0));
        nombreBilles += 1;
    }
}

Here are the helper functions used:

float calculerDistanceEntrePoints(Vector2f p1, Vector2f p2) {
    Vector2f difference = p2 - p1;
    return std::sqrt(difference.x * difference.x + difference.y * difference.y);
}

Vector2f interpolerPoints(Vector2f pointA, Vector2f pointB, float avancement) {
    return Vector2f(pointA.x + (pointB.x - pointA.x) * avancement, pointA.y + (pointB.y - pointA.y) * avancement);
}

// Function to calculate the position of a marble
Vector2f calculerPositionBille(Marble bille, Vector2f positionCarre) {
    float rayonCercle = bille.size;
    return Vector2f(positionCarre.x - rayonCercle, positionCarre.y - rayonCercle);
}

// Function to calculate a point on a spiral
Vector2f calculerPointSpirale(float angle, float rayon, Vector2f centreFenetre) {
    return Vector2f(centreFenetre.x / 2 + rayon * cos(angle), centreFenetre.y / 2 + rayon * sin(angle));
}

This works well, although I think it could be cleaner. However, I need to shoot marbles and insert them into the chain. It works, but the marble isn’t set at the right point, and the following marbles do not move as they should.


I try to make different function like this one :


void deplacerBilles(vector<Marble>& billes, size_t index, const vector<Vector2f>& chemin) {
    for (int i = index; i >= 0; i--) {
        if (billes[i].pathPosition >= chemin.size() - 1) {
            continue; // Évite les indices invalides
        }

        // Distance entre la position actuelle et la prochaine position sur le chemin
        float distance = calculerDistanceEntrePoints(chemin[billes[i].pathPosition], chemin[billes[i].pathPosition + 1]);

        // Avancer le facteur de déplacement de la bille
        if ((billes[i].factor + billes[i].size / distance) >= 1.0f) {
            // Si la bille dépasse la position actuelle, on passe à la suivante
            billes[i].pathPosition += 1;
            float distanceRestante = (billes[i].factor + (billes[i].size / distance));             // factor actuelle + distance a parcourir = objectif
                                                                                                // mais  >= 1 donc 1-distance restante = distance restante a partir
                                                                                                // de pathPosition+1 et ensuite il faut exprimer cela en fonction 
                                                                                                // de longueur pathPosition et pathPosition +1
            billes[i].factor = (((1 - distanceRestante)*calculerDistanceEntrePoints(chemin[billes[i].pathPosition], chemin[billes[i].pathPosition + 1]))/distance);
            
            if (billes[i].pathPosition >= chemin.size() - 1) {
                // Si la bille dépasse la fin du chemin, on l'enlève
                billes.erase(billes.begin() + i);
            }
        } else {
            billes[i].factor += billes[i].size / distance;
        }
    }
}





bool collisionTab(Marble& bille, vector<Marble>& billes, const vector<Vector2f>& chemin) {
    for (size_t i = 0; i < billes.size(); ++i) {
        if (collision(bille, billes[i])) {
            bille.pathPosition = billes[i].pathPosition;
            bille.factor = billes[i].factor;
            billes.insert(billes.begin() + i, bille);
            deplacerBilles(billes, i, chemin);
            return true; // Collision détectée
        }
    }
    return false;
}


And sorry for the my english level and the french code ahah ^^

Here is a link to the zip file : Zuma.zip

New contributor

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

1

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