Implemented Physics raycasting and getting entity as result from raycast

dev
Shariq Shah 8 years ago
parent b3cecf3221
commit a976a9aa2e
  1. 2
      README.md
  2. 14
      src/common/common.h
  3. 6
      src/common/hashmap.c
  4. 12
      src/game/main.c
  5. 52
      src/game/physics.c
  6. 11
      src/game/physics.h
  7. 2
      src/libsymmetry/bounding_volumes.c
  8. 2
      src/libsymmetry/camera.c
  9. 2
      src/libsymmetry/editor.c
  10. 69
      src/libsymmetry/entity.c
  11. 6
      src/libsymmetry/entity.h
  12. 56
      src/libsymmetry/game.c
  13. 4
      src/libsymmetry/renderer.c
  14. 4
      src/libsymmetry/transform.c
  15. 2
      src/libsymmetry/transform.h

@ -155,7 +155,6 @@
- ## TODO - ## TODO
- Physics raycasting
- Physics forces/torque etc - Physics forces/torque etc
- Fix lights type not being correctly saved/loaded from file - Fix lights type not being correctly saved/loaded from file
- Physics Trimesh support - Physics Trimesh support
@ -365,3 +364,4 @@
* Separated collision shape and rigidbody * Separated collision shape and rigidbody
* Implemented Getting/Modifying primitive physics shapes' values like length, radius etc * Implemented Getting/Modifying primitive physics shapes' values like length, radius etc
* Update physics if entity position/rotation/scale etc are changed * Update physics if entity position/rotation/scale etc are changed
* Implemented Physics raycasting

@ -53,6 +53,12 @@ enum Sound_Attenuation_Type
SA_EXPONENTIAL // Exponential distance attenuation model SA_EXPONENTIAL // Exponential distance attenuation model
}; };
struct Raycast_Hit
{
int entity_id;
float normal_x, normal_y, normal_z;
};
struct Physics_Api struct Physics_Api
{ {
void (*init)(void); void (*init)(void);
@ -62,6 +68,8 @@ struct Physics_Api
void (*gravity_get)(float* x, float* y, float* z); void (*gravity_get)(float* x, float* y, float* z);
void (*cs_remove)(Collision_Shape shape); void (*cs_remove)(Collision_Shape shape);
void (*cs_data_set)(Collision_Shape shape, void* data);
void* (*cs_data_get)(Collision_Shape shape);
Collision_Shape (*cs_plane_create)(float a, float b, float c, float d); Collision_Shape (*cs_plane_create)(float a, float b, float c, float d);
void (*cs_plane_params_set)(Collision_Shape shape, float a, float b, float c, float d); void (*cs_plane_params_set)(Collision_Shape shape, float a, float b, float c, float d);
@ -79,6 +87,12 @@ struct Physics_Api
void (*cs_capsule_params_set)(Collision_Shape shape, float radius, float length); void (*cs_capsule_params_set)(Collision_Shape shape, float radius, float length);
void (*cs_capsule_params_get)(Collision_Shape shape, float* radius, float* length); void (*cs_capsule_params_get)(Collision_Shape shape, float* radius, float* length);
Collision_Shape (*cs_ray_create)(float length, bool first_contact, bool backface_cull);
bool (*cs_ray_cast)(Collision_Shape ray,
struct Raycast_Hit* out_rayhit,
float pos_x, float pos_y, float pos_z,
float dir_x, float dir_y, float dir_z);
void (*body_remove)(Rigidbody body); void (*body_remove)(Rigidbody body);
Rigidbody (*body_box_create)(float length, float width, float height); Rigidbody (*body_box_create)(float length, float width, float height);
Rigidbody (*body_sphere_create)(float radius); Rigidbody (*body_sphere_create)(float radius);

@ -348,13 +348,13 @@ int hashmap_iter_next(struct Hashmap* hashmap, char** key, struct Variant** valu
return 0; return 0;
} }
void hashmap_copy(struct Hashmap* from, struct Hashmap* to) void hashmap_copy(struct Hashmap* from, struct Hashmap* to)
{ {
struct Variant* from_val = NULL; struct Variant* from_val = NULL;
char* from_key = NULL; char* from_key = NULL;
HASHMAP_FOREACH(from, from_key, from_val) HASHMAP_FOREACH(from, from_key, from_val)
{ {
hashmap_value_set(to, from_key, from_val); hashmap_value_set(to, from_key, from_val);
} }
} }

@ -152,14 +152,18 @@ int main(int argc, char** args)
.cs_sphere_create = &physics_cs_sphere_create, .cs_sphere_create = &physics_cs_sphere_create,
.cs_capsule_create = &physics_cs_capsule_create, .cs_capsule_create = &physics_cs_capsule_create,
.cs_remove = &physics_cs_remove, .cs_remove = &physics_cs_remove,
.cs_data_set = &physics_cs_data_set,
.cs_data_get = &physics_cs_data_get,
.cs_plane_params_get = &physics_cs_plane_params_get, .cs_plane_params_get = &physics_cs_plane_params_get,
.cs_capsule_params_get = &physics_cs_capsule_params_get, .cs_capsule_params_get = &physics_cs_capsule_params_get,
.cs_box_params_get = &physics_cs_box_params_get, .cs_box_params_get = &physics_cs_box_params_get,
.cs_sphere_radius_get = &physics_cs_sphere_radius_get, .cs_sphere_radius_get = &physics_cs_sphere_radius_get,
.cs_plane_params_set = &physics_cs_plane_params_set, .cs_plane_params_set = &physics_cs_plane_params_set,
.cs_capsule_params_set = &physics_cs_capsule_params_set, .cs_capsule_params_set = &physics_cs_capsule_params_set,
.cs_box_params_set = &physics_cs_box_params_set, .cs_box_params_set = &physics_cs_box_params_set,
.cs_shpere_radius_set = &physics_cs_sphere_radius_set .cs_shpere_radius_set = &physics_cs_sphere_radius_set,
.cs_ray_create = &physics_cs_ray_create,
.cs_ray_cast = &physics_cs_ray_cast
} }
}; };

