1
1
Fork 0
Browse Source

viewport

thread-physics
user 5 years ago
parent
commit
0fc13a4734
  1. 74
      draw.c
  2. 2
      draw.h
  3. 84
      game.c
  4. 22
      game.h
  5. 3
      main.c

74
draw.c

@ -32,15 +32,27 @@ SDL_Texture * load_image(SDL_Renderer * ren, char fname[]) { @@ -32,15 +32,27 @@ SDL_Texture * load_image(SDL_Renderer * ren, char fname[]) {
return (png_image);
}
Vect in_view(Vect V) {
Vect ret;
ret.x = V.x -viewport_pos.x;
ret.y = V.y -viewport_pos.y;
return ret;
}
/* Game Specific rendering functions */
void draw_player(SDL_Renderer * ren, int x, int y, bool red) {
logwrite(DEBUG, "Drawing player\n");
// translate to viewport
Vect V;
V.x = x; V.y = y;
V = in_view(V);
/* draw the player as a coloured rect */
SDL_Rect player_rect;
player_rect.x = x;
player_rect.y = y;
player_rect.x = V.x - 5;
player_rect.y = V.y - 5;
player_rect.w = 10;
player_rect.h = 10;
@ -68,32 +80,36 @@ void draw_collision_poly(SDL_Renderer* ren, Body *body) { @@ -68,32 +80,36 @@ void draw_collision_poly(SDL_Renderer* ren, Body *body) {
}
if (body->collision_poly_size == 1) {
SDL_Rect dot;
dot.x = body->collision_poly[0].x;
dot.y = body->collision_poly[0].y;
Vect V;
V.x = body->collision_poly[0].x;
V.y = body->collision_poly[0].y;
Vect T = in_view(V);
dot.x = T.x;
dot.y = T.y;
dot.w = 4;
dot.h = 4;
SDL_RenderDrawRect(ren, &dot);
SDL_RenderFillRect(ren, &dot);
} else if (body->collision_poly_size == 2) {
int x_st = body->collision_poly[0].x;
int y_st = body->collision_poly[0].y;
int x_en = body->collision_poly[1].x;
int y_en = body->collision_poly[1].y;
int x_st = body->collision_poly[0].x -viewport_pos.x;
int y_st = body->collision_poly[0].y -viewport_pos.y;
int x_en = body->collision_poly[1].x -viewport_pos.x;
int y_en = body->collision_poly[1].y -viewport_pos.y;
SDL_RenderDrawLine(ren, x_st, y_st, x_en, y_en);
} else if (body->collision_poly_size > 2) {
int x_st = body->collision_poly[0].x;
int y_st = body->collision_poly[0].y;
int x_st = body->collision_poly[0].x -viewport_pos.x;
int y_st = body->collision_poly[0].y -viewport_pos.y;
int x_en, y_en;
for (int i = 1; i < body->collision_poly_size; i++) {
x_en = body->collision_poly[i].x;
y_en = body->collision_poly[i].y;
x_en = body->collision_poly[i].x -viewport_pos.x;
y_en = body->collision_poly[i].y -viewport_pos.y;
printf("colpoly: %d %d %d %d\n", x_st, y_st, x_en, y_en);
SDL_RenderDrawLine(ren, x_st, y_st, x_en, y_en);
x_st = x_en;
y_st = y_en;
}
x_en = body->collision_poly[0].x;
y_en = body->collision_poly[0].y;
x_en = body->collision_poly[0].x -viewport_pos.x;
y_en = body->collision_poly[0].y -viewport_pos.y;
SDL_RenderDrawLine(ren, x_st, y_st, x_en, y_en);
}
SDL_SetRenderDrawColor(ren, 0,0,0, 255);
@ -105,8 +121,8 @@ void draw_forces(SDL_Renderer *ren, Body *body) { @@ -105,8 +121,8 @@ void draw_forces(SDL_Renderer *ren, Body *body) {
return;
}
Vect start;
start.x = (int)body->x_pos;
start.y = (int)body->y_pos;
start.x = (int)body->position.x -viewport_pos.x;
start.y = (int)body->position.y -viewport_pos.y;
Vect F;
for (int i = 0; i < body->num_motors; i++) {
@ -122,27 +138,24 @@ void draw_forces(SDL_Renderer *ren, Body *body) { @@ -122,27 +138,24 @@ void draw_forces(SDL_Renderer *ren, Body *body) {
SDL_RenderDrawLine(ren, start.x, start.y, end.x, start.y);
}
SDL_SetRenderDrawColor(ren, 0,0,0, 255);
}
void draw_wall(SDL_Renderer *ren, Wall *wall) {
SDL_SetRenderDrawColor(ren, 120, 85, 188, 255);
int x_st, x_en, y_st, y_en;
x_st = wall->nodes[0].x + wall->physics->x_pos;
y_st = wall->nodes[0].y + wall->physics->y_pos;
x_st = wall->nodes[0].x + wall->physics->position.x -viewport_pos.x;
y_st = wall->nodes[0].y + wall->physics->position.y -viewport_pos.y;
for (int i = 1; i < wall->numNodes; i++) {
x_en = wall->nodes[i].x + wall->physics->x_pos;
y_en = wall->nodes[i].y + wall->physics->y_pos;
x_en = wall->nodes[i].x + wall->physics->position.x -viewport_pos.x;
y_en = wall->nodes[i].y + wall->physics->position.y -viewport_pos.y;
printf("wall: %d %d %d %d\n", x_st, y_st, x_en, y_en);
SDL_RenderDrawLine(ren, x_st, y_st, x_en, y_en);
x_st = x_en;
y_st = y_en;
}
x_en = wall->nodes[0].x + wall->physics->x_pos;
y_en = wall->nodes[0].y + wall->physics->y_pos;
x_en = wall->nodes[0].x + wall->physics->position.x -viewport_pos.x;
y_en = wall->nodes[0].y + wall->physics->position.y -viewport_pos.y;
SDL_RenderDrawLine(ren, x_st, y_st, x_en, y_en);
SDL_SetRenderDrawColor(ren, 0,0,0, 255);
@ -167,7 +180,12 @@ int distance_colour(int x, int y, int x2, int y2, int blackpoint) { @@ -167,7 +180,12 @@ int distance_colour(int x, int y, int x2, int y2, int blackpoint) {
}
return frac;
}
void update_viewport(double x, double y) {
viewport_pos.x = x - 300;
viewport_pos.y = y - 300;
}
@ -190,6 +208,7 @@ void redraw_buffer(SDL_Renderer * ren) { @@ -190,6 +208,7 @@ void redraw_buffer(SDL_Renderer * ren) {
int col = 0;
//SDL_GetMouseState(&newmousex, &newmousey);
update_viewport(player.physics->position.x, player.physics->position.y);
for (int i=0; i<things_in_world; i++) {
world_thing thing;
@ -198,9 +217,8 @@ void redraw_buffer(SDL_Renderer * ren) { @@ -198,9 +217,8 @@ void redraw_buffer(SDL_Renderer * ren) {
switch (thing.kind) {
case PLAYER:
newmousex = (*thing.player).physics->x_pos;
newmousey = (*thing.player).physics->y_pos;
draw_player(ren, (*thing.player).physics->x_pos, (*thing.player).physics->y_pos, thing.player->colliding);
draw_player(ren, (*thing.player).physics->position.x, (*thing.player).physics->position.y, thing.player->colliding);
draw_collision_poly(ren, thing.player->physics);
draw_forces(ren, thing.player->physics);
break;

2
draw.h

@ -29,6 +29,8 @@ void add_to_view(draw_type kind, void * object); @@ -29,6 +29,8 @@ void add_to_view(draw_type kind, void * object);
void redraw_buffer(SDL_Renderer * ren);
Vect viewport_pos;
int width, height;

84
game.c

@ -1,5 +1,4 @@ @@ -1,5 +1,4 @@
#include "game.h"
#include "vect.h"
player_st *glob_player;
void set_motor_newtons(Body *thing, int motorID, double x, double y);
@ -7,17 +6,14 @@ void set_motor_max_velocity(Body *thing, int motorID, double max); @@ -7,17 +6,14 @@ void set_motor_max_velocity(Body *thing, int motorID, double max);
// move the collision poly to the position of the player
void default_update_collision_poly(Body *body) {
printf("Collision polygon: ");
for (int i=0; i < body->collision_poly_size; i++) {
int x = body->collision_shape[i].x + body->x_pos;
int y = body->collision_shape[i].y + body->y_pos;
printf("%d %d,", x, y);
double x = body->collision_shape[i].x + body->position.x;
double y = body->collision_shape[i].y + body->position.y;
body->collision_poly[i].x = x;
body->collision_poly[i].y = y;
}
printf("\n");
fflush(stdout);
}
@ -36,7 +32,6 @@ void default_motor_curve(Motor *motor) { @@ -36,7 +32,6 @@ void default_motor_curve(Motor *motor) {
// @param uninitialised Body pointer
// @result: malloc and configure a physics thing pointer
void get_new_physics(Body **phys) {
static int uid = 0;
/* physics */
@ -80,8 +75,8 @@ player_st get_player(int x, int y) { @@ -80,8 +75,8 @@ player_st get_player(int x, int y) {
get_new_physics(&player.physics);
player.physics->dynamics = true;
player.physics->x_pos = x;
player.physics->y_pos = y;
player.physics->position.x = x;
player.physics->position.y = y;
// friction (not in use)
player.physics->glob_friction = 40; // drag coef * area
@ -97,7 +92,8 @@ player_st get_player(int x, int y) { @@ -97,7 +92,8 @@ player_st get_player(int x, int y) {
rect[2].x = 5; rect[2].y = 20;
rect[3].x = -5; rect[3].y = 20;
set_motor_newtons(player.physics, M_GRAVITY, 0.0, player.physics->obj_mass * 5);
// gravity
//set_motor_newtons(player.physics, M_GRAVITY, 0.0, player.physics->obj_mass * 5);
// walking motor
add_motor(player.physics, 0.0, player.physics->obj_mass * 9.81);
@ -350,11 +346,9 @@ void accel_thing(Body * thing, float x, float y) { @@ -350,11 +346,9 @@ void accel_thing(Body * thing, float x, float y) {
float x_adj = x / 1000.0;
float y_adj = y / 1000.0;
(*thing).y_acc += (float)y_adj;
(*thing).x_acc += (float)x_adj;
(*thing).acc.y += (float)y_adj;
(*thing).acc.x += (float)x_adj;
logwrite(DEBUG, "Accel thing\n");
}
void set_motor_max_velocity(Body *thing, int motorID, double max) {
@ -417,8 +411,8 @@ bool process_collisions(Body *thing) { @@ -417,8 +411,8 @@ bool process_collisions(Body *thing) {
* object's position.
*/
void advance_thing(Body * thing) {
thing->x_acc = 0;
thing->y_acc = 0;
thing->acc.x = 0;
thing->acc.y = 0;
thing->updateCollisionPoly(thing);
@ -444,8 +438,8 @@ void advance_thing(Body * thing) { @@ -444,8 +438,8 @@ void advance_thing(Body * thing) {
Vect V;
F.x = thing->motors[i].x;
F.y = thing->motors[i].y;
V.x = thing->x_vel;
V.y = thing->y_vel;
V.x = thing->vel.x;
V.y = thing->vel.y;
Vect vel_in_dir = project_vect(V, F);
double dirF = atan2(F.y, F.x);
@ -469,11 +463,11 @@ void advance_thing(Body * thing) { @@ -469,11 +463,11 @@ void advance_thing(Body * thing) {
// accelerate based on accel
printf("time: %f", (float)time_delta);
thing->x_vel += thing->x_acc * (float)time_delta;
thing->y_vel += thing->y_acc * (float)time_delta;
thing->vel.x += thing->acc.x * (float)time_delta;
thing->vel.y += thing->acc.y * (float)time_delta;
double velocity = sqrt((double)(thing->x_vel * thing->x_vel + thing->y_vel * thing->y_vel));
printf("\nvels %e %e\n\n", thing->x_vel, thing->y_vel);
double velocity = sqrt((double)(thing->vel.x * thing->vel.x + thing->vel.y * thing->vel.y));
printf("\nvels %e %e\n\n", thing->vel.x, thing->vel.y);
// simple air drag
@ -491,17 +485,17 @@ void advance_thing(Body * thing) { @@ -491,17 +485,17 @@ void advance_thing(Body * thing) {
/*}*/
if (fabs((*thing).x_vel) < 0.00001) {
(*thing).x_vel = 0;
if (fabs((*thing).vel.x) < 0.00001) {
(*thing).vel.x = 0;
}
if (fabs((*thing).y_vel) < 0.00001) {
(*thing).y_vel = 0;
if (fabs((*thing).vel.y) < 0.00001) {
(*thing).vel.y = 0;
}
int oldx = thing->x_pos;
int oldy = thing->y_pos;
(*thing).x_pos += (int)((*thing).x_vel * 1/2 * (float)time_delta);
(*thing).y_pos += (int)((*thing).y_vel * 1/2 * (float)time_delta);
double oldx = thing->position.x;
double oldy = thing->position.y;
thing->position.x += (thing->vel.x * 1/2 * (double)time_delta);
thing->position.y += (thing->vel.y * 1/2 * (double)time_delta);
// revert if this caused collision
thing->updateCollisionPoly(thing);
@ -514,25 +508,24 @@ void advance_thing(Body * thing) { @@ -514,25 +508,24 @@ void advance_thing(Body * thing) {
// wrap screen
if (thing->x_pos > width) {
thing->x_pos = 0;
}
/*if (thing->x_pos > width) {*/
/*thing->x_pos = 0;*/
/*}*/
if (thing->y_pos > height) {
thing->y_pos = 0;
}
/*if (thing->y_pos > height) {*/
/*thing->y_pos = 0;*/
/*}*/
if (thing->y_pos < 0) {
thing->y_pos = height;
}
/*if (thing->y_pos < 0) {*/
/*thing->y_pos = height;*/
/*}*/
if (thing->x_pos < 0) {
thing->x_pos = width;
}
/*if (thing->x_pos < 0) {*/
/*thing->x_pos = width;*/
/*}*/
}
void advance_things(void) {
for (int i = 0; i < things_in_world; i++) {
switch (world[i].kind) {
@ -588,6 +581,9 @@ void startgame(SDL_Renderer * ren) { @@ -588,6 +581,9 @@ void startgame(SDL_Renderer * ren) {
wall_world.wall = get_long_wall(4, wall_nodes);
wall_world.kind = STATIC_WALL_W;
add_to_world(wall_world);
viewport_pos.x = 0;
viewport_pos.y = 0;
}
float get_abs(float x,float y) {
@ -596,7 +592,7 @@ float get_abs(float x,float y) { @@ -596,7 +592,7 @@ float get_abs(float x,float y) {
void walk_player(int x, int y) {
if (y == -1) {
add_motor_newtons(glob_player->physics, M_PLAYER_WALK, 0, 100 * 10 * y);
add_motor_newtons(glob_player->physics, M_PLAYER_WALK, 0, 100 * y);
return;
}
add_motor_newtons(glob_player->physics, M_PLAYER_WALK, 100 * x , 100 * y);

22
game.h

@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <math.h>
#include "vect.h"
#ifndef _DEFGAME
@ -50,14 +51,23 @@ typedef struct BodyStruct{ @@ -50,14 +51,23 @@ typedef struct BodyStruct{
bool colliding;
int x_pos;
int y_pos;
// position in viewport (pixels)
SDL_Point screen_pos;
float x_vel;
float y_vel;
// SI Unit kinematics
/*------------------*/
Vect position;
float x_acc;
float y_acc;
Vect vel;
Vect acc;
//float x_vel;
//float y_vel;
//float x_acc;
//float y_acc;
/*------------------*/
// collisions
SDL_Point *collision_poly;

3
main.c

@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
#include <string.h>
#include <time.h>
#include <stdbool.h>
#include <pthread.h>
#include "logger.h"
#include "game.h"
@ -63,7 +64,7 @@ int main () { @@ -63,7 +64,7 @@ int main () {
logwrite(INFO, "Starting\n");
SDL_Window * win = make_window();
SDL_Renderer * ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_Renderer * ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
queue_for_cleanup(win, WINDOW);
queue_for_cleanup(ren, RENDERER);

Loading…
Cancel
Save