예제 #1
0
/*
=============
CMod_PhysicsInit
=============
*/
void CMod_PhysicsInit() {
	Com_Printf("----- Initializing Newton Physics Engine -----\n");
	Com_Printf("Initialized with Newton Version %i.%i \n", NEWTON_MAJOR_VERSION, NEWTON_MINOR_VERSION);
	Com_Printf("----------------------------------------------\n");

	// create the Newton World
	bspModels.clear();
	g_world = NewtonCreate (AllocMemory, FreeMemory);

	// use the standard x86 floating point model  
	// Dushan - the engine will try to use the best possible hardware setting found in the current platform this is the default configuration
	NewtonSetPlatformArchitecture (g_world, 2);

	// Set up default material properties for newton
	defaultMaterialGroup = NewtonMaterialGetDefaultGroupID(g_world);
	NewtonMaterialSetDefaultFriction   (g_world, defaultMaterialGroup, defaultMaterialGroup, 0.9f, 0.5f);
	NewtonMaterialSetDefaultElasticity (g_world, defaultMaterialGroup, defaultMaterialGroup, 0.4f);
	NewtonMaterialSetDefaultSoftness   (g_world, defaultMaterialGroup, defaultMaterialGroup, 0.1f);
	NewtonMaterialSetCollisionCallback (g_world, defaultMaterialGroup, defaultMaterialGroup, NULL, NULL, NULL);
	NewtonMaterialSetDefaultCollidable (g_world, defaultMaterialGroup, defaultMaterialGroup, 1 );

	// configure the Newton world to use iterative solve mode 0
	// this is the most efficient but the less accurate mode
	NewtonSetSolverModel (g_world, 8);
	NewtonSetFrictionModel (g_world, 0);

	g_collision = NULL;
}
// create physics scene
void PrimitiveCollision (DemoEntityManager* const scene)
{
	// load the skybox
	scene->CreateSkyBox();


	// customize the scene after loading
	// set a user friction variable in the body for variable friction demos
	// later this will be done using LUA script
	NewtonWorld* const world = scene->GetNewton();
	dMatrix offsetMatrix (dGetIdentityMatrix());

	int materialID = NewtonMaterialGetDefaultGroupID (world);

	// disable collision
	NewtonMaterialSetDefaultCollidable (world, materialID, materialID, 0);

	AddSinglePrimitive (scene, -10.0f, _SPHERE_PRIMITIVE, materialID);
	AddSinglePrimitive (scene,  -8.0f, _BOX_PRIMITIVE, materialID);
	AddSinglePrimitive (scene,  -6.0f, _CAPSULE_PRIMITIVE, materialID);
	AddSinglePrimitive (scene,  -4.0f, _CYLINDER_PRIMITIVE, materialID);
	AddSinglePrimitive (scene,  -2.0f, _CONE_PRIMITIVE, materialID);
	AddSinglePrimitive (scene,   4.0f, _CHAMFER_CYLINDER_PRIMITIVE, materialID);
	AddSinglePrimitive (scene,   6.0f, _RANDOM_CONVEX_HULL_PRIMITIVE, materialID);
	AddSinglePrimitive (scene,   8.0f, _REGULAR_CONVEX_HULL_PRIMITIVE, materialID);


	// here we will change the standard gravity force callback and apply null and add a 
	for (NewtonBody* body = NewtonWorldGetFirstBody(world); body; body = NewtonWorldGetNextBody(world, body)) {
		DemoEntity* const entity = (DemoEntity*) NewtonBodyGetUserData(body);
		entity->SetUserData (new ShowCollisionCollide(body));
		NewtonBodySetForceAndTorqueCallback(body, PhysicsSpinBody);
		NewtonBodySetAutoSleep (body, 0);
	}

	// place camera into position
	//dMatrix camMatrix (dYawMatrix(90.0f * dDegreeToRad));
	dMatrix camMatrix (dGetIdentityMatrix());
	dQuaternion rot (camMatrix);
	dVector origin (-15.0f, 0.0f, 0.0f, 0.0f);
	scene->SetCameraMatrix(rot, origin);

}
예제 #3
0
void Physics::setupMaterials()
{
    assert( _p_world );

    // get the default material ID
    int defaultID = NewtonMaterialGetDefaultGroupID( _p_world );
    // set default material properties
    NewtonMaterialSetDefaultSoftness( _p_world, defaultID, defaultID, 0.05f );
    NewtonMaterialSetDefaultElasticity( _p_world, defaultID, defaultID, 0.4f );
    NewtonMaterialSetDefaultCollidable( _p_world, defaultID, defaultID, 1 );
    NewtonMaterialSetDefaultFriction( _p_world, defaultID, defaultID, 1.0f, 0.5f );
    NewtonMaterialSetCollisionCallback( _p_world, defaultID, defaultID, &defaultCollStruct, genericContactBegin, genericContactProcess, genericContactEnd );

    // create all predefined materials IDs
    int nocolID = NewtonMaterialCreateGroupID( _p_world );
    int wallID  = NewtonMaterialCreateGroupID( _p_world );
    int levelID = NewtonMaterialCreateGroupID( _p_world );
    int woodID  = NewtonMaterialCreateGroupID( _p_world );
    int metalID = NewtonMaterialCreateGroupID( _p_world );
    int stoneID = NewtonMaterialCreateGroupID( _p_world );
    int grassID = NewtonMaterialCreateGroupID( _p_world );

    _materials.insert( std::make_pair( "default", defaultID ) );
    _materials.insert( std::make_pair( "wall"   , wallID    ) );
    _materials.insert( std::make_pair( "nocol"  , nocolID   ) );
    _materials.insert( std::make_pair( "level"  , levelID   ) );
    _materials.insert( std::make_pair( "wood"   , woodID    ) );
    _materials.insert( std::make_pair( "metal"  , metalID   ) );
    _materials.insert( std::make_pair( "stone"  , stoneID   ) );
    _materials.insert( std::make_pair( "grass"  , grassID   ) );

    // set non-colliding pairs
    NewtonMaterialSetDefaultCollidable( _p_world, nocolID, defaultID, 0 );
    NewtonMaterialSetDefaultCollidable( _p_world, nocolID, wallID,    0 );
    NewtonMaterialSetDefaultCollidable( _p_world, nocolID, levelID,   0 );
    NewtonMaterialSetDefaultCollidable( _p_world, nocolID, woodID,    0 );
    NewtonMaterialSetDefaultCollidable( _p_world, nocolID, metalID,   0 );
    NewtonMaterialSetDefaultCollidable( _p_world, nocolID, grassID,   0 );
    NewtonMaterialSetDefaultCollidable( _p_world, nocolID, stoneID,   0 );

    // set the material properties for level on wood
    NewtonMaterialSetDefaultElasticity( _p_world, levelID, woodID, 0.5f );
    NewtonMaterialSetDefaultFriction  ( _p_world, levelID, woodID, 0.7f, 0.6f);
    NewtonMaterialSetCollisionCallback( _p_world, levelID, woodID, &level_woodCollStruct, genericContactBegin, levelContactProcess, genericContactEnd );

    // set the material properties for level on metal
    NewtonMaterialSetDefaultElasticity( _p_world, levelID, metalID, 0.5f );
    NewtonMaterialSetDefaultFriction  ( _p_world, levelID, metalID, 0.8f, 0.6f );
    NewtonMaterialSetCollisionCallback( _p_world, levelID, metalID, &level_metalCollStruct, genericContactBegin, levelContactProcess, genericContactEnd );

    // set the material properties for level on grass
    NewtonMaterialSetDefaultElasticity( _p_world, levelID, grassID, 0.3f );
    NewtonMaterialSetDefaultFriction  ( _p_world, levelID, grassID, 0.9f, 0.7f );
    NewtonMaterialSetCollisionCallback( _p_world, levelID, grassID, &level_grassCollStruct, genericContactBegin, levelContactProcess, genericContactEnd );

    // set the material properties for level on stone
    NewtonMaterialSetDefaultElasticity( _p_world, levelID, stoneID, 0.45f );
    NewtonMaterialSetDefaultFriction  ( _p_world, levelID, stoneID, 0.8f, 0.7f );
    NewtonMaterialSetCollisionCallback( _p_world, levelID, stoneID, &level_stoneCollStruct, genericContactBegin, levelContactProcess, genericContactEnd );

    //------

    // set the material properties for wood on wood
    NewtonMaterialSetDefaultElasticity( _p_world, woodID, woodID, 0.3f );
    NewtonMaterialSetDefaultFriction  ( _p_world, woodID, woodID, 1.1f, 0.7f);
    NewtonMaterialSetCollisionCallback( _p_world, woodID, woodID, &wood_woodCollStruct, genericContactBegin, genericContactProcess, genericContactEnd );

    // set the material properties for wood on metal
    NewtonMaterialSetDefaultElasticity( _p_world, woodID, metalID, 0.5f );
    NewtonMaterialSetDefaultFriction  ( _p_world, woodID, metalID, 0.8f, 0.6f );
    NewtonMaterialSetCollisionCallback( _p_world, woodID, metalID, &wood_metalCollStruct, genericContactBegin, genericContactProcess, genericContactEnd );

    // set the material properties for wood on grass
    NewtonMaterialSetDefaultElasticity( _p_world, woodID, grassID, 0.2f );
    NewtonMaterialSetDefaultFriction  ( _p_world, woodID, grassID, 0.9f, 0.7f );
    NewtonMaterialSetCollisionCallback( _p_world, woodID, grassID, &wood_grassCollStruct, genericContactBegin, genericContactProcess, genericContactEnd );

    // set the material properties for wood on stone
    NewtonMaterialSetDefaultElasticity( _p_world, woodID, stoneID, 0.45f );
    NewtonMaterialSetDefaultFriction  ( _p_world, woodID, stoneID, 0.7f, 0.6f );
    NewtonMaterialSetCollisionCallback( _p_world, woodID, stoneID, &wood_stoneCollStruct, genericContactBegin, genericContactProcess, genericContactEnd );

    //------

    // set the material properties for metal on metal
    NewtonMaterialSetDefaultElasticity( _p_world, metalID, metalID, 0.7f );
    NewtonMaterialSetDefaultFriction  ( _p_world, metalID, metalID, 0.5f, 0.2f );
    NewtonMaterialSetCollisionCallback( _p_world, metalID, metalID, &metal_metalCollStruct, genericContactBegin, genericContactProcess, genericContactEnd );

    // set the material properties for metal on grass
    NewtonMaterialSetDefaultElasticity( _p_world, metalID, grassID, 0.2f );
    NewtonMaterialSetDefaultFriction  ( _p_world, metalID, grassID, 0.6f, 0.5f );
    NewtonMaterialSetCollisionCallback( _p_world, metalID, grassID, &metal_grassCollStruct, genericContactBegin, genericContactProcess, genericContactEnd );

    // set the material properties for metal on stone
    NewtonMaterialSetDefaultElasticity( _p_world, metalID, stoneID, 0.6f );
    NewtonMaterialSetDefaultFriction  ( _p_world, metalID, stoneID, 0.5f, 0.4f );
    NewtonMaterialSetCollisionCallback( _p_world, metalID, stoneID, &metal_stoneCollStruct, genericContactBegin, genericContactProcess, genericContactEnd );

    //------

    // set the material properties for stone on stone
    NewtonMaterialSetDefaultElasticity( _p_world, stoneID, stoneID, 0.8f );
    NewtonMaterialSetDefaultFriction  ( _p_world, stoneID, stoneID, 0.5f, 0.2f );
    NewtonMaterialSetCollisionCallback( _p_world, stoneID, stoneID, &stone_stoneCollStruct, genericContactBegin, genericContactProcess, genericContactEnd );

    // set the material properties for stone on grass
    NewtonMaterialSetDefaultElasticity( _p_world, stoneID, grassID, 0.2f );
    NewtonMaterialSetDefaultFriction  ( _p_world, stoneID, grassID, 0.7f, 0.6f );
    NewtonMaterialSetCollisionCallback( _p_world, stoneID, grassID, &stone_grassCollStruct, genericContactBegin, genericContactProcess, genericContactEnd );

    //------

    // set the material properties for grass on grass ( may we need this!? )
    NewtonMaterialSetDefaultElasticity( _p_world, grassID, grassID, 0.1f );
    NewtonMaterialSetDefaultFriction  ( _p_world, grassID, grassID, 0.9f, 0.8f );
    NewtonMaterialSetCollisionCallback( _p_world, grassID, grassID, &grass_grassCollStruct, genericContactBegin, genericContactProcess, genericContactEnd );
}
예제 #4
0
		void tContact::SetCollidable(int state)
		{
			NewtonMaterialSetDefaultCollidable(tWorld::Instance()->_getNewtonWorld(), mId0->GetId(), mId1->GetId(), state);
		}
