Archeus 0.0.0
A C library and game engine that focuses on documentation
Loading...
Searching...
No Matches
rectangle.c
Go to the documentation of this file.
2
3//VERY TEMP
4// #include <SDL.h>
5
6void ARC_Rect_CenterOn(ARC_Rect *rect, ARC_Rect *bounds){
7 rect->x = (bounds->x + (bounds->w / 2)) - (rect->w / 2);
8 rect->y = (bounds->y + (bounds->h / 2)) - (rect->h / 2);
9}
10
12 rect->x = (bounds->x + (bounds->w / 2.0f)) - (rect->w / 2.0f);
13 rect->y = (bounds->y + (bounds->h / 2.0f)) - (rect->h / 2.0f);
14}
15
17 return (ARC_FRect){
18 .x = (float)rect->x,
19 .y = (float)rect->y,
20 .w = (float)rect->w,
21 .h = (float)rect->h,
22 };
23}
24
26 return (ARC_Rect){
27 .x = (int32_t)rect->x,
28 .y = (int32_t)rect->y,
29 .w = (int32_t)rect->w,
30 .h = (int32_t)rect->h,
31 };
32}
33
34int32_t ARC_Rect_Intersects(ARC_Rect *rect1, ARC_Rect *rect2){
35 if(rect1->x <= rect2->x + rect2->w && rect1->x + rect1->w >= rect2->x &&
36 rect1->y <= rect2->y + rect2->h && rect1->y + rect1->h >= rect2->y){
37 return 1;
38 }
39 return 0;
40}
41
43 if(rect1->x <= rect2->x + rect2->w && rect1->x + rect1->w >= rect2->x &&
44 rect1->y <= rect2->y + rect2->h && rect1->y + rect1->h >= rect2->y){
45 return 1;
46 }
47 return 0;
48}
49
51 if(rect->x <= point->x && rect->x + rect->w >= point->x &&
52 rect->y <= point->y && rect->y + rect->h >= point->y){
53 return 1;
54 }
55 return 0;
56}
57
59 if(rect->x <= point->x && rect->x + rect->w >= point->x &&
60 rect->y <= point->y && rect->y + rect->h >= point->y){
61 return 1;
62 }
63 return 0;
64}
65
66int32_t ARC_Rect_LineIntersects(ARC_Rect *rect, int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2){
67 //TODO: Replace soon
68 // return SDL_IntersectRectAndLine((SDL_Rect *) rect, x1, y1, x2, y2);
69 return 1;
70}
71
72//TODO: position 1px away from colliding, so if velocity is greater than 2px it doesn't have weird gap spacing
73// might need to check diagnals as well, this is a rudamentry implementation
75 ARC_FRect nextRectPosition = {
76 .x = rect->x + velocity->x,
77 .y = rect->y + velocity->y,
78 .w = rect->w,
79 .h = rect->h
80 };
81
82 //there is no collision, return
83 if(!ARC_FRect_Intersects(&nextRectPosition, wall)){
84 return;
85 }
86
87 nextRectPosition.x = rect->x + velocity->x;
88 nextRectPosition.y = rect->y;
89 if(ARC_FRect_Intersects(&nextRectPosition, wall)){
90 velocity->x = 0;
91 }
92
93 nextRectPosition.x = rect->x;
94 nextRectPosition.y = rect->y + velocity->y;
95 if(ARC_FRect_Intersects(&nextRectPosition, wall)){
96 velocity->y = 0;
97 }
98}
int32_t ARC_Rect_Intersects(ARC_Rect *rect1, ARC_Rect *rect2)
checks if two ARC_Rects intersect
Definition rectangle.c:34
ARC_Rect ARC_FRect_CastToRect(ARC_FRect *rect)
casts FRect to Rect
Definition rectangle.c:25
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_FRect ARC_Rect_CastToFRect(ARC_Rect *rect)
casts Rect to FRect
Definition rectangle.c:16
void ARC_Rect_CenterOn(ARC_Rect *rect, ARC_Rect *bounds)
centers rect on given bounds
Definition rectangle.c:6
int32_t ARC_FRect_IntersectsPoint(ARC_FRect *rect, ARC_Point *point)
checks if ARC_FRect intersects with point
Definition rectangle.c:58
int32_t ARC_FRect_Intersects(ARC_FRect *rect1, ARC_FRect *rect2)
checks if two ARC_FRects intersect
Definition rectangle.c:42
int32_t ARC_Rect_IntersectsPoint(ARC_Rect *rect, ARC_Point *point)
checks if ARC_Rect intersects with point
Definition rectangle.c:50
void ARC_FRect_CenterOn(ARC_FRect *rect, ARC_FRect *bounds)
centers rect on given bounds
Definition rectangle.c:11
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
float y
Definition rectangle.h:28
float x
Definition rectangle.h:27
float w
Definition rectangle.h:29
float h
Definition rectangle.h:30
int32_t y
Definition point.h:12
int32_t x
Definition point.h:11
int32_t x
Definition rectangle.h:13
int32_t w
Definition rectangle.h:15
int32_t y
Definition rectangle.h:14
int32_t h
Definition rectangle.h:16
float x
Definition vector2.h:9
float y
Definition vector2.h:9