@ -16,6 +16,7 @@ static struct
Physics; Physics;
static void physics_near_callback(void* data, dGeomID body1, dGeomID body2); static void physics_near_callback(void* data, dGeomID body1, dGeomID body2);
static void physics_cs_ray_hit_callback(void* data, dGeomID geom1, dGeomID geom2);
void physics_init(void) void physics_init(void)
{ {
@ -399,6 +400,45 @@ void physics_body_kinematic_set(Rigidbody body)
dBodySetKinematic(body); dBodySetKinematic(body);
} }
void physics_cs_ray_hit_callback(void* data, dGeomID geom1, dGeomID geom2)
{
dContact contact;
if(dCollide(geom1, geom2, 1, &contact.geom, sizeof(contact)) == 1)
{
struct Raycast_Hit* rayhit = (struct Raycast_Hit*)data;
Rigidbody body = dGeomGetBody(geom2);
if(body)
{
int entity_id = (int)dBodyGetData(body);
if(entity_id != 0) rayhit->entity_id = entity_id;
}
else
{
int entity_id = (int)dGeomGetData(geom2);
if(entity_id != 0) rayhit->entity_id = entity_id;
}
}
}
bool physics_cs_ray_cast(Collision_Shape ray,
struct Raycast_Hit* out_rayhit,
float pos_x, float pos_y, float pos_z,
float dir_x, float dir_y, float dir_z)
{
assert(out_rayhit);
out_rayhit->entity_id = -1;
dGeomRaySet(ray, pos_x, pos_y, pos_z, dir_x, dir_y, dir_z);
dSpaceCollide2(ray, Physics.space, (void*)out_rayhit, &physics_cs_ray_hit_callback);
return out_rayhit->entity_id == -1 ? false : true;
}
Collision_Shape physics_cs_ray_create(float length, bool first_contact, bool backface_cull)
{
dGeomID ray = dCreateRay(Physics.space, length);
dGeomRaySetParams(ray, first_contact, backface_cull);
return ray;
}
void physics_body_remove(Rigidbody body) void physics_body_remove(Rigidbody body)
{ {
dGeomID shape = dBodyGetFirstGeom(body); dGeomID shape = dBodyGetFirstGeom(body);
@ -411,3 +451,15 @@ void physics_cs_remove(Collision_Shape shape)
{ {
dGeomDestroy(shape); dGeomDestroy(shape);
} }
void physics_cs_data_set(Collision_Shape shape, void* data)
{
assert(shape);
dGeomSetData(shape, data);
}
void* physics_cs_data_get(Collision_Shape shape)
{
assert(shape);
return dGeomGetData(shape);
}

@ -36,6 +36,15 @@ Collision_Shape physics_cs_capsule_create(float radius, float height);
void physics_cs_capsule_params_get(Collision_Shape shape, float* radius, float* height); void physics_cs_capsule_params_get(Collision_Shape shape, float* radius, float* height);
void physics_cs_capsule_params_set(Collision_Shape shape, float radius, float height); void physics_cs_capsule_params_set(Collision_Shape shape, float radius, float height);
void physics_cs_data_set(Collision_Shape shape, void* data);
void* physics_cs_data_get(Collision_Shape shape);
Collision_Shape physics_cs_ray_create(float length, bool first_contact, bool backface_cull);
bool physics_cs_ray_cast(Collision_Shape ray,
struct Raycast_Hit* out_rayhit,
float pos_x, float pos_y, float pos_z,
float dir_x, float dir_y, float dir_z);
Rigidbody physics_body_box_create(float x, float y, float z); Rigidbody physics_body_box_create(float x, float y, float z);
Rigidbody physics_body_sphere_create(float radius); Rigidbody physics_body_sphere_create(float radius);
Rigidbody physics_body_capsule_create(float radius, float height); Rigidbody physics_body_capsule_create(float radius, float height);
@ -55,4 +64,6 @@ void physics_body_force_add(Rigidbody body, float fx, float fy, float fz);
void physics_body_kinematic_set(Rigidbody body); void physics_body_kinematic_set(Rigidbody body);
#endif #endif

@ -46,7 +46,7 @@ int bv_intersect_frustum_sphere(vec4* frustum, struct Bounding_Sphere* sphere, s
vec3_fill(&abs_pos, 0.f, 0.f, 0.f); vec3_fill(&abs_pos, 0.f, 0.f, 0.f);
vec3_fill(&abs_scale, 0.f, 0.f, 0.f); vec3_fill(&abs_scale, 0.f, 0.f, 0.f);
transform_get_absolute_pos(entity, &abs_pos); transform_get_absolute_position(entity, &abs_pos);
transform_get_absolute_scale(entity, &abs_scale); transform_get_absolute_scale(entity, &abs_scale);
float max_scale_dimension = fabsf(abs_scale.x); float max_scale_dimension = fabsf(abs_scale.x);
if(fabsf(abs_scale.y) > max_scale_dimension) max_scale_dimension = fabsf(abs_scale.y); if(fabsf(abs_scale.y) > max_scale_dimension) max_scale_dimension = fabsf(abs_scale.y);

@ -76,7 +76,7 @@ void camera_update_view(struct Entity* entity)
vec3 position = {0.f, 0.f, 0.f}; vec3 position = {0.f, 0.f, 0.f};
transform_get_absolute_lookat(entity, &lookat); transform_get_absolute_lookat(entity, &lookat);
transform_get_absolute_up(entity, &up); transform_get_absolute_up(entity, &up);
transform_get_absolute_pos(entity, &position); transform_get_absolute_position(entity, &position);
mat4_lookat(&camera->view_mat, &position, &lookat, &up); mat4_lookat(&camera->view_mat, &position, &lookat, &up);
camera_update_view_proj(entity); camera_update_view_proj(entity);
} }

@ -284,7 +284,7 @@ void editor_update(float dt)
{ {
nk_layout_row_dynamic(context, row_height, 1); nk_label(context, "Position", NK_TEXT_ALIGN_CENTERED); nk_layout_row_dynamic(context, row_height, 1); nk_label(context, "Position", NK_TEXT_ALIGN_CENTERED);
vec3 abs_pos = {0.f, 0.f, 0.f}; vec3 abs_pos = {0.f, 0.f, 0.f};
transform_get_absolute_pos(entity, &abs_pos); transform_get_absolute_position(entity, &abs_pos);
if(editor_widget_v3(context, &abs_pos, "Px", "Py", "Pz", -FLT_MAX, FLT_MAX, 5.f, 1.f, row_height)) transform_set_position(entity, &abs_pos); if(editor_widget_v3(context, &abs_pos, "Px", "Py", "Pz", -FLT_MAX, FLT_MAX, 5.f, 1.f, row_height)) transform_set_position(entity, &abs_pos);
nk_layout_row_dynamic(context, row_height, 1); nk_label(context, "Rotation", NK_TEXT_ALIGN_CENTERED); nk_layout_row_dynamic(context, row_height, 1); nk_label(context, "Rotation", NK_TEXT_ALIGN_CENTERED);

@ -68,7 +68,10 @@ void entity_remove(int index)
if(entity->has_collision) if(entity->has_collision)
{ {
platform->physics.body_remove(entity->collision.rigidbody); if(entity->collision.rigidbody)
platform->physics.body_remove(entity->collision.rigidbody);
else
platform->physics.cs_remove(entity->collision.collision_shape);
entity->has_collision = false; entity->has_collision = false;
entity->collision.rigidbody = NULL; entity->collision.rigidbody = NULL;
entity->collision.on_collision = NULL; entity->collision.on_collision = NULL;
@ -176,7 +179,7 @@ void entity_post_update(void)
vec3 abs_pos = {0.f, 0.f, 0.f}; vec3 abs_pos = {0.f, 0.f, 0.f};
vec3 abs_fwd = {0.f, 0.f, -1.f}; vec3 abs_fwd = {0.f, 0.f, -1.f};
vec3 abs_up = {0.f, 1.f, 0.f}; vec3 abs_up = {0.f, 1.f, 0.f};
transform_get_absolute_pos(entity, &abs_pos); transform_get_absolute_position(entity, &abs_pos);
transform_get_absolute_forward(entity, &abs_fwd); transform_get_absolute_forward(entity, &abs_fwd);
transform_get_absolute_up(entity, &abs_up); transform_get_absolute_up(entity, &abs_up);
@ -193,11 +196,13 @@ void entity_post_update(void)
if(entity->has_collision && entity->transform.sync_physics) if(entity->has_collision && entity->transform.sync_physics)
{ {
assert(entity->collision.rigidbody); if(entity->collision.rigidbody)
quat abs_rot = { 0.f, 0.f, 0.f, 1.f }; {
transform_get_absolute_rot(entity, &abs_rot); quat abs_rot = { 0.f, 0.f, 0.f, 1.f };
platform->physics.body_rotation_set(entity->collision.rigidbody, abs_rot.x, abs_rot.y, abs_rot.z, abs_rot.w); transform_get_absolute_rot(entity, &abs_rot);
platform->physics.body_position_set(entity->collision.rigidbody, abs_pos.x, abs_pos.y, abs_pos.z); platform->physics.body_rotation_set(entity->collision.rigidbody, abs_rot.x, abs_rot.y, abs_rot.z, abs_rot.w);
platform->physics.body_position_set(entity->collision.rigidbody, abs_pos.x, abs_pos.y, abs_pos.z);
}
entity->transform.sync_physics = false; entity->transform.sync_physics = false;
} }
} }
@ -500,7 +505,7 @@ struct Entity* entity_read(struct Parser_Object* object)
vec3 abs_pos = {0.f, 0.f, 0.f}; vec3 abs_pos = {0.f, 0.f, 0.f};
vec3 abs_fwd = {0.f, 0.f, -1.f}; vec3 abs_fwd = {0.f, 0.f, -1.f};
vec3 abs_up = {0.f, 1.f, 0.f}; vec3 abs_up = {0.f, 1.f, 0.f};
transform_get_absolute_pos(entity, &abs_pos); transform_get_absolute_position(entity, &abs_pos);
transform_get_absolute_forward(entity, &abs_fwd); transform_get_absolute_forward(entity, &abs_fwd);
transform_get_absolute_up(entity, &abs_up); transform_get_absolute_up(entity, &abs_up);
platform->sound.source_instance_update_position(entity->sound_source.source_instance, abs_pos.x, abs_pos.y, abs_pos.z); platform->sound.source_instance_update_position(entity->sound_source.source_instance, abs_pos.x, abs_pos.y, abs_pos.z);
@ -555,7 +560,7 @@ struct Entity* entity_read(struct Parser_Object* object)
vec3 abs_pos = {0.f, 0.f, 0.f}; vec3 abs_pos = {0.f, 0.f, 0.f};
vec3 abs_fwd = {0.f, 0.f, -1.f}; vec3 abs_fwd = {0.f, 0.f, -1.f};
vec3 abs_up = {0.f, 1.f, 0.f}; vec3 abs_up = {0.f, 1.f, 0.f};
transform_get_absolute_pos(entity, &abs_pos); transform_get_absolute_position(entity, &abs_pos);
transform_get_absolute_forward(entity, &abs_fwd); transform_get_absolute_forward(entity, &abs_fwd);
transform_get_absolute_up(entity, &abs_up); transform_get_absolute_up(entity, &abs_up);
@ -659,7 +664,7 @@ void entity_apply_sound_params(struct Entity* entity)
vec3 abs_pos = {0.f, 0.f, 0.f}; vec3 abs_pos = {0.f, 0.f, 0.f};
vec3 abs_fwd = {0.f, 0.f, -1.f}; vec3 abs_fwd = {0.f, 0.f, -1.f};
vec3 abs_up = {0.f, 1.f, 0.f}; vec3 abs_up = {0.f, 1.f, 0.f};
transform_get_absolute_pos(entity, &abs_pos); transform_get_absolute_position(entity, &abs_pos);
transform_get_absolute_forward(entity, &abs_fwd); transform_get_absolute_forward(entity, &abs_fwd);
transform_get_absolute_up(entity, &abs_up); transform_get_absolute_up(entity, &abs_up);
platform->sound.source_instance_update_position(entity->sound_source.source_instance, abs_pos.x, abs_pos.y, abs_pos.z); platform->sound.source_instance_update_position(entity->sound_source.source_instance, abs_pos.x, abs_pos.y, abs_pos.z);
@ -718,7 +723,7 @@ void entity_rigidbody_on_collision(Rigidbody body_A, Rigidbody body_B)
if(ent_A && ent_B) if(ent_A && ent_B)
{ {
log_message("Entity %s collided with Entity %s", ent_A->name, ent_B->name); //log_message("Entity %s collided with Entity %s", ent_A->name, ent_B->name);
} }
} }
@ -727,21 +732,53 @@ void entity_rigidbody_set(struct Entity * entity, Rigidbody body)
assert(entity && body); assert(entity && body);
//Remove previous rigidbody if there is any //Remove previous rigidbody if there is any
if(entity->has_collision && entity->collision.rigidbody) if(entity->has_collision && (entity->collision.rigidbody || entity->collision.collision_shape))
{ {
platform->physics.body_remove(entity->collision.rigidbody); if(entity->collision.rigidbody)
{
platform->physics.body_remove(entity->collision.rigidbody);
}
else if(entity->collision.collision_shape)
{
platform->physics.cs_remove(entity->collision.collision_shape);
}
entity->collision.rigidbody = NULL; entity->collision.rigidbody = NULL;
entity->collision.collision_shape = NULL;
} }
entity->has_collision = true; entity->has_collision = true;
entity->collision.rigidbody = body; entity->collision.rigidbody = body;
entity->collision.collision_shape = platform->physics.body_cs_get(body);
vec3 abs_pos = {0.f, 0.f, 0.f}; vec3 abs_pos = {0.f, 0.f, 0.f};
quat abs_rot = {0.f, 0.f, 0.f, 1.f}; quat abs_rot = {0.f, 0.f, 0.f, 1.f};
transform_get_absolute_pos(entity, &abs_pos); transform_get_absolute_position(entity, &abs_pos);
transform_get_absolute_rot(entity, &abs_rot); transform_get_absolute_rot(entity, &abs_rot);
platform->physics.body_rotation_set(body, abs_rot.x, abs_rot.y, abs_rot.z, abs_rot.w); platform->physics.body_rotation_set(body, abs_rot.x, abs_rot.y, abs_rot.z, abs_rot.w);
platform->physics.body_position_set(body, abs_pos.x, abs_pos.y, abs_pos.z); platform->physics.body_position_set(body, abs_pos.x, abs_pos.y, abs_pos.z);
platform->physics.body_data_set(body, (void*)entity->id); platform->physics.body_data_set(body, (void*)entity->id);
} }
void entity_collision_shape_set(struct Entity* entity, Collision_Shape shape)
{
assert(entity && shape);
if(entity->has_collision && (entity->collision.rigidbody || entity->collision.collision_shape))
{
if(entity->collision.rigidbody)
{
platform->physics.body_remove(entity->collision.rigidbody);
}
else if(entity->collision.collision_shape)
{
platform->physics.cs_remove(entity->collision.collision_shape);
}
entity->collision.rigidbody = NULL;
entity->collision.collision_shape = NULL;
}
entity->has_collision = true;
entity->collision.collision_shape = shape;
platform->physics.cs_data_set(shape, (void*)entity->id);
}

