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.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      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