I am writing a particle physics engine, and I have a fairly verbose section of code to calculate relative masses when there are massless particles involved. It seems like it should be reducible down to some concise math and fewer if statements. Can you suggest any ways to do this? I'd appreciate the help.
m -> a mass
w -> an inverse mass (1/m) used by the physics engine
when w = 0, the particle has infinite mass and does not move
when w > 0, the particle has some normal amount of mass, unless...
unless m = 0, in which case the particle has no mass
so w values for the other particles must be adjusted to compensate
Here's the relevant code:
Thanks in advance!Code:// calculate relative node weights var w0:Number = n0.w; // node 0 weight var w1:Number = n1.w; // node 1 weight var w2:Number = n2.w; // node 2 weight // check whether nodes are weightless if ( n0.m ) { if ( n1.m ) { if ( n2.m ) { // keep weights as they are } else { // only node 2 should move w0 = w1 = 0; w2 = 1; } } else { if ( n2.m ) { // only node 1 should move w0 = w2 = 0; w1 = 1; } else { // node 1 and node 2 should move w0 = 0; w1 = w2 = 1; } } } else { if ( n1.m ) { if ( n2.m ) { // only node 0 should move w1 = w2 = 0; w0 = 1; } else { // node 0 and node 2 should move w1 = 0; w0 = w2 = 1; } } else { if ( n2.m ) { // node 0 and node 1 should move w2 = 0; w0 = w1 = 1; } else { // all nodes are equal w0 = w1 = w2 = 1; } } }![]()




Reply With Quote