@ -105,8 +105,9 @@ struct Light
struct Collision struct Collision
{ {
Rigidbody rigidbody; Rigidbody rigidbody;
Collision_CB on_collision; Collision_Shape collision_shape;
Collision_CB on_collision;
}; };
struct Entity struct Entity
@ -155,5 +156,6 @@ void entity_apply_sound_params(struct Entity* entity); // Convenience
void entity_rigidbody_on_move(Rigidbody body); void entity_rigidbody_on_move(Rigidbody body);
void entity_rigidbody_on_collision(Rigidbody body_A, Rigidbody body_B); void entity_rigidbody_on_collision(Rigidbody body_A, Rigidbody body_B);
void entity_rigidbody_set(struct Entity* entity, Rigidbody body); void entity_rigidbody_set(struct Entity* entity, Rigidbody body);
void entity_collision_shape_set(struct Entity* entity, Collision_Shape shape); // Only used for collision shapes like plane which can't have a rigidbody attached to collision shape
#endif #endif

@ -263,12 +263,13 @@ void scene_setup(void)
entity_rigidbody_set(suz, box); entity_rigidbody_set(suz, box);
suz->collision.on_collision = &on_collision_test; suz->collision.on_collision = &on_collision_test;
/*Rigidbody plane = platform->physics.plane_create(0, 1, 0, 0);*/ Collision_Shape plane = platform->physics.cs_plane_create(0, 1, 0, 0);
Rigidbody ground_box = platform->physics.body_box_create(10, 10, 10); //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_position_set(ground_box, 0.f, 0.f, 0.f);
platform->physics.body_kinematic_set(ground_box); platform->physics.body_kinematic_set(ground_box);*/
struct Entity* ground = entity_find("Ground"); struct Entity* ground = entity_find("Ground");
entity_rigidbody_set(ground, ground_box); entity_collision_shape_set(ground, plane);
//entity_rigidbody_set(ground, ground_box);
/*platform->physics.body_data_set(ground_box, (void*)ground->id);*/ /*platform->physics.body_data_set(ground_box, (void*)ground->id);*/
} }
@ -276,7 +277,7 @@ void scene_setup(void)
void debug(float dt) void debug(float dt)
{ {
//struct Entity* entity = entity_get(player_node); //struct Entity* entity = entity_get(player_node);
struct Entity* entity = entity_get(game_state->player_node); struct Entity* player_entity = entity_get(game_state->player_node);
float move_speed = 5.f, move_scale = 3.f, turn_speed = 50.f; float move_speed = 5.f, move_scale = 3.f, turn_speed = 50.f;
vec3 offset = {0, 0, 0}; vec3 offset = {0, 0, 0};
float turn_up_down = 0.f; float turn_up_down = 0.f;
@ -329,25 +330,25 @@ void debug(float dt)
if(turn_left_right != 0.f) if(turn_left_right != 0.f)
{ {
transform_rotate(entity, &rot_axis_left_right, -turn_left_right, TS_WORLD); transform_rotate(player_entity, &rot_axis_left_right, -turn_left_right, TS_WORLD);
vec3 up = {0.f, 0.f, 0.f}; vec3 up = {0.f, 0.f, 0.f};
vec3 forward = {0.f, 0.f, 0.f}; vec3 forward = {0.f, 0.f, 0.f};
vec3 lookat = {0.f, 0.f, 0.f}; vec3 lookat = {0.f, 0.f, 0.f};
transform_get_up(entity, &up); transform_get_up(player_entity, &up);
transform_get_forward(entity, &forward); transform_get_forward(player_entity, &forward);
transform_get_lookat(entity, &lookat); transform_get_lookat(player_entity, &lookat);
/* log_message("Up : %s", tostr_vec3(&up)); */ /* log_message("Up : %s", tostr_vec3(&up)); */
/* log_message("FR : %s", tostr_vec3(&forward)); */ /* log_message("FR : %s", tostr_vec3(&forward)); */
} }
if(turn_up_down != 0.f) if(turn_up_down != 0.f)
{ {
transform_rotate(entity, &rot_axis_up_down, turn_up_down, TS_LOCAL); transform_rotate(player_entity, &rot_axis_up_down, turn_up_down, TS_LOCAL);
vec3 up = {0.f, 0.f, 0.f}; vec3 up = {0.f, 0.f, 0.f};
vec3 forward = {0.f, 0.f, 0.f}; vec3 forward = {0.f, 0.f, 0.f};
vec3 lookat = {0.f, 0.f, 0.f}; vec3 lookat = {0.f, 0.f, 0.f};
transform_get_up(entity, &up); transform_get_up(player_entity, &up);
transform_get_forward(entity, &forward); transform_get_forward(player_entity, &forward);
transform_get_lookat(entity, &lookat); transform_get_lookat(player_entity, &lookat);
/* log_message("Up : %s", tostr_vec3(&up)); */ /* log_message("Up : %s", tostr_vec3(&up)); */
/* log_message("FR : %s", tostr_vec3(&forward)); */ /* log_message("FR : %s", tostr_vec3(&forward)); */
} }
@ -364,7 +365,7 @@ void debug(float dt)
vec3_scale(&offset, &offset, dt); vec3_scale(&offset, &offset, dt);
if(offset.x != 0 || offset.y != 0 || offset.z != 0) if(offset.x != 0 || offset.y != 0 || offset.z != 0)
{ {
transform_translate(entity, &offset, TS_LOCAL); transform_translate(player_entity, &offset, TS_LOCAL);
//log_message("Position : %s", tostr_vec3(&transform->position)); //log_message("Position : %s", tostr_vec3(&transform->position));
} }
@ -428,6 +429,27 @@ void debug(float dt)
sprite_batch_add_sprite(batch, &sprite); sprite_batch_add_sprite(batch, &sprite);
sprite_batch_end(batch); sprite_batch_end(batch);
//Raycast test
if(input_is_key_pressed(KEY_R))
{
Collision_Shape ray = platform->physics.cs_ray_create(20.f, true, true);
vec3 position = {0.f, 0.f, 0.f};
vec3 direction = {0.f, 0.f, 0.f};
transform_get_absolute_forward(player_entity, &direction);
transform_get_absolute_position(player_entity, &position);
struct Raycast_Hit hit;
if(platform->physics.cs_ray_cast(ray, &hit, position.x, position.y, position.z, direction.x, direction.y, direction.z))
{
struct Entity* entity_hit = entity_get(hit.entity_id);
log_message("Ray hit %s", entity_hit->name);
}
else
{
log_message("Ray didn't hit anything!");
}
platform->physics.cs_remove(ray);
}
} }
bool run(void) bool run(void)
@ -1724,8 +1746,8 @@ void on_collision_test(struct Entity* this_ent, struct Entity* other_ent, Rigidb
float y = this_ent->transform.position.y; float y = this_ent->transform.position.y;
if(y < 10.f) if(y < 10.f)
{ {
vec3 translation = {0.f, 50.f, 0.f}; //vec3 translation = {0.f, 50.f, 0.f};
transform_translate(this_ent, &translation, TS_WORLD); //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, -100.f, 0.f);
} }
//platform->physics.body_force_add(body, 0.f, 500.f, 0.f); //platform->physics.body_force_add(body, 0.f, 500.f, 0.f);

@ -179,7 +179,7 @@ void renderer_draw(struct Entity* active_viewer)
struct Entity* light_entity = entity_get(light_index_list[i]); struct Entity* light_entity = entity_get(light_index_list[i]);
struct Light* light = &light_entity->light; /* TODO: Cull lights according to camera frustum */ struct Light* light = &light_entity->light; /* TODO: Cull lights according to camera frustum */
vec3 light_pos = {0, 0, 0}; vec3 light_pos = {0, 0, 0};
transform_get_absolute_pos(light_entity, &light_pos); transform_get_absolute_position(light_entity, &light_pos);
if(light->type != LT_POINT) if(light->type != LT_POINT)
{ {
@ -229,7 +229,7 @@ void renderer_draw(struct Entity* active_viewer)
shader_set_uniform_int(material->shader, "total_active_lights", valid_light_count); shader_set_uniform_int(material->shader, "total_active_lights", valid_light_count);
vec3 camera_pos = {0, 0, 0}; vec3 camera_pos = {0, 0, 0};
transform_get_absolute_pos(viewer, &camera_pos); transform_get_absolute_position(viewer, &camera_pos);
shader_set_uniform_vec3(material->shader, "camera_pos", &camera_pos); shader_set_uniform_vec3(material->shader, "camera_pos", &camera_pos);
} }

@ -137,7 +137,7 @@ void transform_get_absolute_forward(struct Entity* entity, vec3* res)
void transform_get_absolute_lookat(struct Entity* entity, vec3* res) void transform_get_absolute_lookat(struct Entity* entity, vec3* res)
{ {
vec3 abs_position = {0.f, 0.f, 0.f}; vec3 abs_position = {0.f, 0.f, 0.f};
transform_get_absolute_pos(entity, &abs_position); transform_get_absolute_position(entity, &abs_position);
transform_get_absolute_forward(entity, res); transform_get_absolute_forward(entity, res);
vec3_add(res, &abs_position, res); vec3_add(res, &abs_position, res);
} }
@ -240,7 +240,7 @@ void transform_set_position(struct Entity* entity, vec3* new_position)
transform_update_transmat(entity); transform_update_transmat(entity);
} }
void transform_get_absolute_pos(struct Entity* entity, vec3* res) void transform_get_absolute_position(struct Entity* entity, vec3* res)
{ {
vec3_assign(res, &entity->transform.position); vec3_assign(res, &entity->transform.position);
bool done = false; bool done = false;

@ -22,7 +22,7 @@ void transform_get_lookat(struct Entity* entity, vec3* res);
void transform_get_up(struct Entity* entity, vec3* res); void transform_get_up(struct Entity* entity, vec3* res);
void transform_get_right(struct Entity* entity, vec3* res); void transform_get_right(struct Entity* entity, vec3* res);
void transform_update_transmat(struct Entity* entity); void transform_update_transmat(struct Entity* entity);
void transform_get_absolute_pos(struct Entity* entity, vec3* res); void transform_get_absolute_position(struct Entity* entity, vec3* res);
void transform_get_absolute_rot(struct Entity* entity, quat* res); void transform_get_absolute_rot(struct Entity* entity, quat* res);
void transform_get_absolute_scale(struct Entity* entity, vec3* res); void transform_get_absolute_scale(struct Entity* entity, vec3* res);
void transform_get_absolute_lookat(struct Entity* entity, vec3* res); void transform_get_absolute_lookat(struct Entity* entity, vec3* res);

Loading…
Cancel
Save