Loading...
Searching...
No Matches
rectangle.c
Go to the documentation of this file.
#include "arc/math/rectangle.h"
//VERY TEMP
// #include <SDL.h>
void ARC_Rect_CenterOn(ARC_Rect *rect, ARC_Rect *bounds){
rect->x = (bounds->x + (bounds->w / 2)) - (rect->w / 2);
rect->y = (bounds->y + (bounds->h / 2)) - (rect->h / 2);
}
void ARC_FRect_CenterOn(ARC_FRect *rect, ARC_FRect *bounds){
rect->x = (bounds->x + (bounds->w / 2.0f)) - (rect->w / 2.0f);
rect->y = (bounds->y + (bounds->h / 2.0f)) - (rect->h / 2.0f);
}
ARC_FRect ARC_Rect_CastToFRect(ARC_Rect *rect){
return (ARC_FRect){
.x = (float)rect->x,
.y = (float)rect->y,
.w = (float)rect->w,
.h = (float)rect->h,
};
}
ARC_Rect ARC_FRect_CastToRect(ARC_FRect *rect){
return (ARC_Rect){
.x = (int32_t)rect->x,
.y = (int32_t)rect->y,
.w = (int32_t)rect->w,
.h = (int32_t)rect->h,
};
}
int32_t ARC_Rect_Intersects(ARC_Rect *rect1, ARC_Rect *rect2){
if(rect1->x <= rect2->x + rect2->w && rect1->x + rect1->w >= rect2->x &&
rect1->y <= rect2->y + rect2->h && rect1->y + rect1->h >= rect2->y){
return 1;
}
return 0;
}
int32_t ARC_FRect_Intersects(ARC_FRect *rect1, ARC_FRect *rect2){
if(rect1->x <= rect2->x + rect2->w && rect1->x + rect1->w >= rect2->x &&
rect1->y <= rect2->y + rect2->h && rect1->y + rect1->h >= rect2->y){
return 1;
}
return 0;
}
int32_t ARC_Rect_IntersectsPoint(ARC_Rect *rect, ARC_Point *point){
if(rect->x <= point->x && rect->x + rect->w >= point->x &&
rect->y <= point->y && rect->y + rect->h >= point->y){
return 1;
}
return 0;
}
int32_t ARC_FRect_IntersectsPoint(ARC_FRect *rect, ARC_Point *point){
if(rect->x <= point->x && rect->x + rect->w >= point->x &&
rect->y <= point->y && rect->y + rect->h >= point->y){
return 1;
}
return 0;
}
int32_t ARC_Rect_LineIntersects(ARC_Rect *rect, int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2){
//TODO: Replace soon
// return SDL_IntersectRectAndLine((SDL_Rect *) rect, x1, y1, x2, y2);
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;
}
}
ARC_Rect_Intersects
int32_t ARC_Rect_Intersects(ARC_Rect *rect1, ARC_Rect *rect2)
checks if two ARC_Rects intersect
Definition rectangle.c:34
ARC_FRect_CastToRect
ARC_Rect ARC_FRect_CastToRect(ARC_FRect *rect)
casts FRect to Rect
Definition rectangle.c:25
ARC_FRect_CollideAndSlide
void ARC_FRect_CollideAndSlide(ARC_FRect *rect, ARC_Vector2 *velocity, ARC_FRect *wall)
checks for a ARC_Rect on ARC_Rect collision and slides on collision
Definition rectangle.c:74
ARC_Rect_CastToFRect
ARC_FRect ARC_Rect_CastToFRect(ARC_Rect *rect)
casts Rect to FRect
Definition rectangle.c:16
ARC_Rect_CenterOn
void ARC_Rect_CenterOn(ARC_Rect *rect, ARC_Rect *bounds)
centers rect on given bounds
Definition rectangle.c:6
ARC_FRect_IntersectsPoint
int32_t ARC_FRect_IntersectsPoint(ARC_FRect *rect, ARC_Point *point)
checks if ARC_FRect intersects with point
Definition rectangle.c:58
ARC_FRect_Intersects
int32_t ARC_FRect_Intersects(ARC_FRect *rect1, ARC_FRect *rect2)
checks if two ARC_FRects intersect
Definition rectangle.c:42
ARC_Rect_IntersectsPoint
int32_t ARC_Rect_IntersectsPoint(ARC_Rect *rect, ARC_Point *point)
checks if ARC_Rect intersects with point
Definition rectangle.c:50
ARC_FRect_CenterOn
void ARC_FRect_CenterOn(ARC_FRect *rect, ARC_FRect *bounds)
centers rect on given bounds
Definition rectangle.c:11
ARC_Rect_LineIntersects
int32_t ARC_Rect_LineIntersects(ARC_Rect *rect, int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2)
checks if ARC_Rect intersects a line
Definition rectangle.c:66
rectangle.h
ARC_FRect
Definition rectangle.h:26
ARC_FRect::y
float y
Definition rectangle.h:28
ARC_FRect::x
float x
Definition rectangle.h:27
ARC_FRect::w
float w
Definition rectangle.h:29
ARC_FRect::h
float h
Definition rectangle.h:30
ARC_Point
Definition point.h:10
ARC_Point::y
int32_t y
Definition point.h:12
ARC_Point::x
int32_t x
Definition point.h:11
ARC_Rect
Definition rectangle.h:12
ARC_Rect::x
int32_t x
Definition rectangle.h:13
ARC_Rect::w
int32_t w
Definition rectangle.h:15
ARC_Rect::y
int32_t y
Definition rectangle.h:14
ARC_Rect::h
int32_t h
Definition rectangle.h:16
ARC_Vector2
Definition vector2.h:8
ARC_Vector2::x
float x
Definition vector2.h:9
ARC_Vector2::y
float y
Definition vector2.h:9