collide and slide function for FRect
This commit is contained in:
parent
5aede928d8
commit
56a4aa9d39
2 changed files with 43 additions and 0 deletions
|
|
@ -69,3 +69,30 @@ int32_t ARC_Rect_LineIntersects(ARC_Rect *rect, int32_t *x1, int32_t *y1, int32_
|
|||
return 1;
|
||||
}
|
||||
|
||||
//TODO: position 1px away from colliding, so if velocity is greater than 2px it doesn't have weird gap spacing
|
||||
// might need to check diagnals as well, this is a rudamentry implementation
|
||||
void ARC_FRect_CollideAndSlide(ARC_FRect *rect, ARC_Vector2 *velocity, ARC_FRect *wall){
|
||||
ARC_FRect nextRectPosition = {
|
||||
.x = rect->x + velocity->x,
|
||||
.y = rect->y + velocity->y,
|
||||
.w = rect->w,
|
||||
.h = rect->h
|
||||
};
|
||||
|
||||
//there is no collision, return
|
||||
if(!ARC_FRect_Intersects(&nextRectPosition, wall)){
|
||||
return;
|
||||
}
|
||||
|
||||
nextRectPosition.x = rect->x + velocity->x;
|
||||
nextRectPosition.y = rect->y;
|
||||
if(ARC_FRect_Intersects(&nextRectPosition, wall)){
|
||||
velocity->x = 0;
|
||||
}
|
||||
|
||||
nextRectPosition.x = rect->x;
|
||||
nextRectPosition.y = rect->y + velocity->y;
|
||||
if(ARC_FRect_Intersects(&nextRectPosition, wall)){
|
||||
velocity->y = 0;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue