Click here to Skip to main content
15,894,540 members
Articles / Multimedia / OpenGL

Bouncing Balls in OSG

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
29 Aug 2008CPOL4 min read 36.9K   835   26  
Simulation in OSG using discrete events, a Ternary Heap and Interpolation
// Physics.cpp : Defines the entry point for the console application.

#include "stdafx.h"

#include <osg/Material>

#include <osg/Group>
#include <osg/Texture2D>
#include <osg/ShapeDrawable>
#include <osg/PositionAttitudeTransform>
#include <osgdb/ReadFile>

#include "UpdateCallback.h"
#include "Simulator.h"
#include "Situation.h"
#include "Ball.h"
#include "BounceBalls.h"


int __stdcall WinMain(
  int hInstance,  // handle to current instance
  int hPrevInstance,  // handle to previous instance
  char * lpCmdLine,      // pointer to command line
  int nCmdShow          // show state of window
)
/*
int _tmain(int argc, _TCHAR* argv[])
*/
{
	CSimulator sim;

	osg::Group *root = new osg::Group();

	osg::Material *material = new osg::Material();
	/*
	material->setColorMode( osg::Material::DIFFUSE );
    material->setAmbient(osg::Material::FRONT_AND_BACK,
		osg::Vec4(0, 0.5, 0, 1));
    material->setSpecular(osg::Material::FRONT_AND_BACK,
		osg::Vec4(1, 0.5, 1, 1));
    material->setShininess(osg::Material::FRONT_AND_BACK,
		64.0f);
		*/
	material->setTransparency(osg::Material::FRONT_AND_BACK,
		0.8f);

	osg::Box *box = new osg::Box( osg::Vec3(), 20.0f );
	osg::Geode *boxGeode = new osg::Geode();
	osg::ShapeDrawable *sdBox = new osg::ShapeDrawable( box );
	{
		osg::StateSet *sdState = sdBox->getOrCreateStateSet();
		sdState->setMode( 
			GL_BLEND,
			osg::StateAttribute::ON);
		sdState->setRenderingHint(osg::StateSet::RenderingHint::TRANSPARENT_BIN);
		sdState->setAttributeAndModes(
			material, osg::StateAttribute::ON);
	}
	boxGeode->addDrawable( sdBox );
	root->addChild( boxGeode );

	osg::Sphere *sphere = new osg::Sphere( osg::Vec3( 0.0f, 0.0f, 0.0f ), 2.0f );
	osg::ShapeDrawable *sd = new osg::ShapeDrawable(sphere);
	{
		osg::StateSet *sdState = sd->getOrCreateStateSet();
		osg::Texture2D *texture = new osg::Texture2D();
		osg::Image *image = osgDB::readImageFile( "SolarSystem/earth_clouds256128.jpg" );
		texture->setImage( image );
		sdState->setTextureAttributeAndModes( 0, texture, osg::StateAttribute::ON );
	}
	osg::Geode *geode = new osg::Geode();
	geode->addDrawable( sd );
	osg::Vec3d pos0( -9.0, 0.0, 0.0 );
	osg::Vec3d vel0( 0.1, -30.1, 1.1 );
	osg::Vec3d posInc0( 4.1, 0.0, 0.0 );
	osg::Vec3d posInc1( 0.0, 4.1, 0.0 );
	osg::Vec3d velSet( 0.01, 0.01, 0.01 );
	const int MAX = 16;
	CBall *balls[MAX];
	double past = -1000.0;
	for( int i=0; i<MAX; ++i )
	{
		osg::PositionAttitudeTransform *pat = new osg::PositionAttitudeTransform();
		pat->addChild( geode );
		CUpdateCallback *UpdateCallback = new CUpdateCallback();
		UpdateCallback->pat = pat;
		pat->setUpdateCallback( UpdateCallback );

		CBall *ball = new CBall( &sim, 2.0, past, pos0, vel0 );
		pos0 += posInc0;
		if( pos0.x() > 9.1 )
		{
			pos0.x() = -9;
			pos0 += posInc1;
		}
		vel0 = velSet;
		balls[i] = ball;
		for( int j=0; j<i; ++j )
		{
			CBounceBalls *collider = new CBounceBalls( ball, balls[j] );
			ball->AddCollider( collider );
			balls[j]->AddCollider( collider );
			sim.Add( collider );
		}
		ball->PredictChanges();
		UpdateCallback->sit = &ball->situation;
		root->addChild( pat );
	}

	sim.Run( root );
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Engineering, Design and Models Ltd
United Kingdom United Kingdom
I develop rail signalling simulation technology at EDM where I use C++ and C#, but also love to dabble in functional programming languages such as Clean and Cat.

Comments and Discussions