Figure this piece of code out



  • Hi, I'm having trouble figuring some piece of C++ 2D rigid body dynamics code.



    Forgive me, since I don't know how to use code tags on this forum.



    Vector is a class, and have two fields, X and Y, both floats. Operators (, /, ^, +, -, etc) are all overloaded, more info on this later.



    The piece of code I'm having trouble is this:



    Vector D = [another random calculated vector]; //(not relevant to my trouble)

    float n = D * N; // N is also vector. D * N results in n storing the dot product between D and N // (not relevant to my trouble)

    Vector Dn = N * n; // not relevant. Does scalar multiplication

    Vector Dt = D - Dn; // not relevant. Subtracts Dn from D

    if (n > 0.0f) Dn = Vector(0, 0);

    float dt = Dt * Dt; // dot product, again not relevant

    float CoF = s_fFriction; // << s_fFriction is a float

    if (dt < s_fGlue
    s_fGlue) CoF = 1.01f; << s_fGlue and CoF are floats

    D = -(1.0f + s_fRestitution) * Dn - (CoF) * Dt; // << What I'm having trouble



    I'm having trouble in:

    D = -(1.0f + s_fRestitution) * Dn - (CoF) * Dt;



    Specifically, what's going on, and in what order.



    D is a vector, and gets assigned. I can't figure out in what order the operations are taking place:

    First: (1 + s_fRestitution)

    Second: (1.0f + s_fRestitution)* Dn

    Third: (CoF) * Dt

    Fourth: (1.0f + s_fRestitution) * Dn - (CoF) * Dt

    Fifth: negates everything.




    Is this right?



    The "-" in the beggining, is negating the vector?

    Both - and * operators are overloaded:

    inline Vector operator - (const Vector &V)const {return Vector(x-V.x, y-V.y); }

    inline float operator * (const Vector &V)const{return (xV.x) + (yV.y); } // dot product

    inline Vector operator * (float s)const{return Vector(xs, ys); }



    What's the use of parentesis in the (CoF)?



    What are the order of operations on this line of code?



    Thanks



  • I can't edit my post anymore. Somethings are still fucked up, gogogo pastebin
    http://pastebin.com/f18d789d1



  • @mauricio said:

    Fifth: negates everything.


    Is this right?
     

    No, I'm pretty sure that the - at the beginning is a unary minus operator, which binds stronger than multiplication whic binds stronger than addition/subtraction.

     @mauricio said:

    What's the use of parentesis in the (CoF)?

    They look redundant to me.

    @mauricio said:

    What are the order of operations on this line of code?

    Here's my attempt :)

    D = -(1.0f + s_fRestitution) * Dn - (CoF) * Dt;

    D1 = -(1.0f + s_fRestitution) * Dn = (-(1.0f + s_fRestitution)) * Dn

    D2 = (CoF) * Dt

    D = D1 - D2

    I don't know if that helps make sense of the intentions of the code :)



  • Overloading operators doesn't affect precedence.

    Oh, and the indentation is all messed up. Is it like this in the original code?



  • The identation is messed thanks to the forum software, I've used pastebin after using the forum. Don't pay attention to this.



    Ok, overloading operators doesn't affect precedence.

    Now, I need to figure out what operators are being called - operations on floats or vectors?



    See again:

    D = -(1.0f + s_fRestitution) * Dn - (CoF) * Dt



    It's processing in this way:

    1. -(1.0f + s_fRestitution) = one_plus_restitution: This is a floating point operation
    2. one_plus_restitution * Dn : This is calling the overloaded * operator: inline Vector operator * (float s)const{return Vector(xs, ys); }

      Resulting in a new vector lets call it D1
    3. (CoF) * Dt : Another vector operation, using the * overloaded operator, let's call it D2
    4. D1 - D2: vector operation


    Which is pretty much what Nandurius pointed out, and since I couldn't get this to compile it to test this myself, and I'm porting it to Delphi, I have no practical way to test if this is actually working (because there are others bugs in the port, that may or may not be caused by this line of code alone, so it's not so simple as it seems).



    Thanks for the help



  •  It probably helps to know why the calculations are being done, not just how it is calculated. From a quick google search of restitution and friction I'm assuming that this is calculating bounce of some object (most references to the coefficient of restitution have been to bouncing tennis balls :P)

    First guess: This code calculates how an object bounces, or more likely it's calculating bounce as part of an animation, i.e. for a certain time interval ("1 frame").

    What do you need to know to animate a bouncing object? The objects direction and speed after it contacts a surface. This is what the vector represents (angle, magnitute) or (horizontal movement, vertical movement.) Since you mention that the Vector class has X and Y members, it's probably safe to assume that the X and Y forces/speeds/directions are stored.

    Next, the coefficient of restitution determines how much energy is retained after the collision? In any case, its some kind of a percentage, so it's added to 1 to allow scaling a number. (i.e. CoR = 25% = 0.25. Scale a unit vector by multiplying it by 1.25 = 1 + CoF.) This is still a scalar (float). Since the bounce is in the opposite direction (wild guess) the negative value is used. This scale factor is then applied to the vector Dn (which would sensibly hold the current direction of an object). The negative multiplicator reverses the vectors direction and the coefficient reduces the magnitute of the X/Y motion (or, for a positive CoR, speeds it up.)

    The coefficient of friction is the same, CoF is multiplied with vector Dt to scale it. Apparently CoF isn't expressed as a percentage (or has already been ajdusted), so no 1 is added. CoF * Dt then represents how friction affects the motion of the object. Quick guess: it removes energy and slows down the movement of the object, as friction is known to do. Therefore the friction vector is subtracted from the new vector of motion of the object.

    So that means:

    Dn = current object motion

    1 + s_fRestitution = movement change due to bouncing collision

    -1 = direction change

    CoF * Dt = Force of friction on the object

    Vector * scalar = vector (individual components of the vector are scaled). Vector - Vector = Vector (components of the vectors are added/subtracted.)

    Sounds plausible, yes?



  • It's calculating the boucing and sliding of 2D objects, and is a very simple and very limited wanna-be 2D rigid body dynamics, sort of a physics engine, except much much more simpler.


    Yes, you pretty much nailed it down.



    If anyone would like to take a look it's on this package:




    http://uk.geocities.com/olivier_rebellion/Polycolly.zip




    I'm currently on tutorial 4, and you'll be able to locate this funcion inside body.cpp on line 157




    My main problem was figuring out if it was doing floating point operations, or vector operations, mostly because the overloaded operators, mostly the *



    But, I think there's no doubt anymore that the line can be broken down as two parts:

    1. -(1.0f + s_fRestitution) * Dn
    2. (CoF) * Dt

      And subtracting 2) from 1)

    Thank you very much



  • Might be a bit unrelated, but this: http://www.box2d.org/ would be better I guess.


Log in to reply