collision/more physics updates
All checks were successful
Build Project / build (ubuntu-latest) (push) Successful in 17m55s
All checks were successful
Build Project / build (ubuntu-latest) (push) Successful in 17m55s
This commit is contained in:
@@ -3,34 +3,42 @@
|
||||
#include "config.h"
|
||||
#include "collision.h"
|
||||
#include "player.h"
|
||||
|
||||
#include <stdio.h>
|
||||
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)
|
||||
{
|
||||
|
||||
float forcex=fabsf(((en1->mass+en2->mass)/2)*((((en1->velocity.x)+(scale))+((en2->velocity.x)+(scale)))/2))*(scale);
|
||||
if (overlapY < overlapX && en1->position.y < en2->position.y)
|
||||
{
|
||||
// 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}
|
||||
@@ -54,4 +64,5 @@ void checkCollision(){
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ typedef struct Entity
|
||||
|
||||
bool isPlayer;
|
||||
bool affectedByGravity;
|
||||
bool headgrounded;
|
||||
|
||||
float mass;
|
||||
float size;
|
||||
|
||||
@@ -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,
|
||||
|
||||
14
src/world.c
14
src/world.c
@@ -1,5 +1,6 @@
|
||||
#include <stddef.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
BIN
wrldboxMacOS
BIN
wrldboxMacOS
Binary file not shown.
Reference in New Issue
Block a user