-2

I'm a complete beginner with C++, and I have an assignement to do, may I ask for a little bit of help ?

So my project consist in creating a bouncing ball, particle explosion and people walking, all in 3D using GLM. I have to use gravity and all of those stuff but i don't understand how to implement it. Here is my code for the bouncing ball :

 glm::mat4 mv_matrix_sphere =
glm::translate(glm::vec3(-2.0f, 0.5f + t, 0.0f)) *
glm::rotate(-t, glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::rotate(-t, glm::vec3(1.0f, 0.0f, 0.0f)) *
glm::mat4(1.0f);
mySphere.mv_matrix = myGraphics.viewMatrix * mv_matrix_sphere;
mySphere.proj_matrix = myGraphics.proj_matrix;
if (t == 0.5) {
    while (t != 0) {
        t -= f; // increment movement variable
    }
}
t += 0.01f; // increment movement variable

When I execute, the function is working( the ball is moving but it doesn't stop) so that means that the ball never come back (doesn't bounce) And here my particle explosion :

void particles() {
    vel = new glm::vec3[numParticles];

    for (int i = 0; i < numParticles; i++) {
        vel[i] = glm::normalize(glm::vec3((rand() % 1500 - 750) / 10000.0f, (rand() % 1500 - 750) / 10000.0f, (rand() % 1500 - 750) / 10000.0f));
    }
}

I found this code on internet but it doesn't work and I don't understand why.

I would be really grateful if you guys can help me. Many thanks !

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Vincent Tng
  • 145
  • 4
  • 12
  • 4
    What exactly is your question? – HolyBlackCat Feb 14 '19 at 13:31
  • 4
    "I found this code on internet but it doesn't work" then dont use it ?!? Taking random code from the internet without understanding what it does in detail is not a good idea. – 463035818_is_not_an_ai Feb 14 '19 at 13:32
  • Back when I learned C++, "a complete beginner with C++" typically spent 2-3 years learning the fundamentals of the language: inheritance, templates, the C++ library, etc... And that was before C++11, which made modern C++ about 3-5 times bigger and more complicated than it used to be. I think that "a complete beginner with C++" [needs to spend several years with a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) before diving into a subject like this one. – Sam Varshavchik Feb 14 '19 at 13:37
  • Why would the ball bounce? There's no provisions in the code that make it do so. – Rietty Feb 14 '19 at 13:37
  • 5
    *""a complete beginner with C++" needs to spend several years with a good C++ book before diving into a subject like this one"* I don't think waiting a few years is truly necessary, but I do agree that "I found this code on the internet, please fix it" is not the way to go. – HolyBlackCat Feb 14 '19 at 13:44
  • 1
    i didnt trigger it, but being the first one commenting in a certain direction, I want to give it a slight positive turn.. of course it is ok to look at code you find online, and it seems you are trying to understand what it does, which is good. However, we cannot know what is your state of knowledge so when you say things like "i dont understand" or "please help" we have literally no idea what exactly is your problem. Try to ask a concrete specific problem. Explaining you each and every detail that a complete beginner would need to fully understand that code is way too broad to be covered here – 463035818_is_not_an_ai Feb 14 '19 at 13:51

1 Answers1

0

I think I remember doing something like this back when I first started out. With the code provided I would say that you need to probably add a directional property with the value of -1 or 1 to help decide which direction the ball needs to go. I think that's what this code is trying to do but I would make a few modifications to your if statement. Depending on the direction you want the ball to go initially and then modify the end.

if (t >= 0.5) {
    d = -1;
}
else if (t <= 0){
    d = 1;
}
t += 0.01f * d; // increment movement variable