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 Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top