diff --git a/src/collision.c b/src/collision.c index 7e099c82..0e5b7822 100644 --- a/src/collision.c +++ b/src/collision.c @@ -17,26 +17,40 @@ void checkCollision(){ if(!en2->active){ continue; } - printf("%d\n", en1->headgrounded); - + //calculate overlap float half = (en1->size + en2->size) * 0.5f; - float overlapX = half - fabsf(en1->position.x - en2->position.x); + float overlapXcheck = half - fabsf(en1->position.x - en2->position.x); + float overlapXapply = ((en1->size + en2->size) * 0.43f) - fabsf(en1->position.x - en2->position.x); float overlapY = half - fabsf(en1->position.y - en2->position.y); - - if (overlapX > 0 && overlapY > 0) + // check if any overlap + if (overlapXcheck > 0 && overlapY > 0) { - if (overlapY < overlapX && en1->position.y < en2->position.y) + if (overlapY < overlapXapply) { - // 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 < en2->position.y) + { + // if the first entity is above the second put norml force on first entity + en1->position.y = en2->position.y - half; + en1->velocity.y = 0; + en1->velocity.x *= en2->drag; + en1->headgrounded = true; + } + else + { + // if the second entity is above the fisrt put norml force on second entity + en2->position.y = en1->position.y - half; + en2->velocity.y = 0; + en2->velocity.x *= en1->drag; + en2->headgrounded = true; + } + } + else + { + // push apart calculation + float forcex = fabsf(((en1->mass+en2->mass)/2)*((((en1->velocity.x)+(scale))+((en2->velocity.x)+(scale)))/2))*(scale*1.5); + //direction resolution if(en1->position.x < en2->position.x){ + //kill velocity so they cant go throughe achother en1->velocity.x=0; en2->velocity.x=0; ApplyForce( diff --git a/wrldboxMacOS b/wrldboxMacOS index d0a1f355..6cdb3ae3 100755 Binary files a/wrldboxMacOS and b/wrldboxMacOS differ