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 "config.h"
|
||||||
#include "collision.h"
|
#include "collision.h"
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
#include <stdio.h>
|
||||||
void checkCollision(){
|
void checkCollision(){
|
||||||
for(int u=0; u<=MAX_ENTITIES-1; u++){
|
for(int u=0; u<=MAX_ENTITIES-1; u++){
|
||||||
Entity *en1 = &entities[u];
|
Entity *en1 = &entities[u];
|
||||||
if(!en1->active){
|
if(!en1->active){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
en1->headgrounded = false;
|
||||||
for(int j=u+1; j<=MAX_ENTITIES-1; j++){
|
for(int j=u+1; j<=MAX_ENTITIES-1; j++){
|
||||||
if(u!=j){
|
if(u!=j){
|
||||||
Entity *en2 = &entities[j];
|
Entity *en2 = &entities[j];
|
||||||
if(!en2->active){
|
if(!en2->active){
|
||||||
continue;
|
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)
|
||||||
{
|
{
|
||||||
|
// top collision
|
||||||
float forcex=fabsf(((en1->mass+en2->mass)/2)*((((en1->velocity.x)+(scale))+((en2->velocity.x)+(scale)))/2))*(scale);
|
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);
|
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){
|
if(en1->position.x < en2->position.x){
|
||||||
ApplyForce(
|
en1->velocity.x=0;
|
||||||
en1,
|
en2->velocity.x=0;
|
||||||
(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){
|
|
||||||
|
|
||||||
ApplyForce(
|
ApplyForce(
|
||||||
en1,
|
en1,
|
||||||
(Vector2){-forcex, 0}
|
(Vector2){-forcex, 0}
|
||||||
@@ -41,6 +49,8 @@ void checkCollision(){
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
en1->velocity.x=0;
|
||||||
|
en2->velocity.x=0;
|
||||||
ApplyForce(
|
ApplyForce(
|
||||||
en1,
|
en1,
|
||||||
(Vector2){forcex, 0}
|
(Vector2){forcex, 0}
|
||||||
@@ -50,6 +60,7 @@ void checkCollision(){
|
|||||||
(Vector2){-forcex, 0}
|
(Vector2){-forcex, 0}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ typedef struct Entity
|
|||||||
|
|
||||||
bool isPlayer;
|
bool isPlayer;
|
||||||
bool affectedByGravity;
|
bool affectedByGravity;
|
||||||
|
bool headgrounded;
|
||||||
|
|
||||||
float mass;
|
float mass;
|
||||||
float size;
|
float size;
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ void UpdateEntities(float dt, int screenWidth)
|
|||||||
bool onGround = IsGrounded(e);
|
bool onGround = IsGrounded(e);
|
||||||
|
|
||||||
// Apply gravity force if enabled for this entity
|
// Apply gravity force if enabled for this entity
|
||||||
if (e->affectedByGravity)
|
if (e->affectedByGravity && !onGround)
|
||||||
{
|
{
|
||||||
ApplyForce(
|
ApplyForce(
|
||||||
e,
|
e,
|
||||||
|
|||||||
14
src/world.c
14
src/world.c
@@ -1,5 +1,6 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include "world.h"
|
#include "world.h"
|
||||||
|
#include "collision.h"
|
||||||
|
|
||||||
// the beeg one. hooooooooooooooooh boy
|
// the beeg one. hooooooooooooooooh boy
|
||||||
|
|
||||||
@@ -39,12 +40,12 @@ bool IsGrounded(Entity *e)
|
|||||||
{
|
{
|
||||||
if (!e)
|
if (!e)
|
||||||
return false;
|
return false;
|
||||||
|
if(e->position.y +e->size * 0.5f >=ground_y - 2.0f || e->headgrounded){
|
||||||
return (
|
return true;
|
||||||
e->position.y +
|
}
|
||||||
e->size * 0.5f >=
|
else{
|
||||||
ground_y - 2.0f
|
return false;
|
||||||
);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Entity *SpawnEntity(
|
Entity *SpawnEntity(
|
||||||
@@ -76,6 +77,7 @@ Entity *SpawnEntity(
|
|||||||
e->force = (Vector2){0, 0};
|
e->force = (Vector2){0, 0};
|
||||||
|
|
||||||
e->color = color;
|
e->color = color;
|
||||||
|
e->headgrounded = false;
|
||||||
|
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
wrldboxMacOS
BIN
wrldboxMacOS
Binary file not shown.
Reference in New Issue
Block a user