Example #1
0
// The start of the Application
int Collision::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;

	// Set the window
	DisplayWindowDescription desc;
	desc.set_title("ClanLib Collision 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
	window.sig_window_close().connect(this, &Collision::on_window_close);

	// Connect a keyboard handler to on_key_up()
	window.get_ic().get_keyboard().sig_key_up().connect(this, &Collision::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/60.0f);
	phys_desc.set_velocity_iterations(8);
	phys_desc.set_position_iterations(3);

	PhysicsWorld phys_world(phys_desc);

	//Setup ground body
	Body ground = create_ground_body(phys_world);

	unsigned int last_time = System::get_time();

	//Setup outline body
	Body outline_body = create_outline_body(phys_world);
	outline_body.set_position(Vec2f(200.0f,200.0f));

	//Setup debug draw.
	PhysicsDebugDraw debug_draw(phys_world);
	debug_draw.set_flags(f_shape);

	GraphicContext gc = canvas.get_gc();
	
	// 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();
		
		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;
}
Example #2
0
// The start of the Application
int Joints::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;

	// Set the window
	DisplayWindowDescription desc;
	desc.set_title("ClanLib Joints 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, &Joints::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, &Joints::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);

	PhysicsWorld phys_world(phys_desc);

	//Setup ground body
	Body ground = create_ground_body(phys_world);

	unsigned int last_time = System::get_time();

	//Setup debug draw.
	PhysicsDebugDraw debug_draw(phys_world);
	debug_draw.set_flags(f_shape|f_aabb|f_joint);

	GraphicContext gc = canvas.get_gc();
	//Setup joints

	const int number_of_joints = 3;
	std::vector<Body> bodies_A(number_of_joints);
	std::vector<Body> bodies_B(number_of_joints);
	std::vector<std::shared_ptr<Joint>> Joints(number_of_joints);
	

	for(int i=0; i<number_of_joints; i++)
	{
		bodies_A[i] = create_box_body(phys_world);
		bodies_A[i].set_position(Vec2f(80.0f+80.0f*i,280.0f));

		bodies_B[i] = create_box_body(phys_world);
		bodies_B[i].set_position(Vec2f(80.0f+80.0f*i,440.0f));

		Joints[i] = create_joint(phys_world, bodies_A[i], bodies_B[i], i);
	}

	// 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();
		
		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;
}