initial collision rewrite
This commit is contained in:
parent
724383a8c0
commit
09ed16029c
1
Makefile
1
Makefile
@ -104,6 +104,7 @@ MAIN_OBJS = \
|
||||
src/opengl.o \
|
||||
src/render.o \
|
||||
src/collision.o \
|
||||
src/collision2.o \
|
||||
src/update.o \
|
||||
src/unparse.o \
|
||||
$(patsubst %.glsl,%.glsl.o,$(wildcard src/shader/*.glsl)) \
|
||||
|
||||
5
include/collision2.hpp
Normal file
5
include/collision2.hpp
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "math/float_types.hpp"
|
||||
|
||||
bool aabb_circle_collision(vec3 aabb_position, vec3 circle_position);
|
||||
@ -1,10 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct block_state {
|
||||
float destroyed_time;
|
||||
};
|
||||
|
||||
struct game_state {
|
||||
struct block_state blocks[28 * 13];
|
||||
const uint8_t * level;
|
||||
const uint8_t * pal;
|
||||
|
||||
float paddle_x;
|
||||
float paddle_y;
|
||||
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void reset_level(struct game_state * state);
|
||||
void update(struct game_state * state);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@ -260,29 +260,3 @@ vec4 collision(mat4x4 trans,
|
||||
smallest_normal.z,
|
||||
collision ? smallest : -9999.0f);
|
||||
}
|
||||
|
||||
/*
|
||||
bool collision(float ball_x, float ball_y,
|
||||
float ball_dx, float ball_dy)
|
||||
{
|
||||
const uint8_t * level = (const uint8_t *)src_level_level1_data_start;
|
||||
const uint8_t * pal = (const uint8_t *)src_level_level1_data_pal_start;
|
||||
|
||||
for (int y = 0; y < 28; y++) {
|
||||
for (int x = 0; x < 13; x++) {
|
||||
char tile = level[y * 13 + x];
|
||||
if (tile == 0)
|
||||
continue;
|
||||
|
||||
//if ( (paddle_x + 1.0) >= ((float)x - 1.0)
|
||||
//&& (paddle_x - 1.0) <= ((float)x + 1.0)
|
||||
//&& (paddle_y + 1.0) >= ((float)y - 1.0)
|
||||
//&& (paddle_y - 1.0) <= ((float)y + 1.0)) {
|
||||
|
||||
collision_inner(translate(vec3(x, y, 0)),
|
||||
vec3(ball_x, ball_y, 0),
|
||||
vec3(ball_dx, ball_dy, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
34
src/collision2.cpp
Normal file
34
src/collision2.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#include <math.h>
|
||||
|
||||
#include "collision2.hpp"
|
||||
|
||||
static inline float clamp(float n, float min, float max)
|
||||
{
|
||||
if (n < min)
|
||||
return min;
|
||||
if (n > max)
|
||||
return max;
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline vec3 clamp(vec3 v, vec3 min_v, vec3 max_v)
|
||||
{
|
||||
return vec3(clamp(v.x, min_v.x, max_v.x),
|
||||
clamp(v.y, min_v.y, max_v.y),
|
||||
clamp(v.z, min_v.z, max_v.z));
|
||||
}
|
||||
|
||||
bool aabb_circle_collision(vec3 aabb_position, vec3 circle_position)
|
||||
{
|
||||
const vec3 bounds = vec3(2, 1, 0);
|
||||
|
||||
vec3 distance = circle_position - aabb_position;
|
||||
vec3 closest_point_pd = clamp(distance, -bounds, bounds);
|
||||
vec3 closest_point_p = aabb_position + closest_point_pd;
|
||||
|
||||
bool collided = fabsf(magnitude(closest_point_p - circle_position)) < 1;
|
||||
if (!collided)
|
||||
return collided;
|
||||
|
||||
return collided;
|
||||
}
|
||||
14
src/main.c
14
src/main.c
@ -164,15 +164,7 @@ int main()
|
||||
const char * last_gamepad_name = NULL;
|
||||
|
||||
struct game_state state;
|
||||
state.paddle_x = 0.0;
|
||||
state.paddle_y = 26.0;
|
||||
|
||||
state.ball_x = 0.0;
|
||||
state.ball_y = 25.0;
|
||||
|
||||
state.ball_dx = 0.1;
|
||||
state.ball_dy = 0.1;
|
||||
|
||||
reset_level(&state);
|
||||
state.start_time = glfwGetTime();
|
||||
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
@ -204,8 +196,8 @@ int main()
|
||||
float sensitivity = 0.4f;
|
||||
paddle_dx = (right - left) * sensitivity;
|
||||
|
||||
//state.ball_dx = deadzone(axes[0]);
|
||||
//state.ball_dy = deadzone(axes[1]);
|
||||
state.ball_dx = deadzone(axes[0]);
|
||||
state.ball_dy = deadzone(axes[1]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,13 +6,11 @@
|
||||
|
||||
#include "unparse.h"
|
||||
#include "collision.hpp"
|
||||
#include "collision2.hpp"
|
||||
#include "render.hpp"
|
||||
#include "math/float_types.hpp"
|
||||
#include "math/transform.hpp"
|
||||
|
||||
#include "level/level1.data.h"
|
||||
#include "level/level1.data.pal.h"
|
||||
|
||||
#define PI 3.14159274101257324219f
|
||||
|
||||
extern int vp_width;
|
||||
@ -131,25 +129,27 @@ void render(mesh paddle_mesh,
|
||||
glEnableVertexAttribArray(attrib_texture);
|
||||
glEnableVertexAttribArray(attrib_normal);
|
||||
|
||||
assert(src_level_level1_data_size == 13 * 28);
|
||||
const uint8_t * level = (const uint8_t *)src_level_level1_data_start;
|
||||
const uint8_t * pal = (const uint8_t *)src_level_level1_data_pal_start;
|
||||
|
||||
vec3 light_pos = normalize(rotate_z(theta) * vec3(1, 1, 1));
|
||||
|
||||
for (int y = 0; y < 28; y++) {
|
||||
for (int x = 0; x < 13; x++) {
|
||||
char tile = level[y * 13 + x];
|
||||
if (tile == 0)
|
||||
continue;
|
||||
char tile = state->level[y * 13 + x];
|
||||
//if (tile == 0)
|
||||
//continue;
|
||||
|
||||
const float cs = 1.0f / 255.0f;
|
||||
vec3 base_color = vec3(((float)pal[tile * 3 + 0]) * cs,
|
||||
((float)pal[tile * 3 + 1]) * cs,
|
||||
((float)pal[tile * 3 + 2]) * cs);
|
||||
vec3 base_color = vec3(((float)state->pal[tile * 3 + 0]) * cs,
|
||||
((float)state->pal[tile * 3 + 1]) * cs,
|
||||
((float)state->pal[tile * 3 + 2]) * cs);
|
||||
|
||||
vec3 block_position = vec3(x * 4.0f, -y * 2.0f, 0.0f);
|
||||
vec3 ball_position = vec3(state->ball_x * 4.0f, -state->ball_y * 2.0f, 0.0);
|
||||
bool collided = aabb_circle_collision(block_position, ball_position);
|
||||
if (collided)
|
||||
base_color = vec3(1, 0, 0);
|
||||
|
||||
mat4x4 rx = rotate_x(-PI / 2.0f);
|
||||
mat4x4 t = translate(vec3(x * 4.0f, -y * 2.0f, 0.0f));
|
||||
mat4x4 t = translate(block_position);
|
||||
|
||||
mat4x4 trans = a * t * rx;
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "update.hpp"
|
||||
#include "collision.hpp"
|
||||
@ -11,7 +12,32 @@
|
||||
#include "math/float_types.hpp"
|
||||
#include "math/transform.hpp"
|
||||
|
||||
void reset_level(struct game_state * state)
|
||||
{
|
||||
state->paddle_x = 0.0;
|
||||
state->paddle_y = 26.0;
|
||||
|
||||
state->ball_x = 0.0;
|
||||
state->ball_y = 25.0;
|
||||
|
||||
state->ball_dx = 0.01;
|
||||
state->ball_dy = 0.01;
|
||||
|
||||
state->start_time = 0.0;
|
||||
|
||||
assert(src_level_level1_data_size == 13 * 28);
|
||||
const uint8_t * level = (const uint8_t *)src_level_level1_data_start;
|
||||
const uint8_t * pal = (const uint8_t *)src_level_level1_data_pal_start;
|
||||
|
||||
state->level = level;
|
||||
state->pal = pal;
|
||||
for (int i = 0; i < 28 * 13; i++) {
|
||||
state->blocks[i].destroyed_time = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
void update(struct game_state * state)
|
||||
{
|
||||
|
||||
state->ball_x += state->ball_dx;
|
||||
state->ball_y += state->ball_dy;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user