just a test

Dependencies:   mbed

Fork of scoreLight_Advanced by Alvaro Cassinelli

Committer:
mbedalvaro
Date:
Tue Dec 02 04:28:42 2014 +0000
Revision:
48:7633d8e7b0d3
Parent:
30:d8af03f01cd4
this is the working version of the skin games sowtware (aka, scorelight but with pre-determined "games")

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedalvaro 30:d8af03f01cd4 1 /*
mbedalvaro 30:d8af03f01cd4 2 * pointMass.cpp
mbedalvaro 30:d8af03f01cd4 3 * laserBlob
mbedalvaro 30:d8af03f01cd4 4 *
mbedalvaro 30:d8af03f01cd4 5 * Created by CASSINELLI ALVARO on 5/19/11.
mbedalvaro 30:d8af03f01cd4 6 * Copyright 2011 TOKYO UNIVERSITY. All rights reserved.
mbedalvaro 30:d8af03f01cd4 7 *
mbedalvaro 30:d8af03f01cd4 8 */
mbedalvaro 30:d8af03f01cd4 9
mbedalvaro 30:d8af03f01cd4 10 #include "classPointMass.h"
mbedalvaro 30:d8af03f01cd4 11
mbedalvaro 30:d8af03f01cd4 12 // Initialization of static member variables:
mbedalvaro 30:d8af03f01cd4 13 vector2Df pointMass::maxWall(4095, 4095);
mbedalvaro 30:d8af03f01cd4 14 vector2Df pointMass::minWall(0, 0);
mbedalvaro 30:d8af03f01cd4 15
mbedalvaro 30:d8af03f01cd4 16 //------------------------------------------------------------
mbedalvaro 30:d8af03f01cd4 17 pointMass::pointMass(){
mbedalvaro 30:d8af03f01cd4 18 setIntegrationStep(.01); // default in case we don't call integration step setting
mbedalvaro 30:d8af03f01cd4 19 // NOTE: it is important to set dt before inital conditions in case of VERLET integration, because we need the integration
mbedalvaro 30:d8af03f01cd4 20 // step for properly setting the initial speed.
mbedalvaro 30:d8af03f01cd4 21 setInitialCondition(0,0,0,0);// default in case we don't call to initial conditions.
mbedalvaro 30:d8af03f01cd4 22 setWallLimits(100,100,4000,4000);
mbedalvaro 30:d8af03f01cd4 23 mass=1.0;
mbedalvaro 30:d8af03f01cd4 24 dampMotion = 0.0175;//0.025 was good for bigger blobs;//2f;//0.07f;
mbedalvaro 30:d8af03f01cd4 25 dampBorder = 0.013f; //0.07f
mbedalvaro 30:d8af03f01cd4 26 bFixed = false;
mbedalvaro 30:d8af03f01cd4 27 bWallCollision=false;
mbedalvaro 30:d8af03f01cd4 28 }
mbedalvaro 30:d8af03f01cd4 29
mbedalvaro 30:d8af03f01cd4 30 //------------------------------------------------------------
mbedalvaro 30:d8af03f01cd4 31 void pointMass::resetForce(){
mbedalvaro 30:d8af03f01cd4 32 totalForce.set(0,0);
mbedalvaro 30:d8af03f01cd4 33 }
mbedalvaro 30:d8af03f01cd4 34
mbedalvaro 30:d8af03f01cd4 35 //------------------------------------------------------------
mbedalvaro 30:d8af03f01cd4 36 void pointMass::addForce(float x, float y){
mbedalvaro 30:d8af03f01cd4 37 totalForce.x = totalForce.x + x;
mbedalvaro 30:d8af03f01cd4 38 totalForce.y = totalForce.y + y;
mbedalvaro 30:d8af03f01cd4 39 }
mbedalvaro 30:d8af03f01cd4 40
mbedalvaro 30:d8af03f01cd4 41 //------------------------------------------------------------
mbedalvaro 30:d8af03f01cd4 42 void pointMass::addForce(vector2Df forceToAdd){
mbedalvaro 30:d8af03f01cd4 43 totalForce+=forceToAdd;
mbedalvaro 30:d8af03f01cd4 44 }
mbedalvaro 30:d8af03f01cd4 45
mbedalvaro 30:d8af03f01cd4 46 //------------------------------------------------------------
mbedalvaro 30:d8af03f01cd4 47 void pointMass::addInvSquareForce(float x, float y, float radiusMin, float radiusMax, float scale){
mbedalvaro 30:d8af03f01cd4 48
mbedalvaro 30:d8af03f01cd4 49 vector2Df posOfForce;
mbedalvaro 30:d8af03f01cd4 50 posOfForce.set(x,y);
mbedalvaro 30:d8af03f01cd4 51
mbedalvaro 30:d8af03f01cd4 52 vector2Df diff = pos - posOfForce; // note: we use the position AT TIME T, so this force is at time t
mbedalvaro 30:d8af03f01cd4 53 float length = diff.length();
mbedalvaro 30:d8af03f01cd4 54
mbedalvaro 30:d8af03f01cd4 55 // check close enough and far enough (to avoid singularities for example):
mbedalvaro 30:d8af03f01cd4 56 if ((length>radiusMin)&&(length<radiusMax)) {
mbedalvaro 30:d8af03f01cd4 57 diff.normalize();
mbedalvaro 30:d8af03f01cd4 58 totalForce += diff * scale * 1.0/(length*length+1);
mbedalvaro 30:d8af03f01cd4 59 }
mbedalvaro 30:d8af03f01cd4 60 }
mbedalvaro 30:d8af03f01cd4 61
mbedalvaro 30:d8af03f01cd4 62 void pointMass::addInterInvSquareForce(pointMass &theOtherParticle, float radiusMin, float radiusMax, float scale){
mbedalvaro 30:d8af03f01cd4 63
mbedalvaro 30:d8af03f01cd4 64 vector2Df posOfForce;
mbedalvaro 30:d8af03f01cd4 65 posOfForce.set(theOtherParticle.pos);
mbedalvaro 30:d8af03f01cd4 66
mbedalvaro 30:d8af03f01cd4 67 vector2Df diff = pos - posOfForce; // note: we use the position AT TIME T, so this force is at time t
mbedalvaro 30:d8af03f01cd4 68 float length = diff.length();
mbedalvaro 30:d8af03f01cd4 69
mbedalvaro 30:d8af03f01cd4 70 // check close enough and far enough (to avoid singularities for example):
mbedalvaro 30:d8af03f01cd4 71 if ((length>radiusMin)&&(length<radiusMax)) {
mbedalvaro 30:d8af03f01cd4 72 diff.normalize();
mbedalvaro 30:d8af03f01cd4 73 totalForce += diff * scale * 1.0/(length*length+1);
mbedalvaro 30:d8af03f01cd4 74 theOtherParticle.totalForce -= diff * scale * 1.0/(length*length+1);
mbedalvaro 30:d8af03f01cd4 75 }
mbedalvaro 30:d8af03f01cd4 76 }
mbedalvaro 30:d8af03f01cd4 77
mbedalvaro 30:d8af03f01cd4 78
mbedalvaro 30:d8af03f01cd4 79 //------------------------------------------------------------
mbedalvaro 30:d8af03f01cd4 80 void pointMass::addSpringForce(float centerx, float centery, float radius, float scale){
mbedalvaro 30:d8af03f01cd4 81
mbedalvaro 30:d8af03f01cd4 82 // ----------- (1) make a vector of where this particle p is:
mbedalvaro 30:d8af03f01cd4 83 vector2Df posOfForce;
mbedalvaro 30:d8af03f01cd4 84 posOfForce.set(centerx, centery);
mbedalvaro 30:d8af03f01cd4 85
mbedalvaro 30:d8af03f01cd4 86 // ----------- (2) calculate the difference & length
mbedalvaro 30:d8af03f01cd4 87
mbedalvaro 30:d8af03f01cd4 88 vector2Df diff = pos - posOfForce;
mbedalvaro 30:d8af03f01cd4 89 float length = diff.length();
mbedalvaro 30:d8af03f01cd4 90
mbedalvaro 30:d8af03f01cd4 91 // ----------- (3) check close enough
mbedalvaro 30:d8af03f01cd4 92
mbedalvaro 30:d8af03f01cd4 93 bool bAmCloseEnough = true;
mbedalvaro 30:d8af03f01cd4 94 if (radius > 0){
mbedalvaro 30:d8af03f01cd4 95 if (length > radius){
mbedalvaro 30:d8af03f01cd4 96 bAmCloseEnough = false;
mbedalvaro 30:d8af03f01cd4 97 }
mbedalvaro 30:d8af03f01cd4 98 }
mbedalvaro 30:d8af03f01cd4 99
mbedalvaro 30:d8af03f01cd4 100 // ----------- (4) if so, update force
mbedalvaro 30:d8af03f01cd4 101
mbedalvaro 30:d8af03f01cd4 102 if (bAmCloseEnough == true){
mbedalvaro 30:d8af03f01cd4 103 float pct = 1 - (length / radius); // stronger on the inside
mbedalvaro 30:d8af03f01cd4 104 diff.normalize();
mbedalvaro 30:d8af03f01cd4 105 totalForce += diff * scale * pct;
mbedalvaro 30:d8af03f01cd4 106 }
mbedalvaro 30:d8af03f01cd4 107 }
mbedalvaro 30:d8af03f01cd4 108
mbedalvaro 30:d8af03f01cd4 109 void pointMass::addInterSpringForce(pointMass &theOtherParticle, float radius, float scale){
mbedalvaro 30:d8af03f01cd4 110
mbedalvaro 30:d8af03f01cd4 111 // ----------- (1) make a vector of where this particle p is:
mbedalvaro 30:d8af03f01cd4 112 vector2Df posOfForce;
mbedalvaro 30:d8af03f01cd4 113 posOfForce.set(theOtherParticle.pos);
mbedalvaro 30:d8af03f01cd4 114
mbedalvaro 30:d8af03f01cd4 115 // ----------- (2) calculate the difference & length
mbedalvaro 30:d8af03f01cd4 116
mbedalvaro 30:d8af03f01cd4 117 vector2Df diff = pos - posOfForce;
mbedalvaro 30:d8af03f01cd4 118 float length = diff.length();
mbedalvaro 30:d8af03f01cd4 119
mbedalvaro 30:d8af03f01cd4 120 // ----------- (3) check close enough
mbedalvaro 30:d8af03f01cd4 121
mbedalvaro 30:d8af03f01cd4 122 bool bAmCloseEnough = true;
mbedalvaro 30:d8af03f01cd4 123 if (radius > 0){
mbedalvaro 30:d8af03f01cd4 124 if (length > radius){
mbedalvaro 30:d8af03f01cd4 125 bAmCloseEnough = false;
mbedalvaro 30:d8af03f01cd4 126 }
mbedalvaro 30:d8af03f01cd4 127 }
mbedalvaro 30:d8af03f01cd4 128
mbedalvaro 30:d8af03f01cd4 129 // ----------- (4) if so, update REPULSIVE force
mbedalvaro 30:d8af03f01cd4 130
mbedalvaro 30:d8af03f01cd4 131 if (bAmCloseEnough == true){
mbedalvaro 30:d8af03f01cd4 132 float pct = 1 - (length / radius);
mbedalvaro 30:d8af03f01cd4 133 diff.normalize();
mbedalvaro 30:d8af03f01cd4 134 totalForce += diff * scale * pct;
mbedalvaro 30:d8af03f01cd4 135 theOtherParticle.totalForce -= diff * scale * pct;
mbedalvaro 30:d8af03f01cd4 136 //theOtherParticle.frc.x = p.frc.x - diff.x * scale * pct;
mbedalvaro 30:d8af03f01cd4 137 //theOtherParticle.frc.y = p.frc.y - diff.y * scale * pct;
mbedalvaro 30:d8af03f01cd4 138 }
mbedalvaro 30:d8af03f01cd4 139 }
mbedalvaro 30:d8af03f01cd4 140
mbedalvaro 30:d8af03f01cd4 141
mbedalvaro 30:d8af03f01cd4 142
mbedalvaro 30:d8af03f01cd4 143 //------------------------------------------------------------
mbedalvaro 30:d8af03f01cd4 144 void pointMass::addDampingForce(){ // NOTE: use only in case of EULER intgration!
mbedalvaro 30:d8af03f01cd4 145 totalForce-= speed* dampMotion;
mbedalvaro 30:d8af03f01cd4 146 }
mbedalvaro 30:d8af03f01cd4 147
mbedalvaro 30:d8af03f01cd4 148 //------------------------------------------------------------
mbedalvaro 30:d8af03f01cd4 149 void pointMass::setIntegrationStep(float _dt){
mbedalvaro 30:d8af03f01cd4 150 dt=_dt;
mbedalvaro 30:d8af03f01cd4 151 }
mbedalvaro 30:d8af03f01cd4 152
mbedalvaro 30:d8af03f01cd4 153 //------------------------------------------------------------
mbedalvaro 30:d8af03f01cd4 154 void pointMass::setInitialCondition(vector2Df _pos, vector2Df _speed) {
mbedalvaro 30:d8af03f01cd4 155 setInitialCondition(_pos.x, _pos.y, _speed.x, _speed.y);
mbedalvaro 30:d8af03f01cd4 156 }
mbedalvaro 30:d8af03f01cd4 157 void pointMass::setInitialCondition(float px, float py, float vx, float vy){
mbedalvaro 30:d8af03f01cd4 158 #ifndef VERLET_METHOD
mbedalvaro 30:d8af03f01cd4 159 pos.set(px,py);
mbedalvaro 30:d8af03f01cd4 160 speed.set(vx,vy);
mbedalvaro 30:d8af03f01cd4 161 #else
mbedalvaro 30:d8af03f01cd4 162 // In case of Verlet method, setting the speed is a little more complicated. It involves in particular the integration step
mbedalvaro 30:d8af03f01cd4 163 // through the approximation formula:
mbedalvaro 30:d8af03f01cd4 164 // speed = (posNew-posOld)/(2*dt), or speed=(pos-posOld)/dt. Hence:
mbedalvaro 30:d8af03f01cd4 165 posOld.set(px, py);
mbedalvaro 30:d8af03f01cd4 166 setSpeed(vx, vy); // this assumes posOld known
mbedalvaro 30:d8af03f01cd4 167 #endif
mbedalvaro 30:d8af03f01cd4 168 }
mbedalvaro 30:d8af03f01cd4 169
mbedalvaro 30:d8af03f01cd4 170 //-------------------------------------------------------
mbedalvaro 30:d8af03f01cd4 171 vector2Df pointMass::getSpeed() {
mbedalvaro 30:d8af03f01cd4 172 // this will give an estimate of the speed (not computed explicitly using the Verlet integration):
mbedalvaro 30:d8af03f01cd4 173 //speed=(posNew-posOld)/(2*dt); // the variable speed is also updated (note: it is private)
mbedalvaro 30:d8af03f01cd4 174 speed=(pos-posOld)/dt; // less approximate than the above, but we avoid having a global posNew variable (remember we will have many particles...)
mbedalvaro 30:d8af03f01cd4 175 return(speed);
mbedalvaro 30:d8af03f01cd4 176 }
mbedalvaro 30:d8af03f01cd4 177
mbedalvaro 30:d8af03f01cd4 178 void pointMass::setSpeed(const vector2Df& vel) { // VERY IMPORTANT! in the case of verlet integration, we need to set dt BEFORE setting the initial speed.
mbedalvaro 30:d8af03f01cd4 179 speed.set(vel); // enough for EULER METHOD
mbedalvaro 30:d8af03f01cd4 180
mbedalvaro 30:d8af03f01cd4 181 // NECESSARY for VERLET METHOD (we assume posOld known):
mbedalvaro 30:d8af03f01cd4 182 // pos=speed*dt+posOld; // when dampMotion=0 (no damping)
mbedalvaro 30:d8af03f01cd4 183 // With damping:
mbedalvaro 30:d8af03f01cd4 184 // we have: speed=(posNew-posOld)/(2*dt) and posNew=pos*(2.0-dampMotion)-posOld*(1.0-dampMotion), so:
mbedalvaro 30:d8af03f01cd4 185 // pos=(speed*2*dt+posOld+posOld*(1.0-dampMotion))/(2.0-dampMotion);
mbedalvaro 30:d8af03f01cd4 186 pos=speed*2*dt/(2.0-dampMotion)+posOld;
mbedalvaro 30:d8af03f01cd4 187
mbedalvaro 30:d8af03f01cd4 188 // no need to compute newPos
mbedalvaro 30:d8af03f01cd4 189 }
mbedalvaro 30:d8af03f01cd4 190
mbedalvaro 30:d8af03f01cd4 191 void pointMass::setSpeed(float vx, float vy) { // VERY IMPORTANT! in the case of verlet integration, we need to set dt BEFORE setting the initial speed.
mbedalvaro 30:d8af03f01cd4 192 speed.set(vx, vy); // enough for EULER METHOD
mbedalvaro 30:d8af03f01cd4 193
mbedalvaro 30:d8af03f01cd4 194 // NECESSARY for VERLET METHOD (we assume posOld known):
mbedalvaro 30:d8af03f01cd4 195 // pos=speed*dt+posOld; // when dampMotion=0 (no damping)
mbedalvaro 30:d8af03f01cd4 196 // With damping:
mbedalvaro 30:d8af03f01cd4 197 // we have: speed=(posNew-posOld)/(2*dt) and posNew=pos*(2.0-dampMotion)-posOld*(1.0-dampMotion), so:
mbedalvaro 30:d8af03f01cd4 198 // pos=(speed*2*dt+posOld+posOld*(1.0-dampMotion))/(2.0-dampMotion);
mbedalvaro 30:d8af03f01cd4 199 pos=speed*2*dt/(2.0-dampMotion)+posOld;
mbedalvaro 30:d8af03f01cd4 200
mbedalvaro 30:d8af03f01cd4 201 // no need to compute newPos
mbedalvaro 30:d8af03f01cd4 202 }
mbedalvaro 30:d8af03f01cd4 203
mbedalvaro 30:d8af03f01cd4 204 void pointMass::setPos(float px, float py) { // assuming the speed is unchanged (must do some tweaking in case of Verlet integration)
mbedalvaro 30:d8af03f01cd4 205 pos.set(px, py);
mbedalvaro 30:d8af03f01cd4 206 posOld=pos-speed*dt;// no damping! attn...
mbedalvaro 30:d8af03f01cd4 207 }
mbedalvaro 30:d8af03f01cd4 208
mbedalvaro 30:d8af03f01cd4 209 //------------------------------------------------------------
mbedalvaro 30:d8af03f01cd4 210 void pointMass::update(){
mbedalvaro 30:d8af03f01cd4 211 if (bFixed == false){
mbedalvaro 30:d8af03f01cd4 212 acc=totalForce/mass; // this is the acceleration at time t
mbedalvaro 30:d8af03f01cd4 213
mbedalvaro 30:d8af03f01cd4 214 #ifndef VERLET_METHOD
mbedalvaro 30:d8af03f01cd4 215 // The following equations (Euler integration) assume acceleration constant during time dt:
mbedalvaro 30:d8af03f01cd4 216 speed = speed + acc*dt;
mbedalvaro 30:d8af03f01cd4 217 pos = pos + speed*dt ;//+acc*dt*dt*0.5;
mbedalvaro 30:d8af03f01cd4 218 #else
mbedalvaro 30:d8af03f01cd4 219 // acc=0;//
mbedalvaro 30:d8af03f01cd4 220 // The following equations are for VERLET integration with pseudo-damping:
mbedalvaro 30:d8af03f01cd4 221 //Without damping this is just:
mbedalvaro 30:d8af03f01cd4 222 //vector2Df posNew=posOld*2 - pos + acc*dt*dt; // i.e., dampMotion=0;
mbedalvaro 30:d8af03f01cd4 223 // With damping:
mbedalvaro 30:d8af03f01cd4 224 // vector2Df posNew=(pos*(2.0-dampMotion)-posOld*(1.0-dampMotion)+acc*dt*dt); // BAD!!! THIS NOTATION will introduce precision artefacts!!!
mbedalvaro 30:d8af03f01cd4 225 vector2Df posNew=pos+(pos-posOld)*(1-dampMotion)+acc*dt*dt;
mbedalvaro 30:d8af03f01cd4 226
mbedalvaro 30:d8af03f01cd4 227 // ATTENTION: because of the precision of the float or double used, it may be that (pos - posNew) is not 0, when it should ( this produces VERY strange motion artefacts).
mbedalvaro 30:d8af03f01cd4 228 // So, I will test if that difference is smaller than an arbitrary tollerance in this numerical implementation (for each coordinate), and if so, will FORCE posNew to be equal to pos.
mbedalvaro 30:d8af03f01cd4 229 // Note: no need to compute the norm of the difference (in fact, we need the abs difference for each component of the difference vector). Fortunately, nothing is needed here, because we can put the
mbedalvaro 30:d8af03f01cd4 230 // expression in a better way that automatically makes 0 the contribution of something smaller than the precision (so the following is not really necessary)
mbedalvaro 30:d8af03f01cd4 231 // vector2Df diff=(pos-posOld)*(1-dampMotion);
mbedalvaro 30:d8af03f01cd4 232 // Precision correction (separate axis or not):
mbedalvaro 30:d8af03f01cd4 233 // if (abs(diff.x)<0.00001) diff.x=diff.x;
mbedalvaro 30:d8af03f01cd4 234 // if (abs(diff.y)<0.00001) diff.y=diff.y;
mbedalvaro 30:d8af03f01cd4 235 // if (posOld.match(pos, 0.001)==true) {posNew=pos; speed.set(0,0);}
mbedalvaro 30:d8af03f01cd4 236 // vector2Df posNew=pos+diff+acc*dt*dt;
mbedalvaro 30:d8af03f01cd4 237
mbedalvaro 30:d8af03f01cd4 238 posOld=pos;
mbedalvaro 30:d8af03f01cd4 239 pos=posNew;
mbedalvaro 30:d8af03f01cd4 240
mbedalvaro 30:d8af03f01cd4 241 // NOTE: we can also estimate the speed if we want. But this may be unnecessary (call getSpeed() for that).
mbedalvaro 30:d8af03f01cd4 242
mbedalvaro 30:d8af03f01cd4 243 #endif
mbedalvaro 30:d8af03f01cd4 244
mbedalvaro 30:d8af03f01cd4 245 // Constrain speed to max speed (in norm):
mbedalvaro 30:d8af03f01cd4 246 float normSpeed=getSpeed().length();
mbedalvaro 30:d8af03f01cd4 247 if (normSpeed>MAX_PERMISSIBLE_SPEED) {
mbedalvaro 30:d8af03f01cd4 248 setSpeed(getSpeed()*1.0*MAX_PERMISSIBLE_SPEED/normSpeed);
mbedalvaro 30:d8af03f01cd4 249 }
mbedalvaro 30:d8af03f01cd4 250
mbedalvaro 30:d8af03f01cd4 251 }
mbedalvaro 30:d8af03f01cd4 252 }
mbedalvaro 30:d8af03f01cd4 253
mbedalvaro 30:d8af03f01cd4 254 void pointMass::setWallLimits(float Minx, float Miny, float Maxx, float Maxy) {
mbedalvaro 30:d8af03f01cd4 255 maxWall.set(Maxx, Maxy);
mbedalvaro 30:d8af03f01cd4 256 minWall.set(Minx, Miny);
mbedalvaro 30:d8af03f01cd4 257 }
mbedalvaro 30:d8af03f01cd4 258
mbedalvaro 30:d8af03f01cd4 259 //------------------------------------------------------------
mbedalvaro 30:d8af03f01cd4 260 void pointMass::bounceOffWalls(){
mbedalvaro 30:d8af03f01cd4 261 // NOTE: bounce is easy in case of EULER method; in case of VERLET, we need to do some hack on the positions.
mbedalvaro 30:d8af03f01cd4 262 //Note: the walls are in (vector2Dd) horizontalLimits and verticalLimits
mbedalvaro 30:d8af03f01cd4 263
mbedalvaro 30:d8af03f01cd4 264 bWallCollision=false;
mbedalvaro 30:d8af03f01cd4 265 innerCollitionDirection.set(0,0);
mbedalvaro 30:d8af03f01cd4 266 #ifndef VERLET_METHOD // EULER METHOD!!
mbedalvaro 30:d8af03f01cd4 267
mbedalvaro 30:d8af03f01cd4 268 if (pos.x > maxWall.x){
mbedalvaro 30:d8af03f01cd4 269 pos.x = maxWall.x;
mbedalvaro 30:d8af03f01cd4 270 speed.x *= -1;
mbedalvaro 30:d8af03f01cd4 271 bWallCollision = true;
mbedalvaro 30:d8af03f01cd4 272 innerCollitionDirection.x=-1;
mbedalvaro 30:d8af03f01cd4 273 } else if (pos.x < minWall.x){
mbedalvaro 30:d8af03f01cd4 274 pos.x = minWall.x;
mbedalvaro 30:d8af03f01cd4 275 speed.x *= -1;
mbedalvaro 30:d8af03f01cd4 276 bWallCollision = true;
mbedalvaro 30:d8af03f01cd4 277 innerCollitionDirection.x=1;
mbedalvaro 30:d8af03f01cd4 278 }
mbedalvaro 30:d8af03f01cd4 279
mbedalvaro 30:d8af03f01cd4 280 if (pos.y > maxWall.y){
mbedalvaro 30:d8af03f01cd4 281 pos.y = maxWall.y;
mbedalvaro 30:d8af03f01cd4 282 speed.y *= -1;
mbedalvaro 30:d8af03f01cd4 283 bWallCollision = true;
mbedalvaro 30:d8af03f01cd4 284 innerCollitionDirection.y=-1;
mbedalvaro 30:d8af03f01cd4 285 } else if (pos.y < minWall.y){
mbedalvaro 30:d8af03f01cd4 286 pos.y = minWall.y;
mbedalvaro 30:d8af03f01cd4 287 speed.y *= -1;
mbedalvaro 30:d8af03f01cd4 288 bWallCollision = true;
mbedalvaro 30:d8af03f01cd4 289 innerCollitionDirection.y=1;
mbedalvaro 30:d8af03f01cd4 290 }
mbedalvaro 30:d8af03f01cd4 291
mbedalvaro 30:d8af03f01cd4 292 if (bWallCollision) {
mbedalvaro 30:d8af03f01cd4 293 // damping:
mbedalvaro 30:d8af03f01cd4 294 speed *=(1-dampBorder);
mbedalvaro 30:d8af03f01cd4 295 // normalization of collision direction:
mbedalvaro 30:d8af03f01cd4 296 innerCollitionDirection.normalize();
mbedalvaro 30:d8af03f01cd4 297 }
mbedalvaro 30:d8af03f01cd4 298
mbedalvaro 30:d8af03f01cd4 299 #else // THIS IS FOR VERLET METHOD:
mbedalvaro 30:d8af03f01cd4 300 // we need to estimate the inverted, damped vector for bumping::
mbedalvaro 30:d8af03f01cd4 301 vector2Df bumpVector=getSpeed()*dt*(dampBorder-1.0); // assuming dampBorder<1 of course
mbedalvaro 30:d8af03f01cd4 302 if (pos.x > maxWall.x){
mbedalvaro 30:d8af03f01cd4 303 //posOld.x=pos.x;
mbedalvaro 30:d8af03f01cd4 304 //pos.x=pos.x+bumpVector.x;
mbedalvaro 30:d8af03f01cd4 305 posOld.x=maxWall.x;
mbedalvaro 30:d8af03f01cd4 306 pos.x=maxWall.x+bumpVector.x;
mbedalvaro 30:d8af03f01cd4 307 bWallCollision = true; // this is just computed here to detect bumps
mbedalvaro 30:d8af03f01cd4 308 innerCollitionDirection.x=-1;
mbedalvaro 30:d8af03f01cd4 309 } else if (pos.x < minWall.x){
mbedalvaro 30:d8af03f01cd4 310 posOld.x=minWall.x;
mbedalvaro 30:d8af03f01cd4 311 pos.x=minWall.x+bumpVector.x;
mbedalvaro 30:d8af03f01cd4 312 innerCollitionDirection.x=1;
mbedalvaro 30:d8af03f01cd4 313 bWallCollision = true;
mbedalvaro 30:d8af03f01cd4 314 }
mbedalvaro 30:d8af03f01cd4 315
mbedalvaro 30:d8af03f01cd4 316 if (pos.y > maxWall.y){
mbedalvaro 30:d8af03f01cd4 317 posOld.y=maxWall.y;
mbedalvaro 30:d8af03f01cd4 318 pos.y=maxWall.y+bumpVector.y;
mbedalvaro 30:d8af03f01cd4 319 innerCollitionDirection.y=-1;
mbedalvaro 30:d8af03f01cd4 320 bWallCollision = true;
mbedalvaro 30:d8af03f01cd4 321 } else if (pos.y < minWall.y){
mbedalvaro 30:d8af03f01cd4 322 posOld.y=minWall.y;
mbedalvaro 30:d8af03f01cd4 323 pos.y=minWall.y+bumpVector.y;
mbedalvaro 30:d8af03f01cd4 324 innerCollitionDirection.y=1;
mbedalvaro 30:d8af03f01cd4 325 bWallCollision = true;
mbedalvaro 30:d8af03f01cd4 326 }
mbedalvaro 30:d8af03f01cd4 327 #endif
mbedalvaro 30:d8af03f01cd4 328
mbedalvaro 30:d8af03f01cd4 329 }