uifw/ganes/src/HgVgSpring.cpp
changeset 47 2f0c06423c72
parent 46 0e1e0022bd03
child 53 3c67ea82fafc
equal deleted inserted replaced
46:0e1e0022bd03 47:2f0c06423c72
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:    
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 
       
    20 #include "HgVgSpring.h"
       
    21 #include "HgVgConstants.h"
       
    22 
       
    23 #include <e32math.h>
       
    24 
       
    25 using namespace HgVgConstants;
       
    26 
       
    27 
       
    28 THgVgSpring::THgVgSpring(TReal aSpringK, TReal aSpringDamping, 
       
    29                 TReal aSpringMaxVelocity, TReal aPositionSnap,
       
    30                 TReal aVelocitySnap) : 
       
    31 iPrevX(0), 
       
    32 iPrevY(0),
       
    33 iX(0), 
       
    34 iY(0),
       
    35 iEndX(0), 
       
    36 iEndY(0),
       
    37 iVelX(0), 
       
    38 iVelY(0),
       
    39 iAccumulator(0),
       
    40 iSpringK(aSpringK),
       
    41 iSpringDamping(aSpringDamping),
       
    42 iMaxSpringVelocity(aSpringMaxVelocity),
       
    43 iPositionSnap(aPositionSnap),
       
    44 iVelocitySnap(aVelocitySnap)
       
    45     {
       
    46     
       
    47     }
       
    48 
       
    49 void THgVgSpring::SetConstants(TReal aSpringK, TReal aSpringDamping, 
       
    50     TReal aSpringMaxVelocity, TReal aPositionSnap,
       
    51     TReal aVelocitySnap)
       
    52     {
       
    53     iSpringK = aSpringK;
       
    54     iSpringDamping = aSpringDamping;
       
    55     iMaxSpringVelocity = aSpringMaxVelocity;
       
    56     iPositionSnap = aPositionSnap;
       
    57     iVelocitySnap = aVelocitySnap;
       
    58     }
       
    59 
       
    60 
       
    61 void THgVgSpring::Reset(TReal aX, TReal aY)
       
    62     {
       
    63     iEndX = aX;
       
    64     iEndY = aY;
       
    65     Reset();
       
    66     }
       
    67 
       
    68 void THgVgSpring::Reset()
       
    69     {
       
    70     iPrevX = iX = iEndX;
       
    71     iPrevY = iY = iEndY;
       
    72     iAccumulator = 0;
       
    73     iVelX = 0;
       
    74     iVelY = 0;
       
    75     }
       
    76 
       
    77 void THgVgSpring::SetXY(TReal aX, TReal aY)
       
    78     {
       
    79     iPrevX = iX = aX;
       
    80     iPrevY = iY = aY;
       
    81     }
       
    82 
       
    83 void THgVgSpring::SetEnd(TReal aX, TReal aY)
       
    84     {
       
    85     iEndX = aX;
       
    86     iEndY = aY;
       
    87     }
       
    88 
       
    89 void THgVgSpring::SetVelocity(TReal aVx, TReal aVy)
       
    90     {
       
    91     iVelX = aVx;
       
    92     iVelY = aVy;
       
    93     }
       
    94 
       
    95 TBool THgVgSpring::IntegratePhysics(TReal aDeltaTime, TBool aEndCondition)
       
    96     {
       
    97     iAccumulator += aDeltaTime;    
       
    98     while (iAccumulator >= KTimeStep)
       
    99         {
       
   100         iPrevX = iX;
       
   101         iPrevY = iY;
       
   102 
       
   103         // calculate spring force F = -kx - bv
       
   104         TReal Fx = -(iX - iEndX) * iSpringK - iVelX * iSpringDamping;
       
   105         TReal Fy = -(iY - iEndY) * iSpringK - iVelY * iSpringDamping;
       
   106         
       
   107         // do euler integration
       
   108         iVelX += Fx * KTimeStep;
       
   109         iVelY += Fy * KTimeStep;
       
   110                 
       
   111         // limit velocity to certain max
       
   112         if (Abs(iVelX) > iMaxSpringVelocity)
       
   113             iVelX = (iVelX < 0) ? -iMaxSpringVelocity : iMaxSpringVelocity;
       
   114         
       
   115         if (Abs(iVelY) > iMaxSpringVelocity)
       
   116             iVelY = (iVelY < 0) ? -iMaxSpringVelocity : iMaxSpringVelocity;
       
   117 
       
   118         // do euler integration
       
   119         iX += iVelX * KTimeStep;
       
   120         iY += iVelY * KTimeStep;
       
   121         
       
   122         // decrease accumulator
       
   123         iAccumulator -= KTimeStep;
       
   124 
       
   125         // snap to item if close enough and speed is small enough
       
   126         if (Abs(iX - iEndX) < iPositionSnap && Abs(iVelX) < iVelocitySnap &&
       
   127                 Abs(iY - iEndY) < iPositionSnap && Abs(iVelY) < iVelocitySnap && aEndCondition)
       
   128             {
       
   129             Reset();
       
   130             return ETrue;
       
   131             }
       
   132                 
       
   133         }
       
   134     
       
   135     return EFalse;
       
   136     }
       
   137 
       
   138 TReal THgVgSpring::GetX() const
       
   139     {
       
   140     return iX;
       
   141     }
       
   142 
       
   143 TReal THgVgSpring::GetY() const
       
   144     {
       
   145     return iY;
       
   146     }
       
   147 
       
   148 TReal THgVgSpring::EndX() const
       
   149     {
       
   150     return iEndX;
       
   151     }
       
   152 
       
   153 TReal THgVgSpring::EndY() const
       
   154     {
       
   155     return iEndY;
       
   156     }
       
   157 
       
   158 TReal THgVgSpring::VelX() const
       
   159     {
       
   160     return iVelX;
       
   161     }
       
   162 
       
   163 TReal THgVgSpring::VelY() const
       
   164     {
       
   165     return iVelY;
       
   166     }
       
   167 
       
   168 TReal THgVgSpring::GetInterpolatedX() const
       
   169     {
       
   170     TReal a = iAccumulator / KTimeStep;
       
   171     return iX * (1.0 - a) + iPrevX * a;    
       
   172     }
       
   173 
       
   174 TReal THgVgSpring::GetInterpolatedY() const
       
   175     {
       
   176     TReal a = iAccumulator / KTimeStep;
       
   177     return iY * (1.0 - a) + iPrevY * a;
       
   178     }
       
   179 
       
   180 
       
   181 // End of File