diff --git a/src/collision.c b/src/collision.c index 14135c9..7e099c8 100644 --- a/src/collision.c +++ b/src/collision.c @@ -3,34 +3,42 @@ #include "config.h" #include "collision.h" #include "player.h" - +#include void checkCollision(){ for(int u=0; u<=MAX_ENTITIES-1; u++){ Entity *en1 = &entities[u]; if(!en1->active){ continue; } + en1->headgrounded = false; for(int j=u+1; j<=MAX_ENTITIES-1; j++){ if(u!=j){ Entity *en2 = &entities[j]; if(!en2->active){ continue; } - if (fabsf(en1->position.x - en2->position.x) < (en1->size * 0.5f + en2->size * 0.5f) && fabsf(en1->position.y - en2->position.y) < (en1->size * 0.5f + en2->size * 0.5f)) + printf("%d\n", en1->headgrounded); + + float half = (en1->size + en2->size) * 0.5f; + float overlapX = half - fabsf(en1->position.x - en2->position.x); + float overlapY = half - fabsf(en1->position.y - en2->position.y); + + if (overlapX > 0 && overlapY > 0) + { + if (overlapY < overlapX && en1->position.y < en2->position.y) { - - float forcex=fabsf(((en1->mass+en2->mass)/2)*((((en1->velocity.x)+(scale))+((en2->velocity.x)+(scale)))/2))*(scale); + // top collision + en1->position.y = en2->position.y - half; + en1->velocity.y = 0; + en1->velocity.x *= en2->drag; + en1->headgrounded = true; + } else { + en1->headgrounded = false; + float forcex=fabsf(((en1->mass+en2->mass)/2)*((((en1->velocity.x)+(scale))+((en2->velocity.x)+(scale)))/2))*(scale*1.5); float forcey=fabsf(((en1->mass+en2->mass)/2)*((((en1->velocity.y)+(scale))+((en2->velocity.y)+(scale)))/2))*(scale/4); - if(en1->position.y+en1->size*0.6f <= en2->position.y+en2->size*0.5f){ - ApplyForce( - en1, - (Vector2){0, -((en1->mass*g))} - ); - en1->velocity.y = en1->velocity.y * 0.1f; - en1->velocity.x = en1->velocity.x * en2->drag; - } - else if(en1->position.x < en2->position.x){ - + if(en1->position.x < en2->position.x){ + en1->velocity.x=0; + en2->velocity.x=0; ApplyForce( en1, (Vector2){-forcex, 0} @@ -41,6 +49,8 @@ void checkCollision(){ ); } else{ + en1->velocity.x=0; + en2->velocity.x=0; ApplyForce( en1, (Vector2){forcex, 0} @@ -50,6 +60,7 @@ void checkCollision(){ (Vector2){-forcex, 0} ); } + } } } } diff --git a/src/entity.h b/src/entity.h index 775a895..a1eeb62 100644 --- a/src/entity.h +++ b/src/entity.h @@ -10,6 +10,7 @@ typedef struct Entity bool isPlayer; bool affectedByGravity; + bool headgrounded; float mass; float size; diff --git a/src/physics.c b/src/physics.c index d6abc6d..a1f2dc4 100644 --- a/src/physics.c +++ b/src/physics.c @@ -21,7 +21,7 @@ void UpdateEntities(float dt, int screenWidth) bool onGround = IsGrounded(e); // Apply gravity force if enabled for this entity - if (e->affectedByGravity) + if (e->affectedByGravity && !onGround) { ApplyForce( e, diff --git a/src/world.c b/src/world.c index a283714..37d0f6f 100644 --- a/src/world.c +++ b/src/world.c @@ -1,5 +1,6 @@ #include #include "world.h" +#include "collision.h" // the beeg one. hooooooooooooooooh boy @@ -39,12 +40,12 @@ bool IsGrounded(Entity *e) { if (!e) return false; - - return ( - e->position.y + - e->size * 0.5f >= - ground_y - 2.0f - ); + if(e->position.y +e->size * 0.5f >=ground_y - 2.0f || e->headgrounded){ + return true; + } + else{ + return false; + } } Entity *SpawnEntity( @@ -76,6 +77,7 @@ Entity *SpawnEntity( e->force = (Vector2){0, 0}; e->color = color; + e->headgrounded = false; return e; } diff --git a/wrldboxMacOS b/wrldboxMacOS index aaee823..d0a1f35 100755 Binary files a/wrldboxMacOS and b/wrldboxMacOS differ