-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_physics.c
122 lines (108 loc) · 3.3 KB
/
game_physics.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "game_physics.h"
#include "global.h"
int collide2d(float x1, float y1, float x2, float y2, float wt1, float ht1, float wt2, float ht2)
{
return (!((x1 > (x2+wt2)) || (x2 > (x1+wt1)) || (y1 > (y2+ht2)) || (y2 > (y1+ht1))));
}
void CheckMonster(GameState *game)
{
for (int i = 0; i < MAX_MONSTERS; i++)
{
//
}
}
int IsColliding(int x, int y, int velX, int velY, int w, int h, GameState *game)
{
float mw = w, mh = h;
float mx = x, my = y;
//Check for collision with any blocks
for(int i = 0; i < MAX_BLOCKS; i++)
{
float bx = game->blocks[i].x, by = game->blocks[i].y, bw = game->blocks[i].w, bh = game->blocks[i].h;
if(mx+mw/2 > bx && mx+mw/2<bx+bw)
{
//are we bumping our head?
if(my < by+bh && my > by && velY < 0)
{
return 1;
}
}
if(mx+mw > bx && mx<bx+bw)
{
//are we landing on the block
if(my+mh > by && my < by && velY > 0)
{
return 1;
}
}
if(my+mh > by && my<by+bh)
{
//rubbing against right edge
if(mx < bx+bw && mx+mw > bx+bw && velX < 0)
{
return 1;
}
//rubbing against left edge
else if(mx+mw > bx && mx < bx && velX > 0)
{
return 1;
}
}
}
return 0;
}
void collisionDetect(GameState *game)
{
float mw = PLAYER_HITBOX_X, mh = PLAYER_HITBOX_Y;
float mx = game->player.x, my = game->player.y;
//Check for collision with any blocks
for(int i = 0; i < MAX_BLOCKS; i++)
{
float bx = game->blocks[i].x, by = game->blocks[i].y, bw = game->blocks[i].w, bh = game->blocks[i].h;
if(mx+mw/2 > bx && mx+mw/2<bx+bw)
{
//are we bumping our head?
if(my < by+bh && my > by && game->player.velY < 0)
{
//correct y
game->player.y = by+bh;
my = by+bh;
//bumped our head, stop any jump velocity
game->player.velY = 0;
//game->player.JumpState = 1;
}
}
if(mx+mw > bx && mx<bx+bw)
{
//are we landing on the block
if(my+mh > by && my < by && game->player.velY > 0)
{
//correct y
game->player.y = by-mh;
my = by-mh;
//landed on this block, stop any jump velocity
game->player.velY = 0;
game->player.JumpState = 1;
}
}
if(my+mh > by && my<by+bh)
{
//rubbing against right edge
if(mx < bx+bw && mx+mw > bx+bw && game->player.velX < 0)
{
//correct x
game->player.x = bx+bw;
mx = bx+bw;
game->player.velX = 0;
}
//rubbing against left edge
else if(mx+mw > bx && mx < bx && game->player.velX > 0)
{
//correct x
game->player.x = bx-mw;
mx = bx-mw;
game->player.velX = 0;
}
}
}
}