bool contact_added_callback( btManifoldPoint &btmanifoldpoint, const btCollisionObject *btcollisionobject0, int part_0, int index_0, const btCollisionObject *btcollisionobject1, int part_1, int index_1 ) { OBJMESH *objmesh0 = ( OBJMESH * )( ( btRigidBody * )btcollisionobject0 )->getUserPointer(); OBJMESH *objmesh1 = ( OBJMESH * )( ( btRigidBody * )btcollisionobject1 )->getUserPointer(); console_print("Object #0: %s\n", objmesh0->name ); console_print("Point #0: %.3f %.3f %.3f\n", btmanifoldpoint.m_positionWorldOnA.x(), btmanifoldpoint.m_positionWorldOnA.y(), btmanifoldpoint.m_positionWorldOnA.z() ); console_print("Object #1: %s\n", objmesh1->name ); console_print("Point #1: %.3f %.3f %.3f\n", btmanifoldpoint.m_positionWorldOnB.x(), btmanifoldpoint.m_positionWorldOnB.y(), btmanifoldpoint.m_positionWorldOnB.z() ); console_print("Normal : %.3f %.3f %.3f\n", btmanifoldpoint.m_normalWorldOnB.x(), btmanifoldpoint.m_normalWorldOnB.y(), btmanifoldpoint.m_normalWorldOnB.z() ); console_print( "%d\n\n", get_milli_time() ); return false; }
/* * btManifoldPoint &btmanifoldpoint: Current contact point info * const btCollisionObject *btcollisionobject0: First collision object * int part_0: Part number of first collision object * int index_0: Index of the first collision object * const btCollisionObject *btcollisionobject1: Second collision object * int part_1: Part number of first collision object * int index_1: Index of the second collision object */ bool contact_added_callback(btManifoldPoint &btmanifoldpoint, const btCollisionObject *btcollisionobject0, int part_0, int index_0, const btCollisionObject *btcollisionobject1, int part_1, int index_1) { /* Remember the user point (setUserPointer) that you set up earlier? * It is now time to retrieve it to check which geometry you are dealing with. * To do this, first extract which rigid body is associated to the current * collision object. Then cast the user pointer back to an OBJMESH pointer. * This way, all the variables and functions of the OBJMESH can be called or * manipulated with the callback. Very convenient! */ OBJMESH *objmesh0 = (OBJMESH *)((btRigidBody *)btcollisionobject0)->getUserPointer(); OBJMESH *objmesh1 = (OBJMESH *)((btRigidBody *)btcollisionobject1)->getUserPointer(); /* Print the name of the first mesh. */ console_print("Object #0: %s\n", objmesh0->name); /* Print the XYZ location on the mesh where the contact point is added. */ console_print("Point #0: %.3f %.3f %.3f\n", btmanifoldpoint.m_positionWorldOnA.x(), btmanifoldpoint.m_positionWorldOnA.y(), btmanifoldpoint.m_positionWorldOnA.z()); /* Same as above for the second mesh. */ console_print("Object #1: %s\n", objmesh1->name); console_print("Point #1: %.3f %.3f %.3f\n", btmanifoldpoint.m_positionWorldOnB.x(), btmanifoldpoint.m_positionWorldOnB.y(), btmanifoldpoint.m_positionWorldOnB.z()); /* Print the normal vector at the current location of the contact point on * the second geometry. */ console_print("Normal : %.3f %.3f %.3f\n", btmanifoldpoint.m_normalWorldOnB.x(), btmanifoldpoint.m_normalWorldOnB.y(), btmanifoldpoint.m_normalWorldOnB.z()); console_print("%d\n\n", get_milli_time()); /* Return true only if you change any variables of the contact point * such as the friction). */ return false; }
void templateAppInit( int width, int height ) { atexit( templateAppExit ); GFX_start(); AUDIO_start(); thread = THREAD_create( decompress_stream, NULL, THREAD_PRIORITY_NORMAL, 1 ); glViewport( 0.0f, 0.0f, width, height ); glGetIntegerv( GL_VIEWPORT, viewport_matrix ); srandom( get_milli_time() ); load_level(); }
void templateAppDraw( void ) { glClearColor( 0.5f, 0.5f, 0.5f, 1.0f ); glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); GFX_set_matrix_mode( PROJECTION_MATRIX ); GFX_load_identity(); GFX_set_perspective( 80.0f, ( float )viewport_matrix[ 2 ] / ( float )viewport_matrix[ 3 ], 1.0f, 1000.0f, -90.0f ); if( game_over == 2 ) { templateAppExit(); load_game(); game_over = 0; } GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); if( view_delta.x || view_delta.y ) { if( view_delta.y ) next_rotz -= view_delta.y; if( view_delta.x ) { next_rotx -= view_delta.x; next_rotx = CLAMP( next_rotx, 0.0f, 90.0f ); } view_delta.x = view_delta.y = 0.0f; } rotx = rotx * 0.9f + next_rotx * 0.1f; rotz = rotz * 0.9f + next_rotz * 0.1f; eye.x = center.x + distance * cosf( rotx * DEG_TO_RAD ) * sinf( rotz * DEG_TO_RAD ); eye.y = center.y - distance * cosf( rotx * DEG_TO_RAD ) * cosf( rotz * DEG_TO_RAD ); eye.z = center.z + distance * sinf( rotx * DEG_TO_RAD ); rotx = rotx * 0.9f + next_rotx * 0.1f; rotz = rotz * 0.9f + next_rotz * 0.1f; center.x = maze->location.x; center.y = maze->location.y; center.z = maze->location.z; GFX_look_at( &eye, ¢er, &up ); if( double_tap ) { vec3 location; if( GFX_unproject( view_location.x, viewport_matrix[ 3 ] - view_location.y, 1.0f, GFX_get_modelview_matrix(), GFX_get_projection_matrix(), viewport_matrix, &location.x, &location.y, &location.z ) ) { btVector3 ray_from( eye.x, eye.y, eye.z ), ray_to( location.x + eye.x, location.y + eye.y, location.z + eye.z ); btCollisionWorld::ClosestRayResultCallback collision_ray( ray_from, ray_to ); dynamicsworld->rayTest( ray_from, ray_to, collision_ray ); if( collision_ray.hasHit() && collision_ray.m_collisionObject == maze->btrigidbody ) { collision_ray.m_hitNormalWorld.normalize(); if( collision_ray.m_hitNormalWorld.z() == 1.0f ) { navigationpath_player.start_location.x = player->location.x; navigationpath_player.start_location.y = player->location.y; navigationpath_player.start_location.z = player->location.z; navigationpath_player.end_location.x = collision_ray.m_hitPointWorld.x(); navigationpath_player.end_location.y = collision_ray.m_hitPointWorld.y(); navigationpath_player.end_location.z = collision_ray.m_hitPointWorld.z(); if( NAVIGATION_get_path( navigation, &navigationpath_player, &navigationpathdata_player ) ) { player_next_point = 1; unsigned int i = 0; while( i != navigationpathdata_player.path_point_count + 1 ) { console_print( "%d: %f %f %f\n", i, navigationpathdata_player.path_point_array[ i ].x, navigationpathdata_player.path_point_array[ i ].y, navigationpathdata_player.path_point_array[ i ].z ); ++i; } printf( "\n" ); } } } } double_tap = 0; } if( navigationpathdata_player.path_point_count ) { vec3 color = { 0.0f, 0.0f, 1.0f }; draw_navigation_points( &navigationpathdata_player, &color ); move_entity( player, &navigationpathdata_player, &player_next_point, 3.0f ); } static unsigned int start_time = get_milli_time(); if( get_milli_time() - start_time > 1000 ) { navigationpath_enemy.start_location.x = enemy->location.x; navigationpath_enemy.start_location.y = enemy->location.y; navigationpath_enemy.start_location.z = enemy->location.z; navigationpath_enemy.end_location.x = player->location.x; navigationpath_enemy.end_location.y = player->location.y; navigationpath_enemy.end_location.z = player->location.z; NAVIGATION_get_path( navigation, &navigationpath_enemy, &navigationpathdata_enemy ); enemy_next_point = 1; start_time = get_milli_time(); } if( navigationpathdata_enemy.path_point_count ) { vec3 color = { 1.0f, 0.0f, 0.0f }; move_entity( enemy, &navigationpathdata_enemy, &enemy_next_point, 4.0f ); draw_navigation_points( &navigationpathdata_enemy, &color ); } PROGRAM_draw( program ); glUniform1i( PROGRAM_get_uniform_location( program, ( char * )"DIFFUSE" ), 1 ); unsigned int i = 0; while( i != obj->n_objmesh ) { OBJMESH *objmesh = &obj->objmesh[ i ]; GFX_push_matrix(); mat4 mat; objmesh->btrigidbody->getWorldTransform().getOpenGLMatrix( ( float * )&mat ); memcpy( &objmesh->location, ( vec3 * )&mat.m[ 3 ], sizeof( vec3 ) ); GFX_multiply_matrix( &mat ); glUniformMatrix4fv( PROGRAM_get_uniform_location( program, ( char * )"MODELVIEWPROJECTIONMATRIX" ), 1, GL_FALSE, ( float * )GFX_get_modelview_projection_matrix() ); OBJ_draw_mesh( obj, i ); GFX_pop_matrix(); ++i; } if( !game_over ) dynamicsworld->stepSimulation( 1.0f / 60.0f ); else { GFX_set_matrix_mode( PROJECTION_MATRIX ); GFX_load_identity(); float half_width = ( float )viewport_matrix[ 2 ] * 0.5f, half_height = ( float )viewport_matrix[ 3 ] * 0.5f; GFX_set_orthographic_2d( -half_width, half_width, -half_height, half_height ); GFX_rotate( -90.0f, 0.0f, 0.0f, 1.0f ); GFX_translate( -half_height, -half_width, 0.0f ); GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); vec4 color = { 0.0f, 0.0f, 0.0f, 1.0f }; char msg[ MAX_CHAR ] = {"GAME OVER!"}; if( !font ) { font = FONT_init( ( char * )"foo.ttf" ); FONT_load( font, font->name, 1, 64.0f, 512, 512, 32, 96 ); } float posx = ( viewport_matrix[ 3 ] * 0.5f ) - ( FONT_length( font, msg ) * 0.5f ), posy = viewport_matrix[ 2 ] - font->font_size; FONT_print( font, posx + 4.0f, posy - 4.0f, msg, &color ); color.y = 1.0f; FONT_print( font, posx, posy, msg, &color ); } }