This commit is contained in:
Sylvain PILLOT 2023-05-20 17:03:50 +02:00
parent 580c0e2d61
commit 7662a06e2c
4 changed files with 92 additions and 24 deletions

View File

@ -36,6 +36,7 @@ extern bool textforetile;
extern uint16_t tilecolor;
extern std::vector<Border*> MyLevelBorders;
extern Player MyPlayer;
struct Map *map_level;
@ -125,7 +126,7 @@ void Level::Render( void )
for( unsigned int i=0; i<MyLevelBorders.size(); i++)
azrp_line( (int) MyLevelBorders[i]->A.x, (int) MyLevelBorders[i]->A.y,
(int) MyLevelBorders[i]->B.x, (int) MyLevelBorders[i]->B.y,
C_GREEN );
MyLevelBorders[i]->color );
if (drawnormals)
for( unsigned int i=0; i<MyLevelBorders.size(); i++)
@ -189,17 +190,86 @@ int Level::GetTileForeground( uint16_t x, uint16_t y )
return currentTile;
}
bool CollisionDroiteSeg(Point2D A,Point2D B,Point2D O,Point2D P)
{
Vector2D AO,AP,AB;
AB.x = B.x - A.x;
AB.y = B.y - A.y;
AP.x = P.x - A.x;
AP.y = P.y - A.y;
AO.x = O.x - A.x;
AO.y = O.y - A.y;
libnum::num part1 = AB.x*AP.y - AB.y*AP.x;
libnum::num part2 = AB.x*AO.y - AB.y*AO.x;
if (part1*part2<0)
return true;
else
return false;
}
bool CollisionSegSeg( Point2D A, Point2D B, Point2D O, Point2D P)
{
if (CollisionDroiteSeg(A,B,O,P)==false)
return false; // inutile d'aller plus loin si le segment [OP] ne touche pas la droite (AB)
Vector2D AB,OP;
AB.x = B.x - A.x;
AB.y = B.y - A.y;
OP.x = P.x - O.x;
OP.y = P.y - O.y;
libnum::num part1 = (A.x*OP.y-O.x*OP.y-OP.x*A.y+OP.x*O.y);
libnum::num part2 = (AB.x*OP.y-AB.y*OP.x);
libnum::num k = - part1 / part2;
if (k<0 || k>1)
return false;
else
return true;
}
/*RETURN true if the player can go in the target position*/
bool Level::CanGo( Player *MyPlayer )
{
// uint16_t targetTile = this->GetTileBackgroundINT( (int) MyPlayer->nextx, (int) MyPlayer->nexty );
Point2D PlayerOrigin, PlayerTarget;
// if (targetTile!=0) return false;
PlayerOrigin.x = MyPlayer->currx;
PlayerOrigin.y = MyPlayer->curry;
if (MyPlayer->nextx >=0 && MyPlayer->nextx<azrp_width && MyPlayer->nexty >=0 && MyPlayer->nexty<azrp_height)
return true;
else
return false;
PlayerTarget.x = MyPlayer->nextx;
PlayerTarget.y = MyPlayer->nexty;
if (PlayerTarget.x == PlayerOrigin.x && PlayerTarget.y == PlayerOrigin.y) return true;
if (MyLevelBorders.size() == 0) return true;
for (unsigned int i=0; i<=MyLevelBorders.size(); i++)
{
Point2D A, B;
A.x = MyLevelBorders[i]->A.x;
A.y = MyLevelBorders[i]->A.y;
B.x = MyLevelBorders[i]->B.x;
B.y = MyLevelBorders[i]->B.y;
if (CollisionSegSeg( A, B, PlayerOrigin, PlayerTarget ))
{
MyLevelBorders[i]->color = C_RED;
return false;
}
else
{
MyLevelBorders[i]->color = C_RGB( 255, 0, 255 );
}
}
return true;
}
/*RETURN true if the player is above a solid tile*/
@ -227,6 +297,8 @@ Border* Level::MakeBorder( libnum::num DX, libnum::num DY, float x1, float y1, f
Bord->N.x = Bord->A.y - Bord->B.y;
Bord->N.y = Bord->B.x - Bord->A.x;
Bord->color = C_GREEN;
return Bord;
}

View File

@ -65,8 +65,8 @@ std::vector<Border*> MyLevelBorders;
bool drawbackground = true;
bool drawforeground = true;
bool drawnormals = false;
bool drawborders = false;
bool drawnormals = true;
bool drawborders = true;
uint16_t tilecolor = C_BLACK;
uint16_t backcolor = C_WHITE;

View File

@ -118,7 +118,7 @@ bool CollisionDroite(Point2D A,Point2D B, Circle C)
libnum::num denominateur = sqrt_num32(u.x*u.x + u.y*u.y); // norme de u
libnum::num CI = numerateur / denominateur;
if (CI<C.r)
if (CI < C.r)
return true;
else
return false;
@ -141,24 +141,16 @@ bool CollisionSegment(Point2D A,Point2D B, Circle C)
libnum::num pscal1 = AB.x*AC.x + AB.y*AC.y; // produit scalaire
libnum::num pscal2 = (-AB.x)*BC.x + (-AB.y)*BC.y; // produit scalaire
if (pscal1>=0 && pscal2>=0)
if (pscal1 >= 0 && pscal2 >= 0)
return true; // I entre A et B, ok.
/*
// dernière possibilité, A ou B dans le cercle
if (CollisionPoint2DCercle(A,C))
return true;
if (CollisionPoint2DCercle(B,C))
return true;
*/
return false;
}
bool CollisionBorder(Border B, Circle C)
bool CollisionBorder(Border *B, Circle C)
{
return CollisionSegment( B.A, B.B, C );
return CollisionSegment( B->A, B->B, C );
}

View File

@ -13,7 +13,8 @@ class Point2D
Point2D() {};
~Point2D() {};
libnum::num x, y;
libnum::num x;
libnum::num y;
};
class Circle
@ -22,7 +23,9 @@ class Circle
Circle() {};
~Circle() {};
libnum::num x,y,r;
libnum::num x;
libnum::num y;
libnum::num r;
};
@ -116,12 +119,13 @@ class Border
Point2D A;
Point2D B;
Vector2D N;
uint16_t color;
};
bool CollisionDroite(Point2D A,Point2D B, Circle C);
bool CollisionSegment(Point2D A,Point2D B,Circle C);
bool CollisionBorder(Border B, Circle C);
bool CollisionBorder(Border* B, Circle C);
Vector2D GetNormale(Point2D A,Point2D B,Point2D C);
Point2D ProjectionI(Point2D A,Point2D B,Point2D C);
Vector2D ComputeVectorRebound(Vector2D v,Vector2D N);