/*
 * void spawn_next_ball();
 *
 * checks if the system has enough mass to spawn the new ball
 */
void spawn_next_ball() {
	//printf("%f/%f till next spawn\n", mass_of_system, next_ball_mass);
	if( mass_of_system >= next_ball_mass ) {
		all_spheres.resize(all_spheres.size()+1);
		all_spheres.push_back( generate_sphere(1) );
		mass_of_system -= next_ball_mass; 
		next_ball_radius = random_radius();
		next_ball_mass = get_mass( next_ball_radius );
	}
}
/*
 * void gfxinit();
 *
 * initializes the system prior to animating
 */
void gfxinit() {
	mass_of_system = 0.0;
	balls = NUMBER_OF_BALLS;
	all_spheres.resize(balls);
	
	current = 0.0;
	// LIGHTING 	
    GLfloat lightpos[4] = { 1.0, 0.0, 1.0, 1.0 };     // light position
    GLfloat lightamb[4] = { 0.0, 0.0, 0.0, 1.0 };     // ambient colour
    GLfloat lightdif[4] = { 1.0, 1.0, 1.0, 1.0 };     // diffuse colour
    GLfloat global_ambient[4] = {0.2, 0.2, 0.2, 1};

    glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
    // set the ambient light colour
    glLightfv(GL_LIGHT0, GL_AMBIENT, lightamb);
    // set the diffuse light colour
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightdif);
	// global ambient
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
	// turn on lighting
    glEnable(GL_LIGHTING);
    // enable light 0, all the other lights are off
    glEnable(GL_LIGHT0);

    // enable the depth buffer
    glEnable(GL_DEPTH_TEST);
   
    glMatrixMode(GL_PROJECTION);
    gluPerspective(60.0, 16/9., 0.1, 500.0);
    //glOrtho(-5.0,5.0,-5.0,5.0,1.0,20.0);
	glMatrixMode(GL_MODELVIEW);
    //gluLookAt(x, y, z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
	
	
	//glRasterPos2i(10, 10);
	//glColor3f(1.0f, 1.0f, 1.0f);
	//glutBitmapString(GLUT_BITMAP_HELVETICA_18, (const unsigned char*)"dog");

	glEnable ( GL_COLOR_MATERIAL ) ;
	glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
	glShadeModel(GL_SMOOTH);

	srand(time(NULL));	// seed for rand() calls

	// create all spheres for the initial system state
	int k;
	for( k = 0; k < all_spheres.size(); k++ ) { //setup all ball settings
		//printf("%d\n",k);
		all_spheres[k] = generate_sphere(0);
	}
	next_ball_radius = random_radius();
	next_ball_mass = get_mass( next_ball_radius ); 
}
/*
 * struct sphere generate_sphere();
 * 
 * return a ball 
 */
struct sphere generate_sphere() {
		struct sphere ball;
		
		do{
			// RADIUS MUST BE BEFORE RANDOM POINTS
			ball.radius = random_radius();
			// gets 4 random points for the bezier curve
			ball.p1 = random_ranged_point(ball.radius);
			ball.p2 = new_curve_point(ball.p1);		
			ball.p3 = new_curve_point(ball.p2);
			ball.p4 = new_curve_point(ball.p3);
			
			// position starts at p1
			ball.pos = ball.p1;
			ball.previous_pos = {0.0, 0.0};
	//printf("generation: %f %f || %f %f\n", ball.previous_pos.x, ball.previous_pos.y, ball.pos.x, ball.pos.y);
			
			// gets a random direction, this might be a wasted step
			ball.direction = random_direction(ball.radius);

			ball.active = 0;

			ball.velocity = random_velocity();	

			// start on a curved path
			ball.path = 1;
			// dead variable is set to 0, used to prevent collisions
			//ball.dead = 0;
			ball.color = random_color();		

			ball.curve_length = curve_length( &ball );
			ball.start_time = (double) clock();
			ball.curve_time = ball.curve_length / ball.velocity;
			ball.ghost = 0;
		}while(collision_detection(ball) == 1 );	
		
		return ball;
}
/*
 * struct sphere generate_sphere();
 * 
 * return a ball 
 */
struct sphere generate_sphere(int rad) {
		struct sphere ball;
		
		do{
			// RADIUS MUST BE BEFORE RANDOM POINTS
			ball.radius = (rad) ? next_ball_radius : random_radius();
			// gets 4 random points for the bezier curve
			ball.p1 = random_ranged_point(ball.radius);
			ball.p2 = new_curve_point(ball.p1);		
			ball.p3 = new_curve_point(ball.p2);
			ball.p4 = new_curve_point(ball.p3);
			
			// position starts at p1
			ball.pos = ball.p1;
			// 3D
			ball.previous_pos = {0.0, 5.0, 0.0};
			
			// gets a random direction, this might be a wasted step
			ball.direction = random_direction(ball.radius);

			ball.active = 0;

			ball.velocity = random_velocity();	

			// start on a curved path
			ball.path = 1;
			ball.color = random_color();		

			ball.curve_length = curve_length( &ball );
			ball.start_time = (double) clock();
			ball.curve_time = ball.curve_length / ball.velocity;
			ball.ghost = 0;
			// 3D
		}while(collision_detection(ball) == 1 || ball.p1.x == 0.0 || ball.p1.y == 0.0 ||
				ball.p1.z == 0.0);	
		
		return ball;
}
Example #5
0
			/// Draws a random point from the n-sphere
			VectorType random_n_sphere(int arg_dim){

				return random_radius(arg_dim) * 
						random_gaussian(arg_dim).normalized();
			}
