collision/more physics updates
All checks were successful
Build Project / build (ubuntu-latest) (push) Successful in 17m55s

This commit is contained in:
swim67667
2026-06-27 00:54:11 -04:00
parent 40747ec508
commit fded40cbd9
5 changed files with 35 additions and 21 deletions

View File

@@ -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)
{
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}
);
}
}
}
}
}