Velocity's value changes when nothing is applied to it O.o

D.V.D

Make a wish
Reaction score
73
So I was trying to make a rigid body dynamics program that simply has objects colliding in a box with each other but I have a small problem. I have my glut render function and in that function I call the update function of my pipeline class. In the update function, all objects are updated which makes them move based on their velocity. The problem however is here, the velocity of the object is 2 for both x and y but the moment I call the physics pipeline update the velocity becomes 0 which doesn't make sense because the velocity is not modified. I used debug statements to display the numbers but I get very weird numbers and its been bugging me for a while. I have 2 header files for my program, one for all the math and object classes while the second header is for the physics pipeline. I then have cpp files for them and my main cpp file with all the glut functions. Heres the code thats been bugging me:

Btw TestStack is also static if that means anything.

Glut Function

Code:
// 4 since each object has 4 vertices creating box
Vector2f VertexStack [TEST_COUNT][4];
// Physics Pipeline
static PhysicsPipeline PS;
 
static void RenderScene () {
   
    glClear(GL_COLOR_BUFFER_BIT);
    // Return values of 2.0, 2.0
    debug(TestStack[0].vel.x);
    debug(TestStack[0].vel.y);
 
    PS.update();
 
    for ( int i = 0; i < TEST_COUNT; i++ ) {
 
        // DEBUG
        if ( i == 1 ) {
            //debug( TestStack[i].pos.x);
            //debug( TestStack[i].pos.y);
        }
        // Update Vertex Stack
        VertexStack[i][0] = TestStack[i].BoundingBox.NW;
        VertexStack[i][1] = TestStack[i].BoundingBox.NE;
        VertexStack[i][2] = TestStack[i].BoundingBox.SW;
        VertexStack[i][3] = TestStack[i].BoundingBox.SE;
        // Sets Quad Color to Black
        glColor3f(0.0f, 0.0f, 0.0f);
        // Draw Quad
        glBegin(GL_TRIANGLE_STRIP);
            glVertex2f(VertexStack[i][0].x,VertexStack[i][0].y);
            glVertex2f(VertexStack[i][1].x,VertexStack[i][1].y);
            glVertex2f(VertexStack[i][2].x,VertexStack[i][2].y);
            glVertex2f(VertexStack[i][3].x,VertexStack[i][3].y);
        glEnd();
 
    }
 
    glutSwapBuffers();
 
}

// Physics Pipeline

Code:
#include "stdafx.h"
#include "Physics Pipeline.h"
 