Example #6
0
sgeroids::view::planar::background::object::object(
	sge::renderer::device::core &_renderer,
	sge::renderer::vertex::declaration const &_vertex_declaration,
	sgeroids::view::planar::texture_tree &_texture_tree,
	sgeroids::model::play_area const &_play_area,
	sgeroids::random_generator &_rng,
	star_size const _star_size,
	star_count const _star_count)
:
	sprite_buffers_(
		sge::sprite::buffers::parameters(
			_renderer,
			_vertex_declaration),
		sge::sprite::buffers::option::dynamic),
	sprite_collection_(),
	sprite_state_(
		_renderer,
		sge::sprite::state::parameters<
			sprite_state_choices
		>()),
	sprites_(),
	sprite_render_range_(),
	texture_tree_(
		_texture_tree),
	play_area_(
		_play_area),
	star_size_(
		_star_size),
	star_count_(
		_star_count)
{
	typedef fcppt::random::distribution::basic<
		fcppt::random::distribution::parameters::uniform_int<
			int
		>
	> int_distribution;

	typedef fcppt::random::distribution::basic<
		fcppt::random::distribution::parameters::uniform_real<
			float
		>
	> float_distribution;

	typedef fcppt::random::variate<
		sgeroids::random_generator,
		int_distribution
	> int_rng;

	typedef fcppt::random::variate<
		sgeroids::random_generator,
		float_distribution
	> float_rng;

	int_rng random_x(
		_rng,
		int_distribution(
			int_distribution::param_type::min(
				0),
			int_distribution::param_type::max(
				play_area_.get().size().w())));

	int_rng random_y(
		_rng,
		int_distribution(
			int_distribution::param_type::min(
				0),
			int_distribution::param_type::max(
				play_area_.get().size().h())));

	float_rng random_angle(
		_rng,
		float_distribution(
			float_distribution::param_type::min(
				0.f),
			float_distribution::param_type::sup(
				6.f)));

	int_rng random_radius(
		_rng,
		int_distribution(
			int_distribution::param_type::min(
				math::unit_magnitude() * star_size_.get()),
			int_distribution::param_type::max(
				math::unit_magnitude() * star_size_.get() * 4)));

	for (
		star_count::value_type index = 0;
		index < star_count_.get();
		++index
	)
		sprites_.push_back(
			planar::sprite::object(
				sge::sprite::roles::connection{} =
					sprite_collection_.connection(
						0
					),
				sge::sprite::roles::texture0{} =
					sgeroids::view::planar::sprite::object::texture_type{
						texture_tree_.get(
							sge::resource_tree::path() / FCPPT_TEXT("star")
						)
					},
				sge::sprite::roles::size{} =
					planar::sprite_size_from_texture_and_radius(
						*texture_tree_.get(
							sge::resource_tree::path() / FCPPT_TEXT("star")),
						planar::radius(random_radius())
					),
				sge::sprite::roles::center{} =
					planar::sprite::object::vector(
						random_x(),
						random_y()
					),
				sge::sprite::roles::rotation{} =
					0.f,
				sge::sprite::roles::color{} =
					sge::image::color::any::convert<
						sgeroids::view::planar::sprite::color_format
					>(
						sge::image::color::predef::white()
					)
			)
		);

	sprites_.push_back(
		planar::sprite::object(
			sge::sprite::roles::connection{} =
				sprite_collection_.connection(
					2
				),
			sge::sprite::roles::texture0{} =
				sgeroids::view::planar::sprite::object::texture_type{
					texture_tree_.get(
						sge::resource_tree::path() / FCPPT_TEXT("planet")
					)
				},
			sge::sprite::roles::size{} =
				planar::sprite_size_from_texture_and_radius(
					*texture_tree_.get(
						sge::resource_tree::path() / FCPPT_TEXT("planet")),
					planar::radius(30 * random_radius())
				),
			sge::sprite::roles::center{} =
				planar::sprite::object::vector(
					random_x(),
					random_y()
				),
			sge::sprite::roles::rotation{} =
				random_angle(),
			sge::sprite::roles::color{} =
				sge::image::color::any::convert<
					sgeroids::view::planar::sprite::color_format
				>(
					sge::image::color::predef::white()
				)
		)
	);

	sprites_.push_back(
		planar::sprite::object(
			sge::sprite::roles::connection{} =
				sprite_collection_.connection(
					1
				),
			sge::sprite::roles::texture0{} =
				sgeroids::view::planar::sprite::object::texture_type{
					texture_tree_.get(
						sge::resource_tree::path() / FCPPT_TEXT("nebula"))
				},
			sge::sprite::roles::size{} =
				planar::sprite_size_from_texture_and_radius(
					*texture_tree_.get(
						sge::resource_tree::path() / FCPPT_TEXT("nebula")),
					planar::radius(math::unit_magnitude() * 1024 * 1024)
				),
			sge::sprite::roles::center{} =
				planar::sprite::object::vector(
					random_x(),
					random_y()
				),
			sge::sprite::roles::rotation{} =
				random_angle(),
			sge::sprite::roles::color{} =
				sge::image::color::any::convert<
					sgeroids::view::planar::sprite::color_format
				>(
					sge::image::color::predef::white()
				)
		)
	);

	sprite_render_range_ =
		sge::sprite::geometry::sort_and_update(
			sprite_collection_.range(),
			sge::sprite::compare::default_(),
			sprite_buffers_);
}