Physics is now updated when entity's position/rotation is changed

dev
Shariq Shah 8 years ago
parent 0af53d7456
commit b3cecf3221
  1. 4
      README.md
  2. 2
      src/game/physics.c
  3. 1377
      src/libsymmetry/entity.c
  4. 12
      src/libsymmetry/entity.h
  5. 31
      src/libsymmetry/game.c
  6. 1
      src/libsymmetry/transform.c

@ -155,7 +155,6 @@
- ## TODO
- Update physics if entity position/rotation/scale etc are changed
- Physics raycasting
- Physics forces/torque etc
- Fix lights type not being correctly saved/loaded from file
@ -167,6 +166,8 @@
- Expose complete physics api with forces/joints etc
- Complete ODE integration
- Test physics code on linux
- Pipeline improvements, getting models/materials etc to/from other programs
- Necessary basic editor additions like placing objects, scaling, rotating etc
- Terrain rendering using heightfields
- Re-order lib folder for linux by putting all libraries in one folder
- Figure out a better way for handling libs on linux, current method DOES NOT work on other computers
@ -363,3 +364,4 @@
* Added physics spheres and other primitive shapes
* Separated collision shape and rigidbody
* Implemented Getting/Modifying primitive physics shapes' values like length, radius etc
* Update physics if entity position/rotation/scale etc are changed

@ -97,6 +97,7 @@ void physics_body_position_get(Rigidbody body, float * x, float * y, float * z)
void physics_body_position_set(Rigidbody body, float x, float y, float z)
{
dBodySetPosition(body, x, y, z);
dBodyEnable(body);
}
void physics_body_rotation_get(Rigidbody body, float * x, float * y, float * z, float * w)
@ -117,6 +118,7 @@ void physics_body_rotation_set(Rigidbody body, float x, float y, float z, float
rotation[3] = z;
rotation[0] = w;
dBodySetQuaternion(body, &rotation[0]);
dBodyEnable(body);
}

File diff suppressed because it is too large Load Diff

@ -11,6 +11,8 @@
struct Material_Param;
struct Parser_Object;
typedef void (*Collision_CB)(struct Entity* this_entity, struct Entity* other_entity, Rigidbody, Rigidbody);
enum Entity_Type
{
ET_NONE,
@ -41,6 +43,7 @@ struct Transform
int parent;
int* children;
bool is_modified;
bool sync_physics;
};
struct Model
@ -100,6 +103,12 @@ struct Light
float depth_bias;
};
struct Collision
{
Rigidbody rigidbody;
Collision_CB on_collision;
};
struct Entity
{
int id;
@ -108,8 +117,10 @@ struct Entity
bool is_listener; /* TODO: Replace all booleans with flags */
bool marked_for_deletion;
bool renderable;
bool has_collision;
int editor_selected;
struct Transform transform;
struct Collision collision;
union
{
@ -143,5 +154,6 @@ const char* entity_type_name_get(struct Entity* entity);
void entity_apply_sound_params(struct Entity* entity); // Convenience function to sync the data set in entity's sound_source with the actual sound source's instance
void entity_rigidbody_on_move(Rigidbody body);
void entity_rigidbody_on_collision(Rigidbody body_A, Rigidbody body_B);
void entity_rigidbody_set(struct Entity* entity, Rigidbody body);
#endif

@ -48,6 +48,7 @@ static void debug(float dt);
static void debug_gui(float dt);
static void scene_setup(void);
static void on_box_move(Rigidbody body);
static void on_collision_test(struct Entity* this_ent, struct Entity* other_ent, Rigidbody body, Rigidbody body2);
static struct Game_State* game_state = NULL;
struct Platform_Api* platform = NULL;
@ -151,7 +152,7 @@ void scene_setup(void)
// /* platform->sound.source_play(sound_source->source_handle); */
// int parent_node = new_ent->id;
// int num_suz = 10;
// int num_suz = 50;
// srand(time(NULL));
// for(int i = 0; i < num_suz; i++)
// {
@ -254,17 +255,21 @@ void scene_setup(void)
platform->physics.cs_plane_create(0, 1, 0, 0);
Rigidbody box = platform->physics.body_box_create(2.5, 2.5, 2.5);
platform->physics.body_position_set(box, 0.f, 50.f, 0.f);
platform->physics.body_mass_set(box, 10.f);
platform->physics.body_data_set(box, (void*)suz_id);
platform->physics.body_force_add(box, -100.f, 0.f, 0.f);
/*platform->physics.body_position_set(box, 0.f, 50.f, 0.f);
platform->physics.body_mass_set(box, 10.f);*/
/*platform->physics.body_data_set(box, (void*)suz_id);*/
//platform->physics.body_force_add(box, -100.f, 0.f, 0.f);
struct Entity* suz = entity_find("Model_Entity");
entity_rigidbody_set(suz, box);
suz->collision.on_collision = &on_collision_test;
/*Rigidbody plane = platform->physics.plane_create(0, 1, 0, 0);*/
Rigidbody ground_box = platform->physics.body_box_create(10, 10, 10);
platform->physics.body_position_set(ground_box, 0.f, 0.f, 0.f);
platform->physics.body_kinematic_set(ground_box);
struct Entity* ground = entity_find("Ground");
platform->physics.body_data_set(ground_box, (void*)ground->id);
entity_rigidbody_set(ground, ground_box);
/*platform->physics.body_data_set(ground_box, (void*)ground->id);*/
}
@ -390,7 +395,7 @@ void debug(float dt)
/*struct Entity* model = scene_find("Model_Entity");
vec3 x_axis = {0, 1, 0};
transform_rotate(model, &x_axis, 25.f * dt, TS_WORLD);
transform_rotate(model, &x_axis, 50.f * dt, TS_WORLD);
vec3 amount = {0, 0, -5 * dt};
transform_translate(model, &amount, TS_LOCAL);*/
@ -1712,4 +1717,16 @@ void game_cleanup(void)
struct Game_State* game_state_get(void)
{
return game_state;
}
void on_collision_test(struct Entity* this_ent, struct Entity* other_ent, Rigidbody body, Rigidbody body2)
{
float y = this_ent->transform.position.y;
if(y < 10.f)
{
vec3 translation = {0.f, 50.f, 0.f};
transform_translate(this_ent, &translation, TS_WORLD);
//platform->physics.body_force_add(body, 0.f, -100.f, 0.f);
}
//platform->physics.body_force_add(body, 0.f, 500.f, 0.f);
}

@ -207,6 +207,7 @@ void transform_update_transmat(struct Entity* entity)
}
}
transform->is_modified = true;
if(entity->has_collision) entity->transform.sync_physics = true;
}
void transform_destroy(struct Entity* entity)

Loading…
Cancel
Save