static void BuildFloorAndSceneRoot (SceneManager& system)
{
	NewtonWorld* world;
	RenderPrimitive* floor;
	NewtonBody* floorBody;
	NewtonCollision* floorCollision;
	OGLMesh* meshInstance;

	world = system.m_world;
	// /////////////////////////////////////////////////////////////////////
	//
	// create the sky box,
	system.AddModel (new SkyBox ());


	// create the the floor graphic objects
	dVector floorSize (100.0f, 2.0f, 100.0f);
	dMatrix location (GetIdentityMatrix());
	location.m_posit.m_y = -5.0f; 

	// create a box for floor 
	floorCollision = NewtonCreateBox (world, floorSize.m_x, floorSize.m_y, floorSize.m_z, 0, NULL); 

	//	meshInstance = OGLMesh::MakeBox (world, size.m_x, size.m_y, size.m_z, "GrassAndDirt.tga");
	meshInstance = new OGLMesh (floorCollision, "GrassAndDirt.tga", "metal_30.tga", "metal_30.tga");
	floor = new RenderPrimitive (location, meshInstance);
	system.AddModel (floor);
	meshInstance->Release();

	// create the the floor collision, and body with default values
	floorBody = NewtonCreateBody (world, floorCollision);
	NewtonReleaseCollision (world, floorCollision);


	// set the transformation for this rigid body
	NewtonBodySetMatrix (floorBody, &location[0][0]);

	// save the pointer to the graphic object with the body.
	NewtonBodySetUserData (floorBody, floor);

	// set a destructor for this rigid body
	NewtonBodySetDestructorCallback (floorBody, PhysicsBodyDestructor);


	// get the default material ID
	int defaultID;
	defaultID = NewtonMaterialGetDefaultGroupID (world);

	// set default material properties
	NewtonMaterialSetDefaultSoftness (world, defaultID, defaultID, 0.05f);
	NewtonMaterialSetDefaultElasticity (world, defaultID, defaultID, 0.4f);
	NewtonMaterialSetDefaultCollidable (world, defaultID, defaultID, 1);
	NewtonMaterialSetDefaultFriction (world, defaultID, defaultID, 1.0f, 0.5f);
	NewtonMaterialSetCollisionCallback (world, defaultID, defaultID, NULL, NULL, GenericContactProcess); 

	//	NewtonMaterialSetSurfaceThickness(world, materialID, materialID, 0.1f);
	NewtonMaterialSetSurfaceThickness(world, defaultID, defaultID, 0.0f);

	// set the island update callback
	NewtonSetIslandUpdateEvent (world, PhysicsIslandUpdate);

	// save the callback
	SetDemoCallbacks (system);

	InitEyePoint (dVector (1.0f, 0.0f, 0.0f), dVector (-40.0f, 10.0f, 0.0f));
}
	void PhysWorld3D::SetMaterialDefaultCollidable(int firstMaterial, int secondMaterial, bool collidable)
	{
		NewtonMaterialSetDefaultCollidable(m_world, firstMaterial, secondMaterial, collidable);
	}