#include "world.h"
#include "base.h"


phy_world *phy_create_world(v3 gravity)
{
	phy_world *world = PHY_ALLOC(phy_world, 1);
	world->bodies = NULL;
	world->bodies_count = 0;
	world->gravity = gravity;
	return world;
}

void phy_destroy_world(phy_world *world)
{
	PHY_FREE(world->bodies);
	world->bodies_count = 0;
}

phy_body_offset phy_new_body(phy_world *world)
{
	phy_body_offset offset = world->bodies_count;
	world->bodies_count++;
	world->bodies = PHY_REALLOC(phy_body, world->bodies, world->bodies_count);
	return offset;
}

void phy_body_init_default(phy_body *body)
{
	body->position = {0, 0, 0};
	body->rotation = {0, 0, 0};
	body->mass = 0;
	body->center_of_mass = {0, 0, 0};

	body->velocity = {0, 0, 0};
	body->angular_velocity = {0, 0, 0};

	body->gravity_multiplier = 1;

	body->friction = 0;
	body->bounciness = 1;
}

phy_body_offset phy_create_box(phy_world *world, v3 position, v3 rotation, f32 mass, v3 dimensions)
{
	phy_body_offset offset = phy_new_body(world);
	phy_body *body = PHY_BODY(world, offset);

	phy_body_init_default(body);
	body->position = position;
	body->rotation = rotation;
	body->mass = mass;
	body->shape = PHY_SHAPE_BOX;
	body->box.dimensions = dimensions;

	return offset;
}

phy_body_offset phy_create_sphere(phy_world *world, v3 position, v3 rotation, f32 mass, f32 radius)
{
	phy_body_offset offset = phy_new_body(world);
	phy_body *body = PHY_BODY(world, offset);

	phy_body_init_default(body);
	body->position = position;
	body->rotation = rotation;
	body->mass = mass;
	body->shape = PHY_SHAPE_SPHERE;
	body->sphere.radius = radius;

	return offset;
}
