These are the functions that create the physics bodies. add_poly_entity_to_space() is the function with the problem currently even though I am positioning the body before creating it's shape.
Code: Select all
void * add_poly_entity_to_space(Sandbox * sandbox, bool static_body, cpFloat mass, int vertex_number, cpVect * vertices, cpVect offset, Entity_properties * properties){
cpBody * body = NULL;
cpShape * shape = NULL;
cpFloat moment = cpMomentForPoly(mass, vertex_number, vertices, offset);
if(!static_body){
body = cpSpaceAddBody(sandbox->space, cpBodyNew(mass, moment));
shape = cpSpaceAddShape(sandbox->space, cpPolyShapeNew(body, vertex_number, vertices, offset));
set_dynamic_entity_properties(shape, body, properties);
Entity * entity = init_entity(body, shape);
add_entity(sandbox->entity_list, entity);
return entity;
}
else if(static_body){
body = cpBodyNewStatic();
cpBodySetPos(body, properties->position);
shape = cpSpaceAddShape(sandbox->space, cpPolyShapeNew(body, vertex_number, vertices, offset));
set_generic_entity_properties(shape, properties);
Outline * outline = init_outline(body, shape);
add_outline(sandbox->level_outline, outline);
return outline;
}
return NULL;
}
void set_generic_entity_properties(cpShape * shape, Entity_properties * properties){
cpShapeSetSensor(shape, properties->sensor); /* is the shape a sensor or not? */
cpShapeSetElasticity(shape, properties->elasticity); /* set the elasticity of the shape, [0, 1] */
cpShapeSetFriction(shape, properties->friction); /* set the friction of the shape, [0, 1] */
cpShapeSetGroup(shape,properties->group); /* set the group of the shape */
cpShapeSetLayers(shape, properties->layers); /* set the layers of the shape, default is CP_ALL_LAYERS */
}
void set_dynamic_entity_properties(cpShape * shape, cpBody * body, Entity_properties * properties){
set_generic_entity_properties(shape, properties);
cpBodySetPos(body, properties->position); /* set the position of the body */
}
void build_level(Level * level, Sandbox * sandbox){
fetch_level_trace(level->info->trace_info, sandbox);
fetch_trace_properties(level->info->trace_properties_info, sandbox);
outline_level(sandbox);
//cpSpaceReindexStatic(sandbox->space);
/* Entity_properties * properties = init_entity_properties(false, 0.9f, 0.1f, 1, CP_ALL_LAYERS, cpv(25, 25));
for(size_t idx = 0; idx < sandbox->vertex_set_size; ++ idx){
Entity * add_poly_entity_to_space(Sandbox * sandbox, cpFloat mass, int vertex_number, cpVect * vertices, cpVect offset, Entity_properties * properties)
} */
//Outline * outline = tlist_tail(&sandbox->level_outline->outline_list, list);
Entity_properties * properties = init_entity_properties(false, 0.9f, 0.1f, 2, CP_ALL_LAYERS, cpv(625, 25));
Entity * entity = add_circular_entity_to_space(sandbox, false, 10.0f , 0.0f, 50.0f, cpvzero, properties);
level->player = init_player("Frames/ball.png", entity);
cpBodySetUserData(level->player->entity->body, level->player);
level->info->in_sandbox = true;
//size_t layer_mask = 0;
//set_mask(&layer_mask, LEVEL_LAYER);
//Entity_properties * prop = init_entity_properties(false, 1.0f, 0.4f, LEVEL_GROUP, layer_mask, cpv(640, 316));
//cpBody * body = cpBodyNewStatic();
//cpShape * shape = cpSpaceAddShape(sandbox->space, cpCircleShapeNew(body, 113.0, cpv(640, 316)));
//set_dynamic_entity_properties(shape, body, prop);
}
void outline_level(Sandbox * sandbox){
Trace * trace = NULL;
Trace * next_trace = NULL;
tlist_for_each_safe(&(sandbox->level_trace->trace_list), trace, next_trace, list){
switch(trace->type){
case POLYGON : {
cpVect polygon_centroid = cpCentroidForPoly(trace->data->vertices_data->size, trace->data->vertices_data->vertices);
Entity_properties * polygon_properties = malloc(sizeof(Entity_properties));
*polygon_properties = *sandbox->trace_properties;
polygon_properties->position = polygon_centroid;
printf(" %f, %f \n", polygon_properties->position.x, polygon_properties->elasticity);
add_poly_entity_to_space(sandbox, true, 0.0f , trace->data->vertices_data->size, trace->data->vertices_data->vertices, cpv(0,0), polygon_properties);
printf("poly shape placed at -> [%f, %f]\n", polygon_centroid.x, polygon_centroid.y);
free(polygon_properties);
} break;
case CIRCLE : {
Entity_properties * circle_properties = malloc(sizeof(Entity_properties));
*circle_properties = *sandbox->trace_properties;
circle_properties->position = trace->data->circle_data->center;
printf("radius is -> %zu\n", trace->data->circle_data->radius);
cpFloat diameter = trace->data->circle_data->radius * 2.0f;
add_circular_entity_to_space(sandbox, true, 50.0f, 0.0f, diameter, cpvzero, circle_properties);
printf("circle shape placed at -> [%f, %f]\n", circle_properties->position.x, circle_properties->position.y);
free(circle_properties);
} break;
case LINE_PATH : {
cpVect * a = &trace->data->vertices_data->vertices[0];
cpVect * b = &trace->data->vertices_data->vertices[1];
cpVect midpoint = segment_midpoint(a, b);
Entity_properties * line_properties = malloc(sizeof(Entity_properties));
*line_properties = *sandbox->trace_properties;
line_properties->position = midpoint;
add_segment_entity_to_space(sandbox, true, 0.0f, a, b, 0.0f, line_properties);
free(line_properties);
} break;
}
}
}
void fetch_trace_properties(json_t * trace_properties_JSON, Sandbox * sandbox){
json_t * sensor_JSON = json_object_get(trace_properties_JSON, SENSOR);
json_t * friction_JSON = json_object_get(trace_properties_JSON, FRICTION);
json_t * elasticity_JSON = json_object_get(trace_properties_JSON, ELASTICITY);
json_t * group_JSON = json_object_get(trace_properties_JSON, GROUP);
json_t * layer_JSON = json_object_get(trace_properties_JSON, LAYER);
bool sensor = (bool) json_integer_value(sensor_JSON);
cpFloat friction = (cpFloat) json_real_value(friction_JSON);
cpFloat elasticity = (cpFloat) json_real_value(elasticity_JSON);
cpFloat group = (Collision_layer) json_integer_value(group_JSON);
size_t layer_size = json_array_size(layer_JSON);
Collision_layer * layers = malloc(sizeof(Collision_layer) * layer_size);
for(size_t idx = 0; idx < layer_size; ++idx){
json_t * sub_layer_JSON = json_array_get(layer_JSON, idx);
layers[idx] = (Collision_layer) json_integer_value(sub_layer_JSON);
}
size_t layer_mask = 0;
for(size_t idx = 0; idx < layer_size; ++idx){ set_mask(&layer_mask, layers[idx]); } /* create a bitmask from the layers */
printf("final value of the mask -> %zu\n", layer_mask);
sandbox->trace_properties = init_entity_properties(sensor, elasticity, friction, group, layer_mask, cpv(0,0));
free(layers);
}