Scale factor and expansion upon basic implementation
This commit is contained in:
115
src/physics.c
Normal file
115
src/physics.c
Normal file
@@ -0,0 +1,115 @@
|
||||
#include <math.h>
|
||||
|
||||
#include "physics.h"
|
||||
#include "world.h"
|
||||
#include "config.h"
|
||||
|
||||
void UpdateEntities(float dt, int screenWidth)
|
||||
{
|
||||
for (int i = 0; i < MAX_ENTITIES; i++)
|
||||
{
|
||||
Entity *e = &entities[i];
|
||||
|
||||
if (!e->active)
|
||||
continue;
|
||||
|
||||
bool onGround = IsGrounded(e);
|
||||
|
||||
if (e->affectedByGravity)
|
||||
{
|
||||
ApplyForce(
|
||||
e,
|
||||
(Vector2){0, e->mass * g}
|
||||
);
|
||||
}
|
||||
|
||||
e->acceleration.x =
|
||||
e->force.x / e->mass;
|
||||
|
||||
e->acceleration.y =
|
||||
e->force.y / e->mass;
|
||||
|
||||
e->velocity.x +=
|
||||
e->acceleration.x * dt;
|
||||
|
||||
e->velocity.y +=
|
||||
e->acceleration.y * dt;
|
||||
|
||||
e->velocity.x *= AIR_DRAG;
|
||||
e->velocity.y *= AIR_DRAG;
|
||||
|
||||
if (onGround)
|
||||
{
|
||||
if (e->velocity.x > 0)
|
||||
{
|
||||
e->velocity.x -=
|
||||
GROUND_FRICTION * dt;
|
||||
|
||||
if (e->velocity.x < 0)
|
||||
e->velocity.x = 0;
|
||||
}
|
||||
else if (e->velocity.x < 0)
|
||||
{
|
||||
e->velocity.x +=
|
||||
GROUND_FRICTION * dt;
|
||||
|
||||
if (e->velocity.x > 0)
|
||||
e->velocity.x = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (e->isPlayer)
|
||||
{
|
||||
if (e->velocity.x > MAX_PLAYER_SPEED)
|
||||
e->velocity.x = MAX_PLAYER_SPEED;
|
||||
|
||||
if (e->velocity.x < -MAX_PLAYER_SPEED)
|
||||
e->velocity.x = -MAX_PLAYER_SPEED;
|
||||
}
|
||||
|
||||
e->position.x +=
|
||||
e->velocity.x * dt;
|
||||
|
||||
e->position.y +=
|
||||
e->velocity.y * dt;
|
||||
|
||||
e->force = (Vector2){0, 0};
|
||||
|
||||
if (e->position.y +
|
||||
e->size * 0.5f >=
|
||||
ground_y)
|
||||
{
|
||||
e->position.y =
|
||||
ground_y -
|
||||
e->size * 0.5f;
|
||||
|
||||
e->velocity.y *=
|
||||
-BOUNCE;
|
||||
|
||||
if (fabsf(e->velocity.y) < 15.0f)
|
||||
{
|
||||
e->velocity.y = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (e->position.x -
|
||||
e->size * 0.5f < 0)
|
||||
{
|
||||
e->position.x =
|
||||
e->size * 0.5f;
|
||||
|
||||
e->velocity.x *= -0.6f;
|
||||
}
|
||||
|
||||
if (e->position.x +
|
||||
e->size * 0.5f >
|
||||
screenWidth)
|
||||
{
|
||||
e->position.x =
|
||||
screenWidth -
|
||||
e->size * 0.5f;
|
||||
|
||||
e->velocity.x *= -0.6f;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user