void PhysicsPipeline::update () {
    // Both returns values of -0.0
    debug(TestStack[0].vel.x);
    debug(TestStack[0].vel.y);
 
    for ( int i=0; i < TEST_COUNT; i++ ) {
 
        TestStack[i].pos.x += TestStack[i].vel.x;
        //printf("POSX: ");
        //debug(pos.x);
        TestStack[i].pos.y += TestStack[i].vel.y;
        //printf("POSY: ");
        //debug(pos.y);
        TestStack[i].BoundingBox.NW = Vector2f( TestStack[i].pos.x + (Box_Radius/2.0f), TestStack[i].pos.y - (Box_Radius/2.0f) );
        TestStack[i].BoundingBox.NE = Vector2f( TestStack[i].pos.x + (Box_Radius/2.0f), TestStack[i].pos.y + (Box_Radius/2.0f) );
        TestStack[i].BoundingBox.SW = Vector2f( TestStack[i].pos.x - (Box_Radius/2.0f), TestStack[i].pos.y - (Box_Radius/2.0f) );
        TestStack[i].BoundingBox.SE = Vector2f( TestStack[i].pos.x - (Box_Radius/2.0f), TestStack[i].pos.y + (Box_Radius/2.0f) );
 
    }
 
    for ( int i=0; i < TEST_COUNT; i++ ) {
 
        for ( int j=0; j < TEST_COUNT; j++ ) {
 
            if ( j != i ) {
 
                if ( TestStack[j].BoundingBox.ispointinbox(TestStack[i].pos) == true ) {
                    // Compute initial momentum
                    Vector2f m1 = Vector2f( ( TestStack[i].mass * TestStack[i].vel.x ) , ( TestStack[i].mass * TestStack[i].vel.y ) );
                    Vector2f m2 = Vector2f( ( TestStack[j].mass * TestStack[j].vel.x ) , ( TestStack[j].mass * TestStack[j].vel.y ) );
                    // Compute magnitudes of both momentums
                    float mag1 = sqrt( ( m1.x * m1.x ) + ( m1.y * m1.y ) );
                    float mag2 = sqrt( ( m2.x * m2.x ) + ( m2.y * m2.y ) );
                    // Compute proportions of momentum transfered
                    float p1, p2;
 
                    if ( mag1 > mag2 ) {
                        p1 = mag2 / mag1;
                        p2 = 1.0f - p1;
                    }
                    if ( mag1 < mag2 ) {
                        p2 = mag1 / mag2;
                        p1 = 1.0f - p2;
                    }
                    if ( mag1 == mag2 ) {
                        p1 = 0.50f;
                        p2 = 0.50f;
                    }
                    // Compute final momentum for both objects
                    Vector2f mf1 = Vector2f( m1.x * p1, m1.y * p1 );
                    Vector2f mf2 = Vector2f( m2.x * p2, m2.y * p2 );
                    // Apply final velocities for both objects
                    TestStack[i].vel.x += mf1.x / TestStack[i].mass;
                    TestStack[i].vel.y += mf1.y / TestStack[i].mass;
                    TestStack[j].vel.x += mf2.x / TestStack[i].mass;
                    TestStack[j].vel.y += mf2.y / TestStack[i].mass;
 
 
                }
 
            }
 
        }
 
        if ( TestStack[i].pos.x >= MAP_X ) {
            TestStack[i].vel.x *= -1.0f;
        }
        if ( TestStack[i].pos.y >= MAP_Y ) {
            TestStack[i].vel.y *= -1.0f;
        }
        if ( TestStack[i].pos.x <= 0.0f ) {
            TestStack[i].vel.x *= -1.0f;
        }
        if ( TestStack[i].pos.y <= 0.0f ) {
            TestStack[i].vel.y *= -1.0f;
        }
 
    }
 
}
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    News portal has been retired. Main page of site goes to Headline News forum now
  • The Helper The Helper:
    I am working on getting access to the old news portal under a different URL for those that would rather use that for news before we get a different news view.
  • Ghan Ghan:
    Easily done
    +1
  • The Helper The Helper:
    https://www.thehelper.net/pages/news/ is a link to the old news portal - i will integrate it into the interface somewhere when i figure it out
  • Ghan Ghan:
    Need to try something
  • Ghan Ghan:
    Hopefully this won't cause problems.
  • Ghan Ghan:
    Hmm
  • Ghan Ghan:
    I have converted the Headline News forum to an Article type forum. It will now show the top 20 threads with more detail of each thread.
  • Ghan Ghan:
    See how we like that.
  • The Helper The Helper:
    I do not see a way to go past the 1st page of posts on the forum though
  • The Helper The Helper:
    It is OK though for the main page to open up on the forum in the view it was before. As long as the portal has its own URL so it can be viewed that way I do want to try it as a regular forum view for a while
  • Ghan Ghan:
    Yeah I'm not sure what the deal is with the pagination.
  • Ghan Ghan:
    It SHOULD be there so I think it might just be an artifact of having an older style.
  • Ghan Ghan:
    I switched it to a "Standard" article forum. This will show the thread list like normal, but the threads themselves will have the first post set up above the rest of the "comments"
  • The Helper The Helper:
    I don't really get that article forum but I think it is because I have never really seen it used on a multi post thread
  • Ghan Ghan:
    RpNation makes more use of it right now as an example: https://www.rpnation.com/news/
  • The Helper The Helper:
  • The Helper The Helper:
    What do you think Tom?
  • tom_mai78101 tom_mai78101:
    I will have to get used to this.
  • tom_mai78101 tom_mai78101:
    The latest news feed looks good
  • The Helper The Helper:
    I would like to see it again like Ghan had it the first time with pagination though - without the pagination that view will not work but with pagination it just might...
  • The Helper The Helper:
    This drink recipe I have had more than a few times back in the day! Mind Eraser https://www.thehelper.net/threads/cocktail-mind-eraser.194720/

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top