Help condensing nested if blocks
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Help condensing nested if blocks

  1. #1
    Senior Member axcho's Avatar
    Join Date
    Jun 2006
    Posts
    113

    Help condensing nested if blocks

    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:
    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;
    		}
    	}
    }
    Thanks in advance!

  2. #2
    Senior Member axcho's Avatar
    Join Date
    Jun 2006
    Posts
    113
    Any suggestions? I'm using AS2, Flash Player 8. Thanks.

  3. #3
    :
    Join Date
    Dec 2002
    Posts
    3,518
    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
    w0 = (n0.m) ? 0 : 1;
    w1 = (n1.m) ? 0 : 1;
    w2 = (n2.m) ? 0 : 1;

  4. #4
    Senior Member axcho's Avatar
    Join Date
    Jun 2006
    Posts
    113
    Hmm, I'm afraid it's more complicated than that... I appreciate the reply though.

    Basically if (n0.m == 0) then I want to set (n1.w = n2.w = 0) and (n0.w = 1). Is that possible to do in a simpler way than above?

  5. #5
    Junior Member
    Join Date
    Dec 2008
    Posts
    6
    Well, those conditional statements that dawsonk suggested can be calls to functions instead of values. The functions can set the variables to what you want.
    Code:
    w0 = (n0.m) ? setWeights1() : setWeights2() ;
    so make the functions set the values. Obviously the variables need to be global. You can set up those conditional statements to suit your needs. I didn't spend much time looking at this solution and I'm not sure by your last reponse if the conditionals should be w0= or n0.m == 0. if the latter

    Code:
    n0.m == 0 ? setWeights1() : setWeights2() ;
    So maybe you can figure if conditional statements can work for you. If not, maybe else if statements might be better

    Code:
    if ( n0.m && n1.m && n2.m ){
    	// keep weights as they are
    		}
    else if ( n0.m && n1.m && !n2.m ){
    	// only node 2 should move
    	w0 = w1 = 0;
    	w2 = 1;
    		}
    
    else if ( n0.m && !n1.m && n2.m ){
    	// only node 1 should move
    	w0 = w2 = 0;
    	w1 = 1;
    		}
    
    else if ( n0.m && !n1.m && !n2.m ){
    	// node 1 and node 2 should move
    	w0 = 0;
    	w1 = w2 = 1;
    		}
    
    else if ( !n0.m && n1.m && n2.m ){
    	// only node 0 should move
    	w1 = w2 = 0;
    	w0 = 1;
    		}
    
    else if ( !n0.m && n1.m && !n2.m ){
    	// node 0 and node 2 should move
    	w1 = 0;
    	w0 = w2 = 1;
    		}
    
    else if ( !n0.m && !n1.m && n2.m ){
    	// node 0 and node 1 should move
    	w2 = 0;
    	w0 = w1 = 1;
    		}
    
    else if ( !n0.m && !n1.m && !n2.m ){
    	// all nodes are equal
    	w0 = w1 = w2 = 1;
    		}
    Hope this helps

  6. #6
    Senior Member axcho's Avatar
    Join Date
    Jun 2006
    Posts
    113
    Thank you, that helps. I will compare my options.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center

Poll by Flashkit.com