Elements used in the Balls and Things games for the RETRO.

Dependents:   RETRO_BallsAndPaddle RETRO_BallAndHoles

Committer:
maxint
Date:
Mon Mar 02 09:58:53 2015 +0000
Revision:
8:19dd2a538cbe
Parent:
2:74bc9b16fb88
more clean-up

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maxint 0:3d0db4e183ee 1 #include "Vector.h"
maxint 0:3d0db4e183ee 2
maxint 0:3d0db4e183ee 3 Vector::Vector()
maxint 0:3d0db4e183ee 4 { // constructor
maxint 0:3d0db4e183ee 5 this->set((float)0,(float)0);
maxint 0:3d0db4e183ee 6 }
maxint 0:3d0db4e183ee 7
maxint 0:3d0db4e183ee 8 Vector::Vector(float fX, float fY)
maxint 0:3d0db4e183ee 9 {
maxint 0:3d0db4e183ee 10 this->set(fX, fY);
maxint 0:3d0db4e183ee 11 }
maxint 0:3d0db4e183ee 12
maxint 0:3d0db4e183ee 13 void Vector::set(float fX, float fY)
maxint 0:3d0db4e183ee 14 {
maxint 0:3d0db4e183ee 15 x=fX;
maxint 0:3d0db4e183ee 16 y=fY;
maxint 0:3d0db4e183ee 17 }
maxint 0:3d0db4e183ee 18 void Vector::set(int nX, int nY)
maxint 0:3d0db4e183ee 19 {
maxint 0:3d0db4e183ee 20 this->set((float)nX, (float)nY);
maxint 0:3d0db4e183ee 21 }
maxint 0:3d0db4e183ee 22
maxint 0:3d0db4e183ee 23 float Vector::getSize()
maxint 0:3d0db4e183ee 24 { // get the size of the vector
maxint 0:3d0db4e183ee 25 return(sqrt(x*x+y*y));
maxint 0:3d0db4e183ee 26 }
maxint 0:3d0db4e183ee 27
maxint 0:3d0db4e183ee 28 bool Vector::isLeft() { return(x<0); }
maxint 0:3d0db4e183ee 29 bool Vector::isRight() { return(x>0); }
maxint 0:3d0db4e183ee 30 bool Vector::isUp() { return(y<0); }
maxint 0:3d0db4e183ee 31 bool Vector::isDown() { return(y>0); }
maxint 0:3d0db4e183ee 32
maxint 0:3d0db4e183ee 33
maxint 0:3d0db4e183ee 34 void Vector::add(float fAdd)
maxint 0:3d0db4e183ee 35 { // add the size of the vector by adding to its size
maxint 0:3d0db4e183ee 36 float fSize=getSize();
maxint 0:3d0db4e183ee 37 float fSizeNew=fSize+fAdd;
maxint 0:3d0db4e183ee 38 if(fSize!=0)
maxint 0:3d0db4e183ee 39 {
maxint 0:3d0db4e183ee 40 x=x * (fSizeNew/fSize);
maxint 0:3d0db4e183ee 41 y=y * (fSizeNew/fSize);
maxint 0:3d0db4e183ee 42 }
maxint 0:3d0db4e183ee 43 else
maxint 0:3d0db4e183ee 44 { // in case of zero lenght, default to addition in first quadrant.
maxint 0:3d0db4e183ee 45 x=sqrt(0.5 * fAdd * fAdd);
maxint 0:3d0db4e183ee 46 y=x;
maxint 0:3d0db4e183ee 47 }
maxint 0:3d0db4e183ee 48 }
maxint 0:3d0db4e183ee 49
maxint 0:3d0db4e183ee 50 void Vector::add(Vector vAdd)
maxint 0:3d0db4e183ee 51 {
maxint 0:3d0db4e183ee 52 x+=vAdd.x;
maxint 0:3d0db4e183ee 53 y+=vAdd.y;
maxint 0:3d0db4e183ee 54 }
maxint 0:3d0db4e183ee 55
maxint 0:3d0db4e183ee 56 void Vector::multiply(Vector vMult)
maxint 0:3d0db4e183ee 57 { // multiply the vector by means of the other vector
maxint 0:3d0db4e183ee 58 x*=vMult.x;
maxint 0:3d0db4e183ee 59 y*=vMult.y;
maxint 2:74bc9b16fb88 60 }
maxint 2:74bc9b16fb88 61
maxint 2:74bc9b16fb88 62 Vector Vector::getNormalized()
maxint 2:74bc9b16fb88 63 { // get the Unit vector (= vector normalized to length 1)
maxint 2:74bc9b16fb88 64 // see http://en.wikipedia.org/wiki/Unit_vector
maxint 2:74bc9b16fb88 65 return(Vector(x/getSize(), y/getSize()));
maxint 2:74bc9b16fb88 66 }
maxint 2:74bc9b16fb88 67
maxint 2:74bc9b16fb88 68 Vector Vector::getNormal()
maxint 2:74bc9b16fb88 69 { // get the normal Vector
maxint 2:74bc9b16fb88 70 // see http://stackoverflow.com/questions/14885693/how-do-you-reflect-a-vector-over-another-vector
maxint 2:74bc9b16fb88 71 // vec.leftNormal --> vx = vec.vy; vy = -vec.vx;
maxint 2:74bc9b16fb88 72 // vec.rightNormal --> vx = -vec.vy; vy = vec.vx;
maxint 2:74bc9b16fb88 73 if(isLeft())
maxint 2:74bc9b16fb88 74 return(Vector(y, -1 * x));
maxint 2:74bc9b16fb88 75 else
maxint 2:74bc9b16fb88 76 return(Vector(-1 * y, x));
maxint 2:74bc9b16fb88 77 }
maxint 2:74bc9b16fb88 78
maxint 2:74bc9b16fb88 79 void Vector::bounce(Vector vBounce)
maxint 2:74bc9b16fb88 80 { // bounce a vector against another vector
maxint 2:74bc9b16fb88 81 // see http://stackoverflow.com/questions/14885693/how-do-you-reflect-a-vector-over-another-vector
maxint 2:74bc9b16fb88 82 Vector vBounceNormalized=vBounce.getNormalized();
maxint 2:74bc9b16fb88 83 Vector vBounceNormal=vBounce.getNormal();
maxint 2:74bc9b16fb88 84 Vector vBounceNormalNormalized=vBounceNormal.getNormalized();
maxint 2:74bc9b16fb88 85
maxint 2:74bc9b16fb88 86 // 1. Find the dot product of vec1 and vec2
maxint 2:74bc9b16fb88 87 // Note: dx and dy are vx and vy divided over the length of the vector (magnitude)
maxint 2:74bc9b16fb88 88 float dpA = x * vBounceNormalized.x + y * vBounceNormalized.y;
maxint 2:74bc9b16fb88 89
maxint 2:74bc9b16fb88 90 // 2. Project vec1 over vec2
maxint 2:74bc9b16fb88 91 float prA_vx = dpA * vBounceNormalized.x;
maxint 2:74bc9b16fb88 92 float prA_vy = dpA * vBounceNormalized.y;
maxint 2:74bc9b16fb88 93
maxint 2:74bc9b16fb88 94 // 3. Find the dot product of vec1 and vec2's normal
maxint 2:74bc9b16fb88 95 float dpB = x * vBounceNormalNormalized.x + y * vBounceNormalNormalized.y;
maxint 2:74bc9b16fb88 96
maxint 2:74bc9b16fb88 97 // 4. Project vec1 over vec2's left normal
maxint 2:74bc9b16fb88 98 float prB_vx = dpB * vBounceNormalNormalized.x;
maxint 2:74bc9b16fb88 99 float prB_vy = dpB * vBounceNormalNormalized.y;
maxint 2:74bc9b16fb88 100
maxint 2:74bc9b16fb88 101 // 5. Add the first projection prA to the reverse of the second -prB
maxint 2:74bc9b16fb88 102 float new_vx = prA_vx - prB_vx;
maxint 2:74bc9b16fb88 103 float new_vy = prA_vy - prB_vy;
maxint 2:74bc9b16fb88 104 set(new_vx, new_vy);
maxint 0:3d0db4e183ee 105 }