Advertisement
paulolol

Untitled

Nov 12th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <math.h>
  2.  
  3. /*
  4. Dans cet algo :
  5. (a,b) représente la position du joueur et (w1,h1) sa largeur et hauteur
  6. (c,d) représente la position du bloc et (w2,h2)   "       "       "
  7. Il ne marche que si l'orgine des coord. d'une sprite est en haut à gauche
  8. Si l'origine est au centre de la sprite, alors x1 = a, y1 = b, x2 = c, y2 = d
  9. */
  10.  
  11. if (/*conditions de collisions habituelles*/){
  12.   double x1 = a + 0.5*w1;
  13.   double y1 = b + 0.5*h1;
  14.   double x2 = c + 0.5*w2;
  15.   double y2 = d + 0.5*h2;
  16.  
  17.   double vx = x1-x2;
  18.   double vy = y1-y2;
  19.   double norm = sqrt( pow(vx,2) + pow(vy,2) );
  20.   vx /= norm;
  21.   vy /= norm;
  22.  
  23.   double angle = acos(vx);
  24.   double sign = vy/abs(vy);
  25.   angle *= sign;
  26.   double pis4 = atan(1);
  27.   if((angle <= pis4 && angle >= -pis4) || norm == 0){
  28.     a = c + w2;
  29.   } else if(angle <= 3*pis4 && angle >= pis4){
  30.     b = d + h2;
  31.   } else if(angle <= -pis4 && angle >= -3*pis4){
  32.     b = d - h1;
  33.   } else if(angle <= -3*pis4 || angle >= 3*pis4){
  34.     a = c - w1;
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement