void weather_update() { static Uint32 last_update = 0; Uint32 ticks = cur_time - last_update; int i; update_wind(); // we update the lightning if (lightning_falling && cur_time > lightning_stop) { lightning_falling = 0; if (skybox_update_delay > 0) skybox_update_colors(); } // we update the areas for (i = 0; i < MAX_WEATHER_AREAS; ++i) if (weather_areas[i].type > 0 && weather_areas[i].intensity_change_duration > 0) { if (weather_areas[i].intensity_change_duration <= ticks) { weather_areas[i].intensity += weather_areas[i].intensity_change_speed*weather_areas[i].intensity_change_duration; weather_areas[i].intensity_change_duration = 0; } else { weather_areas[i].intensity += weather_areas[i].intensity_change_speed*ticks; weather_areas[i].intensity_change_duration -= ticks; } if (weather_areas[i].intensity_change_speed < 0.0) { if (weather_areas[i].intensity <= 0.001) { weather_areas[i].intensity = 0.0; weather_areas[i].type = 0; } } else { if (weather_areas[i].intensity > 1.0) weather_areas[i].intensity = 1.0; } } // we compute the ratios at the actor position weather_compute_ratios(weather_ratios, -camera_x, -camera_y); current_weather_density = weather_get_density_from_ratios(weather_ratios); // we compute the weather color at the actor position weather_get_color_from_ratios(weather_color, weather_ratios); // we update the weather types for (i = 1; i < MAX_WEATHER_TYPES; ++i) update_weather_type(i, -camera_x, -camera_y, 0.0, ticks); last_update = cur_time; }
/* update the balloon simulation by one time step */ void Balloon::update(const struct sitl_input &input) { // get wind vector setup update_wind(input); if (!released && input.servos[6] > 1800) { ::printf("Balloon released\n"); released = true; } if (!burst && input.servos[7] > 1800) { ::printf("Balloon burst\n"); burst = true; } // rotational air resistance Vector3f rot_accel = -gyro * radians(400) / terminal_rotation_rate; // air resistance Vector3f air_resistance = -velocity_air_ef * (GRAVITY_MSS/terminal_velocity); float lift_accel = 0; if (!burst && released) { float air_resistance_at_climb_rate = climb_rate * (GRAVITY_MSS/terminal_velocity); lift_accel = air_resistance_at_climb_rate + GRAVITY_MSS * dcm.c.z; } accel_body = Vector3f(0, 0, -lift_accel); accel_body += dcm * air_resistance; update_dynamics(rot_accel); if (position.z < -burst_altitude) { ::printf("Balloon burst at %.1f\n", -position.z); burst = true; } // update lat/lon/altitude update_position(); }
/* update the helicopter simulation by one time step */ void Helicopter::update(const struct sitl_input &input) { // get wind vector setup update_wind(input); float rsc = constrain_float((input.servos[7]-1000) / 1000.0f, 0, 1); // ignition only for gas helis bool ignition_enabled = gas_heli?(input.servos[5] > 1500):true; float thrust = 0; float roll_rate = 0; float pitch_rate = 0; float yaw_rate = 0; float torque_effect_accel = 0; float lateral_x_thrust = 0; float lateral_y_thrust = 0; float swash1 = (input.servos[0]-1000) / 1000.0f; float swash2 = (input.servos[1]-1000) / 1000.0f; float swash3 = (input.servos[2]-1000) / 1000.0f; if (!ignition_enabled) { rsc = 0; } float rsc_scale = rsc/rsc_setpoint; switch (frame_type) { case HELI_FRAME_CONVENTIONAL: { // simulate a traditional helicopter float tail_rotor = (input.servos[3]-1000) / 1000.0f; thrust = (rsc/rsc_setpoint) * (swash1+swash2+swash3) / 3.0f; torque_effect_accel = (rsc_scale+thrust) * rotor_rot_accel; roll_rate = swash1 - swash2; pitch_rate = (swash1+swash2) / 2.0f - swash3; yaw_rate = tail_rotor - 0.5f; lateral_y_thrust = yaw_rate * rsc_scale * tail_thrust_scale; break; } case HELI_FRAME_DUAL: { // simulate a tandem helicopter float swash4 = (input.servos[3]-1000) / 1000.0f; float swash5 = (input.servos[4]-1000) / 1000.0f; float swash6 = (input.servos[5]-1000) / 1000.0f; thrust = (rsc / rsc_setpoint) * (swash1+swash2+swash3+swash4+swash5+swash6) / 6.0f; torque_effect_accel = (rsc_scale + rsc / rsc_setpoint) * rotor_rot_accel * ((swash1+swash2+swash3) - (swash4+swash5+swash6)); roll_rate = (swash1-swash2) + (swash4-swash5); pitch_rate = (swash1+swash2+swash3) - (swash4+swash5+swash6); yaw_rate = (swash1-swash2) + (swash5-swash4); break; } case HELI_FRAME_COMPOUND: { // simulate a compound helicopter float right_rotor = (input.servos[3]-1000) / 1000.0f; float left_rotor = (input.servos[4]-1000) / 1000.0f; thrust = (rsc/rsc_setpoint) * (swash1+swash2+swash3) / 3.0f; torque_effect_accel = (rsc_scale+thrust) * rotor_rot_accel; roll_rate = swash1 - swash2; pitch_rate = (swash1+swash2) / 2.0f - swash3; yaw_rate = right_rotor - left_rotor; lateral_x_thrust = (left_rotor+right_rotor-1) * rsc_scale * tail_thrust_scale; break; } } roll_rate *= rsc_scale; pitch_rate *= rsc_scale; yaw_rate *= rsc_scale; // rotational acceleration, in rad/s/s, in body frame Vector3f rot_accel; rot_accel.x = roll_rate * roll_rate_max; rot_accel.y = pitch_rate * pitch_rate_max; rot_accel.z = yaw_rate * yaw_rate_max; // rotational air resistance rot_accel.x -= gyro.x * radians(5000.0) / terminal_rotation_rate; rot_accel.y -= gyro.y * radians(5000.0) / terminal_rotation_rate; rot_accel.z -= gyro.z * radians(400.0) / terminal_rotation_rate; // torque effect on tail rot_accel.z += torque_effect_accel; // air resistance Vector3f air_resistance = -velocity_air_ef * (GRAVITY_MSS/terminal_velocity); // scale thrust to newtons thrust *= thrust_scale; accel_body = Vector3f(lateral_x_thrust, lateral_y_thrust, -thrust / mass); accel_body += dcm * air_resistance; bool was_on_ground = on_ground(position); update_dynamics(rot_accel); // constrain height to the ground if (on_ground(position) && !was_on_ground) { // zero roll/pitch, but keep yaw float r, p, y; dcm.to_euler(&r, &p, &y); dcm.from_euler(0, 0, y); position.z = -(ground_level + frame_height - home.alt*0.01f); velocity_ef.zero(); } // update lat/lon/altitude update_position(); }
int main(void) { /* Related object data */ system_t* system; player_t* player1; player_t* player2; alt_timestamp_start(); int start_time = alt_timestamp(); int wind; int restart = 0; /* initialize all hardware dev */ system = system_init(VIDEO_PIXEL_BUFFER_DMA_NAME, "/dev/video_character_buffer_with_dma_0", ALTERA_UP_SD_CARD_AVALON_INTERFACE_0_NAME, PS2_0_NAME); if (!alt_up_sd_card_is_Present()) { printf("SD card not present\n"); return -2; } else { printf("SD card detected.\n"); } if (!alt_up_sd_card_is_FAT16()) { printf("SD card is not FAT16.\n"); return -3; } else { printf("SD card is FAT 16.\n"); } fireSound = initSoundbank("shotgun.wav"); explosionSound = initSoundbank("big_bomb.wav"); printf("Done Initializing\n"); int end_time = alt_timestamp(); srand(end_time - start_time); clearScreen(system); printf("Press enter to start game"); restartGame(system); while (restart == 1) { /* initialize required objects */ player1 = makePlayer(1, "Lomash"); player2 = makePlayer(2, "TJ"); /* * Draw the screen for the first time. */ store_background_data(); draw_background(system); drawPlayers(system); draw_ground(system); draw_windbox(system); draw_player1GUI(system); draw_player2GUI(system); update_wind(system, wind); draw_health(player1, system); draw_health(player2, system); update_power(player1, system); update_power(player2, system); usleep(2000000); // sleep to wait for video buffer to load while (TRUE) { printf( "\n\n\n\n===========================================================\n"); printf("Player 1 Health = %d \t\t Player 2 Health = %d\n", player1->health, player2->health); wind = rand() % 11 - 5; draw_windbox(system); update_wind(system, wind); // Player 1's turn player_1_jump(system); player_1_jump(system); printf("Getting Player 1 Power.\n"); getKeyboardInput(1, player1, system); // Power skipOneEnter(system); printf("Getting Player 1 Angle.\n"); getKeyboardInput(2, player1, system); // Angle // Player 1 Animation printf("Starting animation\n"); clear_angle_drawer(system); animate_cannon1(system); playSound(fireSound, system->audio); switch (animateShooting(system, player1, wind)) { // different value for result case 1: { update_health(player2, system, -DAMAGE); printf( "Player 1 hit player 2.\n Remaining Health for Player 2: %d\n", player2->health); break; } case 2: { update_health(player1, system, -DAMAGE); printf( "Player 1 hit player 1.\n Remaining Health for Player 1: %d\n", player1->health); break; } default: { break; } } printf("Ended animation\n"); // Post-animation Calculation if (player1->health <= 0 || player2->health <= 0) { break; } // Player 2's turn wind = rand() % 11 - 5; draw_windbox(system); update_wind(system, wind); player_2_jump(system); player_2_jump(system); printf("Getting Player 2 Velocity.\n"); getKeyboardInput(1, player2, system); // Player 2 Angle-Selection skipOneEnter(system); printf("Getting Player 2 Angle.\n"); getKeyboardInput(2, player2, system); // Player 2 Animation printf("Starting animation\n"); clear_angle_drawer(system); animate_cannon2(system); playSound(fireSound, system->audio); // Post-animation Calculation switch (animateShooting(system, player2, wind)) { // different value for result case 1: { update_health(player1, system, -DAMAGE); printf( "Player 2 hit player 1.\n Remaining Health for Player 1: %d\n", player1->health); break; } case 2: { update_health(player2, system, -DAMAGE); printf( "Player 2 hit player 2.\n Remaining Health for Player 2: %d\n", player2->health); break; } default: { break; } } if (player1->health <= 0 || player2->health <= 0) { break; } }; /* * Find out who won. */ if (player1->health <= 0) { printf("Player 2 Wins!!! \n"); draw_P2WIN(system); } else if (player2->health <= 0) { printf("Player 1 Wins!!!\n"); draw_P1WIN(system); } else { printf("we shouldn't be here.\n"); } restart = restartGame(system); } return 0; // FIN }