Body Collision::create_ground_body(PhysicsWorld &phys_world) { PhysicsContext pc = phys_world.get_pc(); BodyDescription ground_desc(phys_world); ground_desc.set_position(Vec2f((float)window_x_size/2.0f,(float)window_y_size)); ground_desc.set_type(body_static); Body ground(pc, ground_desc); //Setup ground fixture PolygonShape ground_shape(phys_world); ground_shape.set_as_box((float)window_x_size/2,20.0f); FixtureDescription fixture_desc(phys_world); fixture_desc.set_shape(ground_shape); fixture_desc.set_friction(1.0f); fixture_desc.set_density(1000.0f); Fixture ground_fixture(pc, ground, fixture_desc); return ground; }
Enemy::Enemy(Game &game_) { ID = staticID; staticID++; add_enemy(this); game = &game_; Canvas &canvas = game_.get_canvas(); PhysicsContext pc = game_.get_pc(); ResourceManager &resources = game_.get_resources(); //________________________________________________________________________ // G A M E P L A Y type = go_enemy; eType = T_HOVERBOT; missile.set_game(*game); missile.set_speed(90.0f); missile.set_type(MissileDesc::mt_energy); missile.should_hurt_player(true); missile.should_hurt_enemy(false); turret_angle = Angle(0,angle_degrees); time_since_last_shoot = 0.0f; //________________________________________________________________________ // R E N D E R enemy = Sprite::resource(canvas,"Enemy1", resources); enemy.set_play_loop(true); //int x,y; //Origin origin; //enemy->get_alignment(origin,x,y); update_callback = std::function<void(int)>(std::bind(&Enemy::update, this, std::placeholders::_1)); cc.connect(game_.get_draw_sig(), clan::bind_member(this, &Enemy::draw)); cc.connect(game_.get_update_sig(), update_callback); //__________________________________________________________________________ // P H Y S I C S BodyDescription body_desc(pc); body_desc.set_position(0, game->get_height()-40); body_desc.set_type(body_dynamic); body_desc.set_angular_damping(100.0f); PolygonShape shape(pc); shape.set_as_box(enemy.get_width()/2.5f, enemy.get_height()/2.5f); FixtureDescription fixture_desc(pc); fixture_desc.set_density(1000.0f); fixture_desc.set_shape(shape); body = Body(pc, body_desc); body.set_data(this); Fixture(pc, body, fixture_desc); pos.x = 50; pos.y = 50; life = 100; is_dead = false; body.set_position(pos); speed = -35; body.set_linear_velocity(Vec2f(speed,0)); target = Player::getPlayer1(); }
// The start of the Application int Raycasting::start(const std::vector<std::string> &args) { //Remove the need to send physic world to every object. Instead send just the description. //Fix having two fixtures working weirdly. quit = false; int window_x_size = 640; int window_y_size = 480; // Set the window DisplayWindowDescription desc; desc.set_title("ClanLib Raycasting Example"); desc.set_size(Size(window_x_size, window_y_size), true); desc.set_allow_resize(false); DisplayWindow window(desc); // Connect the Window close event Slot slot_quit = window.sig_window_close().connect(this, &Raycasting::on_window_close); // Connect a keyboard handler to on_key_up() Slot slot_input_up = (window.get_ic().get_keyboard()).sig_key_up().connect(this, &Raycasting::on_input_up); // Create the canvas Canvas canvas(window); //Setup physic world PhysicsWorldDescription phys_desc; phys_desc.set_gravity(0.0f,10.0f); phys_desc.set_sleep(true); phys_desc.set_physic_scale(100); phys_desc.set_timestep(1.0f/200.0f); PhysicsWorld phys_world(phys_desc); //Get the Physics Context PhysicsContext pc = phys_world.get_pc(); //Setup ground body BodyDescription ground_desc(phys_world); ground_desc.set_position(Vec2f((float)window_x_size/2.0f,(float)window_y_size)); ground_desc.set_type(body_static); Body ground(pc, ground_desc); //Setup ground fixture PolygonShape ground_shape(phys_world); ground_shape.set_as_box((float)window_x_size/2,20.0f); FixtureDescription fixture_desc(phys_world); fixture_desc.set_shape(ground_shape); Fixture ground_fixture(pc, ground, fixture_desc); //Setup box body BodyDescription box_desc(phys_world); box_desc.set_position(Vec2f(10.0f,10.0f)); box_desc.set_type(body_dynamic); box_desc.set_linear_velocity(Vec2f(100.0f,0.0f)); Body box(pc, box_desc); box_desc.set_position(Vec2f((float)window_x_size-50.0f,100.0f)); box_desc.set_linear_velocity(Vec2f(-80.0f,0.0f)); Body box2(pc, box_desc); //Setup box fixture PolygonShape box_shape(phys_world); box_shape.set_as_box(30.0f, 30.0f); FixtureDescription fixture_desc2(phys_world); fixture_desc2.set_shape(box_shape); fixture_desc2.set_restitution(0.6f); fixture_desc2.set_friction(0.0005f); Fixture box_fixture(pc, box, fixture_desc2); Fixture box_fixture2(pc, box2, fixture_desc2); Vec2f ground_pos = ground.get_position(); unsigned int last_time = System::get_time(); //Setup debug draw. PhysicsDebugDraw debug_draw(phys_world); debug_draw.set_flags(f_shape|f_aabb); GraphicContext gc = canvas.get_gc(); PhysicsQueryAssistant qa = phys_world.get_qa(); clan::Font font(canvas, "Tahoma", 12); // Set raycast points Pointf p1(300,500); Pointf p2(100,100); // Set query rect; Rectf rect1(400.0f, 380.0f, Sizef(50.0f,50.0f)); // Run until someone presses escape while (!quit) { unsigned int current_time = System::get_time(); float time_delta_ms = static_cast<float> (current_time - last_time); last_time = current_time; canvas.clear(); //Raycast font.draw_text(canvas, 10,20, "Raycasting..."); qa.raycast_first(p1, p2); if(qa.has_query_result()) { font.draw_text(canvas, 100,20, "Found object !"); canvas.draw_line(p1,p2, Colorf::green); } else canvas.draw_line(p1, p2, Colorf::red); //Raycast //Query font.draw_text(canvas, 10,35, "Querying..."); qa.query_any(rect1); if(qa.has_query_result()) { font.draw_text(canvas, 100,35, "Found object !"); canvas.draw_box(rect1, Colorf::green); } else canvas.draw_box(rect1, Colorf::red); //Query phys_world.step(); debug_draw.draw(canvas); canvas.flush(); window.flip(1); // This call processes user input and other events KeepAlive::process(0); System::sleep(10); } return 0; }
Player::Player(Game &game_) { //Init static vars Player::player1 = this; Player::isPlayer1Playing = true; // game = &game_; Canvas canvas = game_.get_canvas(); PhysicsContext pc = game_.get_pc(); ResourceManager resources = game_.get_resources(); vehicle = Sprite::resource(canvas, "Car1", resources); vehicle.set_linear_filter(false); //________________________________________________________________________ // T U R R E T turret = Sprite::resource(canvas, "Turret1", resources); turretBase = Sprite::resource(canvas, "Turret1Base", resources); turret.set_linear_filter(false); turretBase.set_linear_filter(false); turret_angle = Angle(0,angle_degrees); time_since_last_shoot = 0.0f; missile.set_game(*game); missile.set_speed(500.0f); missile.set_type(MissileDesc::mt_bullet); //________________________________________________________________________ // R E N D E R draw_slot = game_.get_draw_sig().connect(this,&Player::draw); update_slot = game_.get_update_sig().connect(this,&Player::update); //________________________________________________________________________ // P H Y S I C S BodyDescription body_desc(pc); body_desc.set_position(0, game->get_height()-40); body_desc.set_type(body_dynamic); body_desc.set_angular_damping(100.0f); PolygonShape shape(pc); shape.set_as_box(vehicle.get_width()/2.5f, vehicle.get_height()/4.0f, Vec2f(0.0f, 5.0f), Angle(0, angle_degrees)); FixtureDescription fixture_desc(pc); fixture_desc.set_density(1000.0f); fixture_desc.set_shape(shape); body = Body(pc, body_desc); body.set_data(this); Fixture(pc, body, fixture_desc); /* pos_x=0; pos_y=game->get_height()-40; */ xAcc = 1000.0f; yAcc = 600.0f; x_max_speed=80.0f; y_max_speed=40.0f; x_speed=0.0f; y_speed=0.0f; go_right = false; go_left = false; go_up = false; go_down = false; doShoot1 = false; doShoot2 = false; wobble_timer = 0; turretBasePos.x = 0.0f; turretBasePos.y = 0.0f; turretPos.x = 0.0f; turretPos.y = 0.0f; //__________________________________________________________________________ // G A M E P L A Y type = go_player; life = max_life = 100.0f; is_dead= false; target_x_pos = 0.0f; target_y_pos = 0.0f; }
//________________________________________________________________________ //________________________________________________________________________ Missile::Missile(MissileDesc &desc) { type = go_missile; game = desc.game; speed = desc.speed_; angle = desc.angle_; pos = desc.pos_; mType = desc.mType_; does_hurt_player = desc.does_hurt_player_; does_hurt_enemy = desc.does_hurt_enemy_; GraphicContext &gc = game->get_gc(); PhysicsContext pc = game->get_pc(); ResourceManager &resources = game->get_resources(); //___________________________________________________________________ // G F X switch(mType) { case MissileDesc::mt_bullet: bullet = Sprite::resource(gc, "Bullet1", resources); damage = 20; break; case MissileDesc::mt_energy: bullet = Sprite::resource(gc, "Bullet2", resources); damage = 10; break; case MissileDesc::mt_rocket: bullet = Sprite::resource(gc, "Rocket", resources); damage = 50; break; } bullet.set_linear_filter(false); bullet.set_angle(angle); //___________________________________________________________________ // P H Y S I C S BodyDescription body_desc(pc); body_desc.set_position(0, game->get_height()-40); body_desc.set_type(body_dynamic); PolygonShape shape(pc); shape.set_as_box(bullet.get_width()/4, bullet.get_height()/4); FixtureDescription fixture_desc(pc); fixture_desc.set_density(50.0f); fixture_desc.set_shape(shape); body = Body(pc, body_desc); body.set_data(this); Fixture(pc, body, fixture_desc); body.set_position(pos); body.set_angle(angle); x_speed = speed*cos(angle.to_radians()); y_speed = speed*sin(angle.to_radians()); body.set_linear_velocity(Vec2f(x_speed, y_speed)); lifeTime= 3000.0f; currentLifeTime=0.0f; //___________________________________________________________________ // C O R E update_slot = game->get_update_sig().connect(this,&Missile::update); draw_slot = game->get_draw_sig().connect(this,&Missile::draw); should_die = false; }