예제 #1
0
void BulletTestApp::initTest()
{

	// Clean up last test
	if ( mWorld ) {
		mWorld->clear();
	}
	
	// Used when generating terrain
	Channel32f heightField;

	// Create and add the wobbly box
	mGroundTransform.setIdentity();
	switch ( mTest ) {
	case 0:
		mGround = bullet::createRigidBox( mWorld, Vec3f( 200.0f, 35.0f, 200.0f ), 0.0f );
		bullet::createRigidSphere( mWorld, 50.0f, 64, 0.0f, Vec3f( 0.0f, -50.0f, 0.0f ) );
		break;
	case 1:
		mGround = bullet::createRigidHull( mWorld, mConvex, Vec3f::one() * 50.0f, 0.0f );
		break;
	case 2:
		mGround = bullet::createRigidMesh( mWorld, mConcave, Vec3f( 10.0f, 1.0f, 10.0f ), 0.0f, 0.0f );
		break;
	case 3:
		mGround = bullet::createRigidBox( mWorld, Vec3f( 200.0f, 35.0f, 200.0f ), 0.0f );
		break;
	case 4:
		mGround = bullet::createRigidBox( mWorld, Vec3f( 200.0f, 35.0f, 200.0f ), 0.0f );
		break;
	case 5:
		heightField = Channel32f( loadImage( loadResource( RES_IMAGE_HEIGHTFIELD_SM ) ) );
		mGround = bullet::createRigidTerrain( mWorld, heightField, -1.0f, 1.0f, Vec3f( 6.0f, 80.0f, 6.0f ), 0.0f );
		break;
	case 6:

		// ADVANCED: To add a custom class, create a standard pointer to it and 
		// pushBack it into your world. Be sure to delete this pointer when you no loinger need it
		heightField = Channel32f( loadImage( loadResource( RES_IMAGE_HEIGHTFIELD ) ) );
		mTerrain = new DynamicTerrain( heightField, -1.0f, 1.0f, Vec3f( 2.0f, 50.0f, 2.0f ), 0.0f );
		mWorld->pushBack( mTerrain );
		break;

	case 7:

		// Start capture
		if ( !mCapture ) {
			mCapture = Capture( 320, 240 );
			mCapture.start();
		}

		break;

	}

	// Set friction for box
	if ( mTest < 5 ) {
		btRigidBody* boxBody = bullet::toBulletRigidBody( mGround );
		boxBody->setFriction( 0.95f );
	}

}
예제 #2
0
void BulletTestApp::update()
{

	// Run next test
	if ( mTest != mTestPrev ) {
		mTestPrev = mTest;
		initTest();
	}

	mFrameRate = getAverageFps();

	// Update light
	mLight->update( mCamera );

	if ( mTest < 3 ) {

		// Set box rotation
		float rotation = math<float>::sin( ( float )getElapsedSeconds() * 0.3333f ) * 0.35f	;
		mGroundTransform.setRotation( btQuaternion( 0.25f, 0.0f, 1.0f + rotation * 0.1f, rotation ) );
		mGroundTransform.setOrigin( btVector3( 0.0f, -60.0f, 0.0f ) );

		// Apply rotation to box
		btRigidBody* body = bullet::toBulletRigidBody( mGround );
		body->getMotionState()->setWorldTransform( mGroundTransform );
		body->setWorldTransform( mGroundTransform );

	} else if ( mTest == 6 ) {

		// Read data
		Channel32f& input = mTerrain->getData();

		// Get image dimensions
		int32_t height = input.getHeight();
		int32_t width = input.getWidth();
		
		// Create output channel
		Channel32f output = Channel32f( width, height );
		
		// Move pixel value over by one
		for ( int32_t y = 0; y < height; y++ ) {
			for ( int32_t x = 0; x < width; x++ ) {
				float value = input.getValue( Vec2i( x, y ) );
				Vec2i position( ( x + 1 ) % width, ( y + 1 ) % height );
				output.setValue( position, value );
			}

		}

		// Copy new data back to original
		input.copyFrom( output, output.getBounds() );

		// Shift texture coordinates to match positions
		vector<Vec2f>& texCoords = mTerrain->getTexCoords();
		Vec2f delta( 1.0f / (float)width, 1.0f / (float)height );
		for ( vector<Vec2f>::iterator uv = texCoords.begin(); uv != texCoords.end(); ++uv ) {
			*uv -= delta;
		}

		// Update terrain VBO
		mTerrain->updateVbo();

	} else if ( mTest == 7 ) {

		bool init = !mSurface;
		
		if ( mCapture.isCapturing() && mCapture.checkNewFrame() ) {
		
			mSurface = mCapture.getSurface();
			ip::flipVertical( &mSurface );

			if ( init ) {
				mTerrain = new DynamicTerrain( Channel32f( 160, 160 ), -1.0f, 1.0f, Vec3f( 2.0f, 70.0f, 2.0f ), 0.0f );
				mWorld->pushBack( mTerrain );
				btRigidBody* terrain = ( btRigidBody* )mTerrain->getBulletBody();
				terrain->setAngularFactor( 0.6f );
				terrain->setFriction( 0.6f );
			} else {
				mTerrain->getData().copyFrom( Channel32f( mSurface ), Area( 0, 0, 160, 160 ) );
				mTerrain->updateVbo();
			}

		}

	}
	
	// Update dynamics world
	mWorld->update( mFrameRate );

	/*if ( mGround ) { 
		Iter iter = mWorld->find( mGround );
		OutputDebugStringA( toString( iter->getPosition().x ).c_str() );
		OutputDebugStringA( "\n" );
	}*/

	// Remove out of bounbds objects
	for ( bullet::Iter object = mWorld->begin(); object != mWorld->end(); ) {
		if ( object != mWorld->begin() && object->getPosition().y < -800.0f ) {
			object = mWorld->erase( object );
		} else {
			++object;
		}
	}

	// Remove objects when count is too high
	uint32_t max = mTest >= 4 ? MAX_OBJECTS_TERRAIN : MAX_OBJECTS;
	if ( mWorld->getNumObjects() > max + 1 ) {
		for ( uint32_t i = 1; i < mWorld->getNumObjects() - MAX_OBJECTS_TERRAIN; i++ ) {
			mWorld->erase( mWorld->begin() + 1 );
		}
	}

}
예제 #3
0
void BulletTestApp::initTest()
{

	// Clean up last test
	if ( mWorld ) {
		mWorld->clear();
		mWorld->getInfo().m_sparsesdf.Reset();
	}
	
	// Used when generating terrain
	Channel32f heightField;

	// Create and add the wobbly box
	btSoftBody* body				= 0;
	btSoftBody::Material* material	= 0;
	mGroundTransform.setIdentity();
	switch ( mTest ) {
	case 0:
		mGround = bullet::createRigidBox( mWorld, Vec3f( 200.0f, 35.0f, 200.0f ), 0.0f );
		bullet::createRigidSphere( mWorld, 50.0f, 0.0f, Vec3f( 0.0f, -50.0f, 0.0f ) );
		break;
	case 1:
		mGround = bullet::createRigidHull( mWorld, mConvex, Vec3f::one() * 50.0f, 0.0f );
		break;
	case 2:
		mGround = bullet::createRigidMesh( mWorld, mConcave, Vec3f( 10.0f, 1.0f, 10.0f ), 0.0f, 0.0f );
		break;
	case 5:
		heightField = Channel32f( loadImage( loadResource( RES_IMAGE_HEIGHTFIELD_SM ) ) );
		mGround = bullet::createRigidTerrain( mWorld, heightField, -1.0f, 1.0f, Vec3f( 6.0f, 80.0f, 6.0f ), 0.0f );
		break;
	case 6:
		// ADVANCED: To add a custom class, create a standard pointer to it and 
		// pushBack it into your world. Be sure to delete this pointer when you no longer need it.
		heightField = Channel32f( loadImage( loadResource( RES_IMAGE_HEIGHTFIELD ) ) );
		mTerrain = new DynamicTerrain( heightField, -1.0f, 1.0f, Vec3f( 2.0f, 50.0f, 2.0f ), 0.0f );
		mWorld->pushBack( mTerrain );
		break;
	case 7:

		// Start capture
		if ( !mCapture ) {
			mCapture = Capture( 320, 240 );
			mCapture.start();
		}

		break;
	case 8:
		mWorld->getInfo().m_gravity = toBulletVector3( Vec3f( 0.0f, -0.5f, 0.0f ) );

		mGround = bullet::createSoftCloth( mWorld, Vec2f::one() * 180.0f, Vec2i( 18, 20 ), SoftCloth::CLOTH_ATTACH_CORNER_ALL, Vec3f( 0.0f, 0.0f, 40.0f ), Quatf( 0.0f, 0.0f, 0.0f, 0.1f ) );
		
		body = toBulletSoftBody( mGround );
		
		body->m_materials[ 0 ]->m_kAST	= 0.2f;
		body->m_materials[ 0 ]->m_kLST	= 0.2f;
		body->m_materials[ 0 ]->m_kVST	= 0.2f;
		
		body->m_cfg.kDF					= 0.9f; 
		body->m_cfg.kSRHR_CL			= 1.0f;
		body->m_cfg.kSR_SPLT_CL			= 0.0f;
		body->m_cfg.collisions			= btSoftBody::fCollision::CL_SS + btSoftBody::fCollision::CL_RS;
		
		body->getCollisionShape()->setMargin( 0.0001f );
		body->setTotalMass( 500.0f );
		body->generateClusters( 0 );

		material						= body->appendMaterial();
		material->m_kAST				= 0.5f;
		material->m_kLST				= 0.5f;
		material->m_kVST				= 1.0f;
		body->generateBendingConstraints( 1, material );

		break;
	case 9:
		heightField = Channel32f( loadImage( loadResource( RES_IMAGE_HEIGHTFIELD_SM ) ) );
		mGround = bullet::createRigidTerrain( mWorld, heightField, -1.0f, 1.0f, Vec3f( 6.0f, 80.0f, 6.0f ), 0.0f );
		break;
	default:
		mGround = bullet::createRigidBox( mWorld, Vec3f( 200.0f, 35.0f, 200.0f ), 0.0f );
		break;
	}

	// Set friction for box
	if ( mTest < 5 ) {
		btRigidBody* boxBody = bullet::toBulletRigidBody( mGround );
		boxBody->setFriction( 0.95f );
	}

}