Add hitbox reduction

master
En Yi 2020-01-15 20:29:45 +08:00
parent 3480c4cdd3
commit 7474d2ebbe
4 changed files with 51 additions and 19 deletions

View File

@ -12,10 +12,11 @@ struct kinematic_obj
{ {
Rectangle rect; Rectangle rect;
Vector2 velocity; Vector2 velocity;
double scale; Vector2 pos;
double set_scale;
int ori_width; int ori_width;
int ori_height; int ori_height;
double dim_reduction[4];
double set_dim_reduction[4];
}; };
struct kinematic_obj_node struct kinematic_obj_node
@ -46,6 +47,8 @@ struct player_obj
enum PLAYER_STATE state; enum PLAYER_STATE state;
}; };
extern unsigned int PLAYER_SIZE;
struct squishy_square struct squishy_square
{ {
struct kinematic_obj *parent; struct kinematic_obj *parent;
@ -68,6 +71,7 @@ struct kinematic_obj init_kinematic_obj(int width, int height);
void move(struct kinematic_obj *obj, Vector2 acceleration); void move(struct kinematic_obj *obj, Vector2 acceleration);
void set_position(struct kinematic_obj *obj, int x, int y); void set_position(struct kinematic_obj *obj, int x, int y);
bool place_meeting(struct kinematic_obj *obj, Vector2 dir); bool place_meeting(struct kinematic_obj *obj, Vector2 dir);
void scale_rect(struct kinematic_obj *obj);
// Math functions, math.c // Math functions, math.c
long mag(Vector2 vec); long mag(Vector2 vec);

2
main.c
View File

@ -40,7 +40,7 @@ int main()
camera.zoom = 1.0f; camera.zoom = 1.0f;
struct player_obj player = { struct player_obj player = {
.kinematic = init_kinematic_obj(40, 40), .kinematic = init_kinematic_obj(PLAYER_SIZE, PLAYER_SIZE),
.state = IDLE .state = IDLE
}; };

View File

@ -4,18 +4,20 @@ extern struct kinematic_obj_node *kinematic_HEAD;
struct kinematic_obj init_kinematic_obj(int width, int height){ struct kinematic_obj init_kinematic_obj(int width, int height){
struct kinematic_obj obj = { struct kinematic_obj obj = {
.velocity = {0.0f,0.0f}, .velocity = {0.0f,0.0f},
.pos = {0.0f,0.0f},
.rect = {0,0,width,height}, .rect = {0,0,width,height},
.scale = 1.0,
.set_scale = 1.0,
.ori_width = width, .ori_width = width,
.ori_height = height .ori_height = height,
.dim_reduction = {0,0,0,0}
}; };
return obj; return obj;
}; };
void set_position(struct kinematic_obj *obj, int x, int y){ void set_position(struct kinematic_obj *obj, int x, int y){
obj->pos.x = x;
obj->rect.x = x; obj->rect.x = x;
obj->pos.y = y;
obj->rect.y = y; obj->rect.y = y;
}; };
@ -27,7 +29,10 @@ void move(struct kinematic_obj *obj, Vector2 acceleration){
struct kinematic_obj_node *current; struct kinematic_obj_node *current;
//Simplistic Collision Handling for AABB, Could add coeff of restitution? //Simplistic Collision Handling for AABB, Could add coeff of restitution?
obj->velocity.x += acceleration.x * delta; obj->velocity.x += acceleration.x * delta;
obj->rect.x += obj->velocity.x * delta; obj->pos.x += obj->velocity.x * delta;
obj->rect.x = obj->pos.x + obj->dim_reduction[0];
obj->rect.width = obj->ori_width - obj->dim_reduction[0] - obj->dim_reduction[1];
current = kinematic_HEAD; current = kinematic_HEAD;
while(current != NULL){ while(current != NULL){
if(current->obj != obj){ if(current->obj != obj){
@ -36,15 +41,19 @@ void move(struct kinematic_obj *obj, Vector2 acceleration){
if(collide_rect.width < collide_rect.height){ if(collide_rect.width < collide_rect.height){
if (!place_meeting(obj, (Vector2){-collide_rect.width,0})){ if (!place_meeting(obj, (Vector2){-collide_rect.width,0})){
obj->rect.x -= collide_rect.width; obj->rect.x -= collide_rect.width;
obj->pos.x -= collide_rect.width;
}else{ }else{
obj->rect.x += collide_rect.width; obj->rect.x += collide_rect.width;
obj->pos.x += collide_rect.width;
} }
obj->velocity.x = 0; obj->velocity.x = 0;
}else{ }else{
if (!place_meeting(obj, (Vector2){0,-collide_rect.height})){ if (!place_meeting(obj, (Vector2){0,-collide_rect.height})){
obj->rect.y -= collide_rect.height; obj->rect.y -= collide_rect.height;
obj->pos.y -= collide_rect.height;
}else{ }else{
obj->rect.y += collide_rect.height; obj->rect.y += collide_rect.height;
obj->pos.y += collide_rect.height;
} }
obj->velocity.y = 0; obj->velocity.y = 0;
} }
@ -55,7 +64,10 @@ void move(struct kinematic_obj *obj, Vector2 acceleration){
// Repeat for y // Repeat for y
obj->velocity.y += acceleration.y * delta; obj->velocity.y += acceleration.y * delta;
obj->rect.y += obj->velocity.y * delta; obj->pos.y += obj->velocity.y * delta;
obj->rect.y = obj->pos.y + obj->dim_reduction[2];
obj->rect.height = obj->ori_height - obj->dim_reduction[2] - obj->dim_reduction[3];
current = kinematic_HEAD; current = kinematic_HEAD;
while(current != NULL){ while(current != NULL){
if(current->obj != obj){ if(current->obj != obj){
@ -63,9 +75,11 @@ void move(struct kinematic_obj *obj, Vector2 acceleration){
collide_rect = GetCollisionRec(obj->rect, current->obj->rect); collide_rect = GetCollisionRec(obj->rect, current->obj->rect);
if(collide_rect.width < collide_rect.height){ if(collide_rect.width < collide_rect.height){
obj->rect.x -= sign(obj->velocity.x) * collide_rect.width; obj->rect.x -= sign(obj->velocity.x) * collide_rect.width;
obj->pos.x -= sign(obj->velocity.x) * collide_rect.width;
obj->velocity.x = 0; obj->velocity.x = 0;
}else{ }else{
obj->rect.y -= sign(obj->velocity.y) * collide_rect.height; obj->rect.y -= sign(obj->velocity.y) * collide_rect.height;
obj->pos.y -= sign(obj->velocity.y) * collide_rect.height;
obj->velocity.y = 0; obj->velocity.y = 0;
} }
} }

View File

@ -20,6 +20,8 @@ const unsigned int run_start_frames = 10;
const unsigned int jump_squat_frames = 4; const unsigned int jump_squat_frames = 4;
const unsigned int land_lag_frames = 6; const unsigned int land_lag_frames = 6;
extern unsigned int PLAYER_SIZE = 40;
// The player FSM // The player FSM
void player_input_check(struct player_obj *player){ void player_input_check(struct player_obj *player){
Vector2 accel = (Vector2){ Vector2 accel = (Vector2){
@ -140,17 +142,29 @@ void player_input_check(struct player_obj *player){
} }
// TODO: Add a key to resize the rect and see what happens? // TODO: Add a key to resize the rect and see what happens?
if (IsKeyDown(KEY_P)){ Vector2 offset = (Vector2){0,0};
player->kinematic.set_scale = 1.2; player->kinematic.set_dim_reduction[0] = 0;
} if (IsKeyDown(KEY_J))
else if (IsKeyDown(KEY_O)){ player->kinematic.set_dim_reduction[0] = 10;
player->kinematic.set_scale = 0.85;
}else{ player->kinematic.set_dim_reduction[1] = 0;
player->kinematic.set_scale = 1; if (IsKeyDown(KEY_L))
} player->kinematic.set_dim_reduction[1] = 10;
approach(&player->kinematic.scale, player->kinematic.set_scale, 0.5);
player->kinematic.rect.width = player->kinematic.scale * player->kinematic.ori_width; player->kinematic.set_dim_reduction[2] = 0;
player->kinematic.rect.height = player->kinematic.scale * player->kinematic.ori_height; if (IsKeyDown(KEY_I))
player->kinematic.set_dim_reduction[2] = 10;
player->kinematic.set_dim_reduction[3] = 0;
if (IsKeyDown(KEY_K))
player->kinematic.set_dim_reduction[3] = 10;
approach(&player->kinematic.dim_reduction[0], player->kinematic.set_dim_reduction[0], 0.5);
approach(&player->kinematic.dim_reduction[1], player->kinematic.set_dim_reduction[1], 0.5);
approach(&player->kinematic.dim_reduction[2], player->kinematic.set_dim_reduction[2], 0.5);
approach(&player->kinematic.dim_reduction[3], player->kinematic.set_dim_reduction[3], 0.5);
//scale_rect(&player->kinematic);
if (IsKeyPressed(JUMP) && jumps > 0){ if (IsKeyPressed(JUMP) && jumps > 0){
player->state = JUMP_SQUAT; player->state = JUMP_SQUAT;