Skip to content

Commit acc9203

Browse files
committed
sdl gravity
1 parent 7fd12b8 commit acc9203

2 files changed

Lines changed: 28 additions & 18 deletions

File tree

‎opencl/benchmarks.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Benchmarks
22

3-
- <https://compubench.com>. Also from Kishonti who makes GFXBench. TODO business model.
3+
- <https://compubench.com> Also from Kishonti who makes GFXBench.
4+
- <http://geekbench.com/> Proprietary by <http://www.primatelabs.com/>, looks like similar business model to Kishonti (license source).

‎sdl/animation_user_control_inertia.c‎

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
/*
2-
Some basic inertia / friction on a single body.
1+
/*Some basic inertia / friction on a single body.
32
43
Gives a strong sense of realism.
54
@@ -12,10 +11,10 @@ of Box2D... right?
1211

1312
#define WINDOW_WIDTH 800
1413
#define WINDOW_HEIGHT WINDOW_WIDTH
15-
#define WINDOW_CENTER_X = (WINDOW_WIDTH / 2)
16-
#define WINDOW_CENTER_Y = (WINDOW_HEIGHT / 2)
14+
#define WINDOW_CENTER_X (WINDOW_WIDTH / 2)
15+
#define WINDOW_CENTER_Y (WINDOW_HEIGHT / 2)
1716
#define WINDOW_HEIGHT WINDOW_WIDTH
18-
#define RECT_WIDTH (WINDOW_WIDTH / 30)
17+
#define RECT_WIDTH (WINDOW_WIDTH / 50)
1918
#define RECT_HEIGHT RECT_WIDTH
2019
#define MAX_RECT_POS (WINDOW_WIDTH - RECT_WIDTH)
2120
/*
@@ -90,7 +89,7 @@ double apply_constant_friction(double speed, double dt) {
9089
int main(void) {
9190
PlayerState player_state;
9291
SDL_Event event;
93-
SDL_Rect rect;
92+
SDL_Rect rect, rect_sun;
9493
SDL_Renderer *renderer;
9594
SDL_Window *window;
9695
int quit = 0;
@@ -102,6 +101,10 @@ int main(void) {
102101
SDL_SetWindowTitle(window, "arrow keys: accelerate | esc: reset | q: quit");
103102
rect.w = RECT_WIDTH;
104103
rect.h = RECT_HEIGHT;
104+
rect_sun.w = RECT_WIDTH;
105+
rect_sun.h = RECT_HEIGHT;
106+
rect_sun.x = WINDOW_CENTER_X;
107+
rect_sun.y = WINDOW_CENTER_Y;
105108
main_loop:
106109
init_state(&player_state, &user_acc_x, &user_acc_y, &last_time);
107110
while (!quit) {
@@ -126,8 +129,6 @@ int main(void) {
126129
goto main_loop;
127130
case SDLK_q:
128131
goto quit;
129-
default:
130-
break;
131132
}
132133
} else if (event.type == SDL_KEYUP) {
133134
switch (event.key.keysym.sym) {
@@ -139,8 +140,6 @@ int main(void) {
139140
case SDLK_DOWN:
140141
user_acc_y = 0;
141142
break;
142-
default:
143-
break;
144143
}
145144
}
146145
}
@@ -157,12 +156,20 @@ int main(void) {
157156
/* Constant gravity down. */
158157
/*acc_y += ACC_GRAVITY_METERS_PER_SECOND;*/
159158

160-
/* Constant gravity to center of screen. */
161-
double dx = player_state.x - WINDOW_CENTER_X;
162-
double dy = player_state.y - WINDOW_CENTER_Y;
159+
/* Constant gravity to center of screen, the "sun". */
160+
{
161+
double dx = player_state.x - WINDOW_CENTER_X;
162+
double dy = player_state.y - WINDOW_CENTER_Y;
163+
double angle = atan2(dy, dx);
164+
double hyp = hypot(dx, dy);
163165

164-
acc_x += ACC_GRAVITY_METERS_PER_SECOND;
165-
acc_y += ACC_GRAVITY_METERS_PER_SECOND;
166+
/* TODO Failed attempt at force proportional to inverse square of distance. */
167+
/*double mod = hyp > 0.001 ? hyp : 1.0;*/
168+
/*double mod2 = mod * mod;*/
169+
170+
acc_x -= cos(angle) * ACC_GRAVITY_METERS_PER_SECOND;
171+
acc_y -= sin(angle) * ACC_GRAVITY_METERS_PER_SECOND;
172+
}
166173
}
167174

168175
/* Update speed. */
@@ -177,8 +184,8 @@ int main(void) {
177184
but we must consider the case where the object stops completely
178185
because of it separately.
179186
*/
180-
player_state.speed_x = apply_constant_friction(player_state.speed_x, dt);
181-
player_state.speed_y = apply_constant_friction(player_state.speed_y, dt);
187+
/*player_state.speed_x = apply_constant_friction(player_state.speed_x, dt);*/
188+
/*player_state.speed_y = apply_constant_friction(player_state.speed_y, dt);*/
182189

183190
/* Friction proportional to speed:
184191
- decelerates very quickly
@@ -199,6 +206,8 @@ int main(void) {
199206
rect.x = player_state.x;
200207
rect.y = player_state.y;
201208
SDL_RenderFillRect(renderer, &rect);
209+
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
210+
SDL_RenderFillRect(renderer, &rect_sun);
202211
SDL_RenderPresent(renderer);
203212

204213
/* Update time. */

0 commit comments

Comments
 (0)