1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
void CBall::Move()
{
float V = pAll->AbsVF(this->V_ini->v_X, this->V_ini->v_Y);
// On definit les vecteurs d'acceleration //
this->Obstacles();
float ADDX = this->V_ini->v_X * dt + 0.5 * this->V_acc->v_X * dt * dt;
float ADDY = this->V_ini->v_Y * dt + 0.5 * this->V_acc->v_Y * dt * dt;
// V = V0 + a * t //
this->V_ini->v_X += dt * this->V_acc->v_X;
this->V_ini->v_Y += dt * this->V_acc->v_Y;
this->m_X = this->m_X0 + ADDX;
this->m_Y = this->m_Y0 + ADDY;
this->m_X0 = this->m_X;
this->m_Y0 = this->m_Y;
}
bool CBall::Obstacles()
{
int nOb = pAll->pPF->m_Ob.size();
for (int i = 0; i < nOb; i++)
{
CObstacle *OB = &pAll->pPF->m_Ob[i];
CLine *L = pAll->pPF->m_Ob[i].ToucheLigne(this);
// Si la balle touche la ligne vérifiée //
if (L != NULL)
{
this->V_acc->v_X = G * sin(L->m_A);
this->V_acc->v_Y = G * cos(L->m_A);
double ANGLE = M_PI_2;
if (this->V_ini->v_X != 0)
ANGLE = atan(this->V_ini->v_Y / this->V_ini->v_X);
float V = pAll->AbsVF(this->V_ini->v_X, this->V_ini->v_Y);
float DegA = ANGLE;
float DegAl = L->m_A;
float Diff = sqrt(pow(DegA - DegAl, 2));
// CADRAN 1 X(+) Y(+) //
if (this->V_ini->v_X >= 0 && this->V_ini->v_Y >= 0)
ANGLE = -ANGLE + 2 * L->m_A;
// CADRAN 2 X(-) Y(+) //
if (this->V_ini->v_X < 0 && this->V_ini->v_Y >= 0)
ANGLE = -ANGLE + 2 * L->m_A - M_PI;
// CADRAN 3 X(-) Y(-) //
if (this->V_ini->v_X < 0 && this->V_ini->v_Y < 0)
ANGLE = -ANGLE + 2 * L->m_A - M_PI;
// CADRAN 4 X(+) Y(-) //
if (this->V_ini->v_X >= 0 && this->V_ini->v_Y < 0)
ANGLE = -ANGLE + 2 * L->m_A;
this->V_ini->v_X = cos(ANGLE) * V;
this->V_ini->v_Y = sin(ANGLE) * V;
if (Diff < 0.25) // Moins de 15° la bille se mettra à rouler //
{
this->V_ini->v_Y = 0;
}
return true;
}
else L = NULL;
}
this->V_acc->v_X = 0;
this->V_acc->v_Y = G;
return false;
}
CLine *CObstacle::ToucheLigne(CBall *pBall)
{
// Si ce n'est pas un cercle
if (!this->m_Circle)
{
int Size = this->m_Lim.size();
// On cherche la colision dans l'obstacle
for (int i = 0; i < Size; i++)
{
// Checking colision with segments //
if (pAll->ColisionDroite(pBall, &this->m_Lim[i], Rayon))
return &this->m_Lim[i];
}
}
return NULL;
}
bool SMath::ColisionDroite(CBall *pB, CLine *pL, int R)
{
// On réapplique les coordonnées réels pour les coordonnées de la balle //
float XN = pB->m_X * 700;
float YN = pB->m_Y * 700;
// On applique la rotation //
float A = pL->m_A * -1;
float X = (XN+R - pL->m_X1) * cos(A) - (YN+R - pL->m_Y1) * sin(A);
float Y = (XN+R - pL->m_X1) * sin(A) + (YN+R - pL->m_Y1) * cos(A);
float X1 = 0;
float Y1 = 0;
float X2 = (pL->m_X2 - pL->m_X1) * cos(A) - (pL->m_Y2 - pL->m_Y1) * sin(A);
float Y2 = (pL->m_X2 - pL->m_X1) * sin(A) + (pL->m_Y2 - pL->m_Y1) * cos(A);
// On obtient une droite horizontale //
// On fait mtn le test //
return this->ColisionPoint(X, Y, X1-R, Y1-R, X2+R, Y2+R);
}
bool SMath::ColisionPoint(float x, float y, float x1, float y1, float x2, float y2)
{
if (x > x2) return false;
if (x < x1) return false;
if (y < y1) return false;
if (y > y2) return false;
return true;
} |
Partager