-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysics.cpp
More file actions
137 lines (116 loc) · 2.34 KB
/
physics.cpp
File metadata and controls
137 lines (116 loc) · 2.34 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "physics.h"
extern bool tiles[1000][1000];
void ApplyPhysics(DynamicEntity &p, Uint32 deltaTicks)
{
int tempx, tempy, tempx2, tempy2;
float tx, ty;
if(!p.onground)
{
#ifdef AIR_RESISTANCE_ENABLED
if(p.xVel > 0)
{
p.xVel -= AIR_RESISTANCE * ( deltaTicks / PHYSICS_SPEED );
if(p.xVel < 0) p.xVel = 0;
}
if(p.xVel < 0)
{
p.xVel += AIR_RESISTANCE * ( deltaTicks / PHYSICS_SPEED );
if(p.xVel > 0) p.xVel = 0;
}
#endif
}
else
{
if(p.xVel > 0)
{
p.xVel -= FRICTION * ( deltaTicks / PHYSICS_SPEED );
if(p.xVel < 0) p.xVel = 0;
}
if(p.xVel < 0)
{
p.xVel += FRICTION * ( deltaTicks / PHYSICS_SPEED );
if(p.xVel > 0) p.xVel = 0;
}
}
//Move the player up or down
ty = p.y;
if(p.jumping) p.yVel -= p.accel * deltaTicks;
// Add some gravity
p.yVel += GRAVITY * ( deltaTicks / PHYSICS_SPEED );
if(p.yVel > GRAVITY)
{
p.yVel = GRAVITY;
}
p.y += p.yVel * ( deltaTicks / PHYSICS_SPEED );
//If the player went too far up
if(p.y < 0 )
{
//Move back
p.y = 0;
}
//or down
else if( p.y + p.height > MAP_HEIGHT )
{
//Move back
p.y = MAP_HEIGHT - p.height;
}
tempx = ceilf(p.x / TILESIZE)-0.5;
tempy = ceilf(p.y / TILESIZE)-0.5;
tempx2 = ceilf((p.x+p.width) / TILESIZE)-0.5;
tempy2 = ceilf((p.y+p.height) / TILESIZE)-0.5;
for(int i = tempx; i <= tempx2; i++)
{
if(tiles[i][tempy] == true)
{
p.y = ty;
p.yVel = 0;
}
}
p.onground = false;
for(int i = tempx; i <= tempx2; i++)
{
if(tiles[i][tempy2] == true)
{
if(p.yVel > 0)
{
p.y = ty;
//y = tempy * 32;
//y = tempy2 * 32 - p.player_height;
p.yVel = 0;
p.onground = true;
}
}
}
//Move the player left or right
tx = p.x;
p.x += p.xVel * ( deltaTicks / PHYSICS_SPEED );
//If the player went too far to the left
if( p.x < 0 )
{
//Move back
p.x = 0;
}
//or the right
else if( p.x + p.width > MAP_WIDTH )
{
//Move back
p.x = MAP_WIDTH - p.width;
}
tempx = ceilf(p.x / TILESIZE)-0.5;
tempy = ceilf(p.y / TILESIZE)-0.5;
tempx2 = ceilf((p.x+p.width) / TILESIZE)-0.5;
tempy2 = ceilf((p.y+p.height) / TILESIZE)-0.5;
for(int j = tempy; j <= tempy2; j++)
{
if(tiles[tempx][j] == true)
{
p.x = tx;
break;
}
if(tiles[tempx2][j] == true)
{
p.x = tx;
break;
}
}
}