Physics and collision
parent
cd03e481c3
commit
c6673be36d
10
src/client.c
10
src/client.c
|
@ -92,13 +92,15 @@ static void client_loop()
|
|||
|
||||
init_camera(window, prog);
|
||||
|
||||
set_camera_position(client.player.pos);
|
||||
set_camera_angle(client.player.yaw, client.player.pitch);
|
||||
set_camera_position((v3f) {0.0f, 0.0f, 0.0f});
|
||||
set_camera_angle(0.0f, 0.0f);
|
||||
|
||||
set_window_size(width, height);
|
||||
|
||||
init_input(&client, window);
|
||||
|
||||
clientplayer_send_pos(&client.player);
|
||||
|
||||
struct timespec ts, ts_old;
|
||||
clock_gettime(CLOCK_REALTIME, &ts_old);
|
||||
|
||||
|
@ -172,10 +174,8 @@ static void client_start(int fd)
|
|||
client.name = NULL;
|
||||
client.map = map_create();
|
||||
client.scene = scene_create();
|
||||
client.player.client = &client;
|
||||
client.player.pos = (v3f) {0.0f, 0.0f, 0.0f};
|
||||
client.player.yaw = client.player.pitch = 0.0;
|
||||
|
||||
clientplayer_init(&client);
|
||||
clientmap_init(&client);
|
||||
|
||||
pthread_t recv_thread;
|
||||
|
|
|
@ -1,26 +1,68 @@
|
|||
#include <stdio.h>
|
||||
#include "camera.h"
|
||||
#include "client.h"
|
||||
#include "clientplayer.h"
|
||||
|
||||
static void send_pos(ClientPlayer *player)
|
||||
void clientplayer_send_pos(ClientPlayer *player)
|
||||
{
|
||||
pthread_mutex_lock(&player->client->mtx);
|
||||
(void) (write_u32(player->client->fd, SC_POS) && write_v3f32(player->client->fd, player->pos));
|
||||
pthread_mutex_unlock(&player->client->mtx);
|
||||
}
|
||||
|
||||
void clientplayer_init(Client *client)
|
||||
{
|
||||
client->player.client = client;
|
||||
client->player.pos = (v3f) {0.0f, 25.0f, 0.0f};
|
||||
client->player.velocity = (v3f) {0.0f, 25.0f, 0.0f};
|
||||
client->player.box = (aabb3f) {{-0.3f, 0.0f, -0.3f}, {0.3f, 1.75f, 0.3f}};
|
||||
client->player.yaw = client->player.pitch = 0.0f;
|
||||
client->player.eye_height = 1.6f;
|
||||
}
|
||||
|
||||
static bool collision(ClientPlayer *player)
|
||||
{
|
||||
aabb3f box = {
|
||||
{player->box.min.x + player->pos.x, player->box.min.y + player->pos.y, player->box.min.z + player->pos.z},
|
||||
{player->box.max.x + player->pos.x, player->box.max.y + player->pos.y, player->box.max.z + player->pos.z},
|
||||
};
|
||||
|
||||
for (s32 x = floor(box.min.x); x <= ceil(box.max.x - 0.01f) - 1; x++) {
|
||||
for (s32 y = floor(box.min.y); y <= ceil(box.max.y - 0.01f) - 1; y++) {
|
||||
for (s32 z = floor(box.min.z); z <= ceil(box.max.z - 0.01f) - 1; z++) {
|
||||
Node node = map_get_node(player->client->map, (v3s32) {x, y, z}).type;
|
||||
|
||||
if (node == NODE_UNLOADED || node_definitions[node].solid)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void clientplayer_tick(ClientPlayer *player, f64 dtime)
|
||||
{
|
||||
v3f old_pos = player->pos;
|
||||
|
||||
player->pos.x += player->velocity.x * dtime;
|
||||
player->pos.y += player->velocity.y * dtime;
|
||||
player->pos.z += player->velocity.z * dtime;
|
||||
#define CALC_PHYSICS(pos, velocity, old) \
|
||||
pos += velocity * dtime; \
|
||||
if (collision(player)) { \
|
||||
pos = old; \
|
||||
velocity = 0.0f; \
|
||||
}
|
||||
|
||||
CALC_PHYSICS(player->pos.x, player->velocity.x, old_pos.x)
|
||||
CALC_PHYSICS(player->pos.y, player->velocity.y, old_pos.y)
|
||||
CALC_PHYSICS(player->pos.z, player->velocity.z, old_pos.z)
|
||||
|
||||
#undef CALC_PHYSICS
|
||||
|
||||
// gravity
|
||||
player->velocity.y -= 9.81f * dtime;
|
||||
|
||||
if (old_pos.x != player->pos.x || old_pos.y != player->pos.y || old_pos.z != player->pos.z) {
|
||||
send_pos(player);
|
||||
set_camera_position(player->pos);
|
||||
clientplayer_send_pos(player);
|
||||
set_camera_position((v3f) {player->pos.x, player->pos.y + player->eye_height, player->pos.z});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,10 +7,14 @@ typedef struct
|
|||
{
|
||||
struct Client *client;
|
||||
v3f pos;
|
||||
f32 yaw, pitch;
|
||||
v3f velocity;
|
||||
aabb3f box;
|
||||
f32 yaw, pitch;
|
||||
f32 eye_height;
|
||||
} ClientPlayer;
|
||||
|
||||
void clientplayer_init(struct Client *client);
|
||||
void clientplayer_send_pos(ClientPlayer *player);
|
||||
void clientplayer_tick(ClientPlayer *player, f64 dtime);
|
||||
|
||||
#endif
|
||||
|
|
30
src/node.c
30
src/node.c
|
@ -2,9 +2,29 @@
|
|||
#include "util.h"
|
||||
|
||||
NodeDefintion node_definitions[NODE_UNLOADED] = {
|
||||
{true},
|
||||
{false},
|
||||
{true},
|
||||
{true},
|
||||
{true},
|
||||
// invalid
|
||||
{
|
||||
.visible = true,
|
||||
.solid = true,
|
||||
},
|
||||
// air
|
||||
{
|
||||
.visible = false,
|
||||
.solid = false,
|
||||
},
|
||||
// grass
|
||||
{
|
||||
.visible = true,
|
||||
.solid = true,
|
||||
},
|
||||
// dirt
|
||||
{
|
||||
.visible = true,
|
||||
.solid = true,
|
||||
},
|
||||
// stone
|
||||
{
|
||||
.visible = true,
|
||||
.solid = true,
|
||||
},
|
||||
};
|
||||
|
|
|
@ -17,6 +17,7 @@ typedef enum
|
|||
typedef struct
|
||||
{
|
||||
bool visible;
|
||||
bool solid;
|
||||
} NodeDefintion;
|
||||
|
||||
extern NodeDefintion node_definitions[];
|
||||
|
|
Loading…
Reference in New Issue