Пример #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;
}
Пример #2
0
	void cPhysicsMaterialNewton::UpdateMaterials()
	{
		cPhysicsMaterialIterator MatIt = mpWorld->GetMaterialIterator();

		while(MatIt.HasNext())
		{
			cPhysicsMaterialNewton* pMat = static_cast<cPhysicsMaterialNewton*>(MatIt.Next());

			ePhysicsMaterialCombMode frictionMode =   (ePhysicsMaterialCombMode) std::max(mFrictionMode,
				pMat->mFrictionMode);
			ePhysicsMaterialCombMode elasticityMode = (ePhysicsMaterialCombMode) std::max(mElasticityMode,
				pMat->mElasticityMode);

			//If the material is the same do not blend.
			if(pMat == this){
				frictionMode = 	ePhysicsMaterialCombMode_Average;
				elasticityMode = ePhysicsMaterialCombMode_Average;
			}


			NewtonMaterialSetDefaultElasticity(mpNewtonWorld,mlMaterialId,pMat->mlMaterialId,
				Combine(elasticityMode,mfElasticity, pMat->mfElasticity));

			NewtonMaterialSetDefaultFriction(mpNewtonWorld,mlMaterialId,pMat->mlMaterialId,
				Combine(frictionMode,mfStaticFriction, pMat->mfStaticFriction),
				Combine(frictionMode,mfKineticFriction, pMat->mfKineticFriction));

			NewtonMaterialSetContinuousCollisionMode(mpNewtonWorld,mlMaterialId,pMat->mlMaterialId,
													1);

			NewtonMaterialSetCollisionCallback(mpNewtonWorld,mlMaterialId,pMat->mlMaterialId,
				(void*)NULL,BeginContactCallback,ProcessContactCallback);
		}
	}
void MaterialPair::setContactCallback( OgreNewt::ContactCallback* callback )
{
    m_contactcallback = callback;
    if( callback )
    {
        NewtonMaterialSetCollisionCallback( m_world->getNewtonWorld(), id0->getID(), id1->getID(), this,
            collisionCallback_onAABBOverlap,
            collisionCallback_contactsProcess);
    }
    else
    {
        NewtonMaterialSetCollisionCallback( m_world->getNewtonWorld(), id0->getID(), id1->getID(), NULL,
            NULL,
            NULL);
    }
}
Пример #4
0
dNewton::dNewton()
	:m_maxUpdatePerIterations(2)
{
	// create a newton world
	m_world = NewtonCreate();

	// for two way communication between low and high lever, link the world with this class for 
	NewtonWorldSetUserData(m_world, this);

	// set the simplified solver mode (faster but less accurate)
	NewtonSetSolverModel (m_world, 1);

	// by default runs on four micro threads
	NewtonSetThreadsCount(m_world, 4);

	// set the collision copy constructor callback
	NewtonWorldSetCollisionConstructorDestructorCallback (m_world, OnCollisionCopyConstruct, OnCollisionDestructorCallback);

	// use default material to implement traditional "Game style" one side material system
	int defaultMaterial = NewtonMaterialGetDefaultGroupID (m_world);
	NewtonMaterialSetCallbackUserData (m_world, defaultMaterial, defaultMaterial, m_world);
	NewtonMaterialSetCompoundCollisionCallback(m_world, defaultMaterial, defaultMaterial, OnCompoundSubCollisionAABBOverlap);
	NewtonMaterialSetCollisionCallback (m_world, defaultMaterial, defaultMaterial, OnBodiesAABBOverlap, OnContactProcess);

	// add a hierarchical transform manage to update local transforms
	new dNewtonTransformManager (this);

	// set the timer
	ResetTimer();
}
Пример #5
0
        world::world():
            _world(NULL),
            _accum(0),
            _freq(1.0 / 60.0)
        {
            // default to global gravity
            gravity.enabled = true;

            _world = NewtonCreate();
            const float WORLD_SIZE = 1000;
            const vec3 A(-WORLD_SIZE, -WORLD_SIZE, -WORLD_SIZE), B(-A);
            NewtonSetWorldSize(_world, &A.x, &B.x);

            platform(PLAT_OPTMIZED);
            solver(3);
            friction(FRIC_ADAPTIVE);
            threads(2);
            clearCache();

            int mid = NewtonMaterialGetDefaultGroupID(_world);
            // allow bodies to be swept-tested
            NewtonMaterialSetContinuousCollisionMode(_world, mid, mid, 1);
            NewtonMaterialSetCollisionCallback(_world, mid, mid, NULL,
                &beginContactCB, &processContactCB);
        }
void MaterialPair::setContactCallback( OgreNewt::ContactCallback* callback )
{
    // set the material callbacks to the functions inside the ContactCallback class.
    NewtonMaterialSetCollisionCallback( m_world->getNewtonWorld(), id0->getID(), id1->getID(), callback,
                                        callback->contactBegin,
                                        callback->contactProcess,
                                        callback->contactEnd );
}
Пример #7
0
		void tContact::Set(const tMaterialId * id0, const tMaterialId * id1)
		{
			d_assert(mId0 == NULL && mId1 == NULL);

			mId0 = id0;
			mId1 = id1;

			NewtonMaterialSetCollisionCallback(tWorld::Instance()->_getNewtonWorld(), mId0->GetId(), mId1->GetId(),
				this, tContact::_newtonAABBOverlap, tContact::_newtonContactProcess);
		}
Пример #8
0
void Friction (DemoEntityManager* const scene)
{
	// load the skybox
	scene->CreateSkyBox();


	// load the scene from a ngd file format
	char fileName[2048];
	dGetWorkingFileName ("frictionDemo.ngd", fileName);
	scene->LoadScene (fileName);


	// set a default material call back
	NewtonWorld* const world = scene->GetNewton();
	int defaultMaterialID = NewtonMaterialGetDefaultGroupID (world);
	NewtonMaterialSetCollisionCallback (world, defaultMaterialID, defaultMaterialID, UserOnAABBOverlap, UserContactFriction); 
	//NewtonMaterialSetDefaultCollidable(world, defaultMaterialID, defaultMaterialID, 0);

	// 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
	int index = 0;
	for (NewtonBody* body = NewtonWorldGetFirstBody(world); body; body = NewtonWorldGetNextBody(world, body)) {
		dFloat Ixx;
		dFloat Iyy;
		dFloat Izz;
		dFloat mass;
		NewtonBodyGetMass (body, &mass, &Ixx, &Iyy, &Izz);
		if (mass > 0.0f) {
			// use the new instance feature to ass per shape information
			NewtonCollision* const collision = NewtonBodyGetCollision(body);

			// use the collision user data to save the coefficient of friction 
			dFloat coefficientOfFriction = dFloat (index) * 0.03f; 
			NewtonCollisionSetUserData (collision, *((void**)&coefficientOfFriction));

//			DemoEntity* const entity = (DemoEntity*) NewtonBodyGetUserData (body);
//			dVariable* const friction = entity->CreateVariable (FRICTION_VAR_NAME);
//			dAssert (friction);
//			friction->SetValue(dFloat (index) * 0.03f);
			index ++;
		}
	}

	// place camera into position
	dQuaternion rot;
	dVector origin (-70.0f, 10.0f, 0.0f, 0.0f);
	scene->SetCameraMatrix(rot, origin);

//	ExportScene (scene->GetNewton(), "../../../media/test1.ngd");
}
void CreateMateials (NewtonWorld* world, SceneManager* sceneManager)
{
	SoundManager* sndManager;
		
		
	sndManager = sceneManager->GetSoundManager();

	// create the Material IDs, 
	g_floorMaterial = NewtonMaterialCreateGroupID (world);
	g_woodMaterial = NewtonMaterialCreateGroupID (world);
	g_metalMaterial = NewtonMaterialCreateGroupID (world);

	// load the sound effects 
	woodOnFloor.m_sound = sndManager->LoadSound ("boxHit.wav");
	woodOnFloor.m_manager = sndManager;

	metalOnFloor.m_sound = sndManager->LoadSound ("metal.wav");
	metalOnFloor.m_manager = sndManager;

	woodOnMetal.m_sound = sndManager->LoadSound ("metalBox.wav");
	woodOnMetal.m_manager = sndManager;
	
	metalOnMetal.m_sound = sndManager->LoadSound ("metalMetal.wav");
	metalOnMetal.m_manager = sndManager;

	woodOnWood.m_sound = sndManager->LoadSound ("boxBox.wav");
	woodOnWood.m_manager = sndManager;


	//configure the Material interactions
	NewtonMaterialSetCollisionCallback (world, g_woodMaterial, g_floorMaterial, &woodOnFloor, NULL,  GenericContactProcess);
	NewtonMaterialSetCollisionCallback (world, g_metalMaterial, g_floorMaterial, &metalOnFloor, NULL,  GenericContactProcess);
	NewtonMaterialSetCollisionCallback (world, g_metalMaterial, g_woodMaterial, &woodOnMetal, NULL,  GenericContactProcess);

	NewtonMaterialSetCollisionCallback (world, g_woodMaterial, g_woodMaterial, &woodOnWood, NULL,  GenericContactProcess);
	NewtonMaterialSetCollisionCallback (world, g_metalMaterial, g_metalMaterial, &metalOnMetal, NULL,  GenericContactProcess);

}
static void Magnets (NewtonFrame& system, dFloat strength)
{
	NewtonWorld* world;
	LevelPrimitive* scene;

	world = system.m_world;

	// create the sky box and the floor,
	scene = LoadLevelAndSceneRoot (system, "playground.dae", 1);

	dVector posit (0.0f, 0.0f, 0.0f, 1.0f);
	posit.m_y = FindFloor (world, posit.m_x, posit.m_z) + 5.0f;

	InitEyePoint (dVector (1.0f, 0.0f, 0.0f), posit);

	// create a material carrier to colliding with this objects
	int defaultMaterialID;
	int magneticFieldID;
	int magneticPicesID;
	defaultMaterialID = NewtonMaterialGetDefaultGroupID (world);
	magneticFieldID = NewtonMaterialCreateGroupID (world);
	magneticPicesID = NewtonMaterialCreateGroupID (world);

	NewtonMaterialSetCollisionCallback (world, magneticPicesID, magneticFieldID, NULL, NULL, Magnet::MagneticField); 
	

	// create a spherical object to serve are the magnet core
	dMatrix matrix (GetIdentityMatrix());
	matrix.m_posit = posit;
	matrix.m_posit.m_x += 7.0f;
	new Magnet (system, matrix, 20.0f, defaultMaterialID, magneticFieldID, strength);

	// add a material to control the buoyancy
	dVector size (1.0f, 0.25f, 0.5f);
	dVector sphSize (1.0f, 1.0f, 1.0f);
	dVector capSize (1.25f, 0.4f, 1.0f);
	dVector location (cameraEyepoint + cameraDir.Scale (10.0f));

	AddBoxes (&system, 1.0f, location, sphSize, 3, 3, 5.0f, _SPHERE_PRIMITIVE, magneticPicesID);
	AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _BOX_PRIMITIVE, magneticPicesID);
	AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _CONE_PRIMITIVE, magneticPicesID);
	AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _CYLINDER_PRIMITIVE, magneticPicesID);
	AddBoxes (&system, 1.0f, location, capSize, 3, 3, 5.0f, _CAPSULE_PRIMITIVE, magneticPicesID);
	AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _CHAMFER_CYLINDER_PRIMITIVE, magneticPicesID);
	AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _RANDOM_CONVEX_HULL_PRIMITIVE, magneticPicesID);
	AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _REGULAR_CONVEX_HULL_PRIMITIVE, magneticPicesID);

	posit.m_x -= 10.0f;
	InitEyePoint (dVector (1.0f, 0.0f, 0.0f), posit);
}
void CompoundCollision (DemoEntityManager* const scene)
{
	// load the skybox
	scene->CreateSkyBox();

	// load the scene from a ngd file format
//	CreateLevelMesh (scene, "flatPlane.ngd", true);
//	CreateLevelMesh (scene, "playground.ngd", true);
	//	CreateLevelMesh (scene, "sponza.ngd", true);
	CreateHeightFieldTerrain(scene, HEIGHTFIELD_DEFAULT_SIZE, HEIGHTFIELD_DEFAULT_CELLSIZE,
							 1.5f, 0.2f, 200.0f, -50.0f);

	int defaultMaterialID = NewtonMaterialGetDefaultGroupID (scene->GetNewton());

	// set a material callback to get the colliding shape
	NewtonMaterialSetCollisionCallback (scene->GetNewton(), defaultMaterialID, defaultMaterialID, NULL, OnGettingTheCollisionSubShapeFromMaterialCallback);
	NewtonMaterialSetCompoundCollisionCallback(scene->GetNewton(), defaultMaterialID, defaultMaterialID, OnSubShapeAABBOverlapTest);

	dMatrix camMatrix (dRollMatrix(-20.0f * 3.1416f /180.0f) * dYawMatrix(-45.0f * 3.1416f /180.0f));
	dQuaternion rot (camMatrix);
	dVector origin (100.0f, 0.0f, 100.0f, 0.0f);
	dFloat hight = 1000.0f;
	origin = FindFloor (scene->GetNewton(), dVector (origin.m_x, hight, origin .m_z, 0.0f), hight * 2);
	origin.m_y += 10.0f;

	dVector location (origin);
	location.m_x += 40.0f;
	location.m_z += 40.0f;

	// this crash temporarily (I need to make the compound use shape Instance)
	MakeFunnyCompound (scene, location);

	int count = 5;
	dVector size (0.5f, 0.5f, 0.75f, 0.0f);
	dMatrix shapeOffsetMatrix (dGetIdentityMatrix());
	AddPrimitiveArray(scene, 10.0f, location, size, count, count, 5.0f, _BOX_PRIMITIVE, defaultMaterialID, shapeOffsetMatrix);
	AddPrimitiveArray(scene, 10.0f, location, size, count, count, 5.0f, _BOX_PRIMITIVE, defaultMaterialID, shapeOffsetMatrix);
	AddPrimitiveArray(scene, 10.0f, location, size, count, count, 5.0f, _CAPSULE_PRIMITIVE, defaultMaterialID, shapeOffsetMatrix);
	AddPrimitiveArray(scene, 10.0f, location, size, count, count, 5.0f, _CYLINDER_PRIMITIVE, defaultMaterialID, shapeOffsetMatrix);
	AddPrimitiveArray(scene, 10.0f, location, size, count, count, 5.0f, _CONE_PRIMITIVE, defaultMaterialID, shapeOffsetMatrix);


	// place camera into position
//	dQuaternion rot;
//	dVector origin (-40.0f, 10.0f, 0.0f, 0.0f);
	scene->SetCameraMatrix(rot, origin);
	//ExportScene (scene->GetNewton(), "../../../media/test1.ngd");

}
	void PhysWorld3D::SetMaterialCollisionCallback(int firstMaterial, int secondMaterial, AABBOverlapCallback aabbOverlapCallback, CollisionCallback collisionCallback)
	{
		static_assert(sizeof(UInt64) >= 2 * sizeof(int), "Oops");

		auto callbackPtr = std::make_unique<Callback>();
		callbackPtr->aabbOverlapCallback = std::move(aabbOverlapCallback);
		callbackPtr->collisionCallback = std::move(collisionCallback);

		NewtonMaterialSetCollisionCallback(m_world, firstMaterial, secondMaterial, callbackPtr.get(), (callbackPtr->aabbOverlapCallback) ? OnAABBOverlap : nullptr, (callbackPtr->collisionCallback) ? ProcessContact : nullptr);

		UInt64 firstMaterialId(firstMaterial);
		UInt64 secondMaterialId(secondMaterial);

		UInt64 callbackIndex = firstMaterialId << 32 | secondMaterialId;
		m_callbacks[callbackIndex] = std::move(callbackPtr);
	}
Пример #13
0
Error PhysicsWorld::create(AllocAlignedCallback allocCb, void* allocCbData)
{
	Error err = ErrorCode::NONE;

	m_alloc = HeapAllocator<U8>(allocCb, allocCbData);

	// Set allocators
	gAlloc = &m_alloc;
	NewtonSetMemorySystem(newtonAlloc, newtonFree);

	// Initialize world
	m_world = NewtonCreate();
	if(!m_world)
	{
		ANKI_LOGE("NewtonCreate() failed");
		return ErrorCode::FUNCTION_FAILED;
	}

	// Set the simplified solver mode (faster but less accurate)
	NewtonSetSolverModel(m_world, 1);

	// Create scene collision
	m_sceneCollision = NewtonCreateSceneCollision(m_world, 0);
	Mat4 trf = Mat4::getIdentity();
	m_sceneBody = NewtonCreateDynamicBody(m_world, m_sceneCollision, &trf[0]);
	NewtonBodySetMaterialGroupID(m_sceneBody, NewtonMaterialGetDefaultGroupID(m_world));

	NewtonDestroyCollision(m_sceneCollision); // destroy old scene
	m_sceneCollision = NewtonBodyGetCollision(m_sceneBody);

	// Set the post update listener
	NewtonWorldAddPostListener(m_world, "world", this, postUpdateCallback, destroyCallback);

	// Set callbacks
	NewtonMaterialSetCollisionCallback(m_world,
		NewtonMaterialGetDefaultGroupID(m_world),
		NewtonMaterialGetDefaultGroupID(m_world),
		nullptr,
		onAabbOverlapCallback,
		onContactCallback);

	return err;
}
Пример #14
0
void Car::initPhysics() {

    NewtonWorld * nWorld = this->controller->getWorld();
    NewtonCollision* collision;
    float mass = 900.0f;

    vector3df v1 = this->carNode->getBoundingBox().MinEdge;
    vector3df v2 = this->carNode->getBoundingBox().MaxEdge;

    dVector minBox(v1.X, v1.Y, v1.Z);
    dVector maxBox(v2.X, v2.Y, v2.Z);

    dVector size(maxBox - minBox);
    dVector origin((maxBox + minBox).Scale(0.5f));

    size.m_w = 1.0f;
    origin.m_w = 1.0f;

    dMatrix offset(GetIdentityMatrix());
    offset.m_posit = origin;

    collision = NewtonCreateBox(nWorld, size.m_x, size.m_y, size.m_z, 0, &offset[0][0]);

    dVector inertia;

    matrix4 m = this->carNode->getRelativeTransformation();
    NewtonConvexHullModifierSetMatrix(collision, m.pointer());
    NewtonBody * body = NewtonCreateBody(nWorld, collision, m.pointer());
    NewtonBodySetUserData(body, this);
    NewtonConvexCollisionCalculateInertialMatrix(collision, &inertia[0], &origin[0]);
    NewtonBodySetMassMatrix(body, mass, mass * inertia.m_x, mass * inertia.m_y, mass * inertia.m_z);
    NewtonBodySetCentreOfMass(body, &origin[0]);
    NewtonBodySetForceAndTorqueCallback(body, applyCarMoveForce);
    NewtonBodySetTransformCallback(body, applyCarTransform);
    int matId = NewtonMaterialGetDefaultGroupID(nWorld);
    NewtonMaterialSetCollisionCallback(nWorld, matId, matId, this, 0, applyCarCollisionForce);

    NewtonReleaseCollision(nWorld, collision);
    this->setCarBodyAndGravity(body, dVector(0,-10,0,0));
    this->setLocalCoordinates(this->createChassisMatrix());
}
Пример #15
0
void Restitution (DemoEntityManager* const scene)
{
	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 defaultMaterialID = NewtonMaterialGetDefaultGroupID (world);
	NewtonMaterialSetCollisionCallback (world, defaultMaterialID, defaultMaterialID, NULL, UserContactRestitution); 


	CreateLevelMesh (scene, "flatPlane.ngd", 0);

	dVector location (0.0f, 0.0f, 0.0f, 0.0f);
	dVector size (0.5f, 0.5f, 1.0f, 0.0f);

	// create some spheres 
	dVector sphSize (1.0f, 1.0f, 1.0f, 0.0f);
	NewtonCollision* const sphereCollision = CreateConvexCollision (world, offsetMatrix, sphSize, _SPHERE_PRIMITIVE, 0);
	DemoMesh* const sphereMesh = new DemoMesh("sphere", sphereCollision, "smilli.tga", "smilli.tga", "smilli.tga");

	// create some boxes too
	dVector boxSize (1.0f, 0.5f, 2.0f, 0.0f);
	NewtonCollision* const boxCollision = CreateConvexCollision (world, offsetMatrix, boxSize, _BOX_PRIMITIVE, 0);
	DemoMesh* const boxMesh = new DemoMesh("box", boxCollision, "smilli.tga", "smilli.tga", "smilli.tga");


	int zCount = 10;
	dFloat spacing = 4.0f;
	dMatrix matrix (dGetIdentityMatrix());
	dVector origin (matrix.m_posit);
	origin.m_x -= 0.0f;

	// create a simple scene
	for (int i = 0; i < zCount; i ++) {
		dFloat z;
		dFloat x;
		dFloat mass;
		dVector size (1.0f, 0.5f, 2.0f, 0.0f);

		x = origin.m_x;
		z = origin.m_z + (i - zCount / 2) * spacing;

		mass = 1.0f;
		matrix.m_posit = FindFloor (world, dVector (x, 100.0f, z), 200.0f);
		matrix.m_posit.m_w = 1.0f;
		
		float restitution;
		NewtonBody* body;
		NewtonCollision* collision;
		

		matrix.m_posit.m_y += 4.0f;
		body = CreateSimpleSolid (scene, sphereMesh, mass, matrix, sphereCollision, defaultMaterialID);
		NewtonBodySetLinearDamping (body, 0.0f);
		collision = NewtonBodyGetCollision(body);
		restitution = i * 0.1f + 0.083f;
		NewtonCollisionSetUserData (collision, *((void**)&restitution));

		matrix.m_posit.m_y += 4.0f;
		//body = CreateSimpleSolid (scene, sphereMesh, mass, matrix, sphereCollision, defaultMaterialID, shapeOffsetMatrix);
		body = CreateSimpleSolid (scene, boxMesh, mass, matrix, boxCollision, defaultMaterialID);
		NewtonBodySetLinearDamping (body, 0.0f);
		collision = NewtonBodyGetCollision(body);
		restitution = i * 0.1f + 0.083f;
		NewtonCollisionSetUserData (collision, *((void**)&restitution));

		matrix.m_posit.m_y += 4.0f;
		body = CreateSimpleSolid (scene, sphereMesh, mass, matrix, sphereCollision, defaultMaterialID);
		NewtonBodySetLinearDamping (body, 0.0f);
		collision = NewtonBodyGetCollision(body);
		restitution = i * 0.1f + 0.083f;
		NewtonCollisionSetUserData (collision, *((void**)&restitution));

		matrix.m_posit.m_y += 4.0f;
		dVector boxSize (1.0f, 0.5f, 2.0f, 0.0f);
		body = CreateSimpleSolid (scene, boxMesh, mass, matrix, boxCollision, defaultMaterialID);
		NewtonBodySetLinearDamping (body, 0.0f);
		collision = NewtonBodyGetCollision(body);
		restitution = i * 0.1f + 0.083f;
		NewtonCollisionSetUserData (collision, *((void**)&restitution));
	}

	boxMesh->Release();
	sphereMesh->Release();
	NewtonDestroyCollision(boxCollision);
	NewtonDestroyCollision(sphereCollision);


	dMatrix camMatrix (dGetIdentityMatrix());
	dQuaternion rot (camMatrix);
	origin = dVector (-25.0f, 5.0f, 0.0f, 0.0f);
	scene->SetCameraMatrix(rot, origin);

}
Пример #16
0
/***************************
Auteur : Loïc Teyssier

Usage : Charge les modeles des caisses, leur attribue un matériau et 
			une fonction de rappel de collision.
***************************/
CMiniJeuCaisse::CMiniJeuCaisse(NewtonWorld *world, int matPerso)
	: CMiniJeu(4)
{
	m_nWorld = world;
	m_sNameElem = "caisse(s)";

	// TIMER :
	int seconde = 1000; // 1 seconde = 1000 ms
    int timerInterval = seconde / 60; // l'intervalle entre deux images
    t_TimerCaisse = new QTimer(this);
    connect(t_TimerCaisse, SIGNAL(timeout()), this, SLOT(timeOutSlotCaisse()));
    t_TimerCaisse->start( timerInterval );

	//		Caisses :
	m_Caisse1 = new C3DModel();
	m_Caisse2 = new C3DModel();
	m_Caisse3 = new C3DModel();
	m_Caisse4 = new C3DModel();

	m_JeuLoader = new CLoad3DS();
	m_JeuLoader->Import3DS(m_Caisse1,"Models/caisse1.3ds");
	delete m_JeuLoader;
	
	m_JeuLoader = new CLoad3DS();
	m_JeuLoader->Import3DS(m_Caisse2,"Models/caisse2.3ds");
	delete m_JeuLoader;

	m_JeuLoader = new CLoad3DS();
	m_JeuLoader->Import3DS(m_Caisse3,"Models/caisse3.3ds");
	delete m_JeuLoader;

	m_JeuLoader = new CLoad3DS();
	m_JeuLoader->Import3DS(m_Caisse4,"Models/caisse4.3ds");
	delete m_JeuLoader;


	m_Caisse1->LoadTextures();
	m_Caisse2->LoadTextures();
	m_Caisse3->LoadTextures();
	m_Caisse4->LoadTextures();


	m_Caisse1->Initialiser(world,true,200.0);
	m_Caisse2->Initialiser(world,true,200.0);
	m_Caisse3->Initialiser(world,true,200.0);
	m_Caisse4->Initialiser(world,true,200.0);

	
	int matCaisse1 = NewtonMaterialCreateGroupID(world);
	int matCaisse2 = NewtonMaterialCreateGroupID(world);
	int matCaisse3 = NewtonMaterialCreateGroupID(world);
	int matCaisse4 = NewtonMaterialCreateGroupID(world);

	NewtonBodySetMaterialGroupID(m_Caisse1->GetBody(),matCaisse1);
	NewtonBodySetMaterialGroupID(m_Caisse2->GetBody(),matCaisse2);
	NewtonBodySetMaterialGroupID(m_Caisse3->GetBody(),matCaisse3);
	NewtonBodySetMaterialGroupID(m_Caisse4->GetBody(),matCaisse4);
	
	NewtonMaterialSetCollisionCallback (world, matCaisse1, matPerso , NULL, ContactBegin, ContactProcessCaisse1, ContactEnd);
	NewtonMaterialSetCollisionCallback (world, matCaisse2, matPerso , NULL, ContactBegin, ContactProcessCaisse2, ContactEnd);
	NewtonMaterialSetCollisionCallback (world, matCaisse3, matPerso , NULL, ContactBegin, ContactProcessCaisse3, ContactEnd);
	NewtonMaterialSetCollisionCallback (world, matCaisse4, matPerso , NULL, ContactBegin, ContactProcessCaisse4, ContactEnd);

	g_colC.c1 = true;
	g_colC.c2 = true;
	g_colC.c3 = true;
	g_colC.c4 = true;
}
Пример #17
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 );
}
Пример #18
0
		void tContact::Reset()
		{
			d_assert (mId0 && mId1)
			NewtonMaterialSetCollisionCallback(tWorld::Instance()->_getNewtonWorld(), mId0->GetId(), mId1->GetId(), NULL, NULL, NULL);
		}
Пример #19
0
 void iPhysics::setCollisionCallback(iPhysicsMaterialCombo* materialCombo)
 {
     NewtonMaterialSetCollisionCallback(static_cast<const NewtonWorld*>(_defaultWorld), materialCombo->getMaterial0(), materialCombo->getMaterial1(), NULL, GenericContactProcess);
     NewtonMaterialSetCallbackUserData(static_cast<const NewtonWorld*>(_defaultWorld), materialCombo->getMaterial0(), materialCombo->getMaterial1(), materialCombo);
 }
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));
}
Пример #21
0
void SceneCollision (DemoEntityManager* const scene)
{
	NewtonWorld* world = scene->GetNewton();

	// add the Sky
	scene->CreateSkyBox();

	// create a body and with a scene collision
	NewtonCollision* const sceneCollision = NewtonCreateSceneCollision (scene->GetNewton(), 0);

	// create a visual scene empty mesh
	ComplexScene* const visualMesh = new ComplexScene(sceneCollision);
	scene->Append (visualMesh);


	// add some shapes
	visualMesh->AddPrimitives(scene);

	// this is optional, finish the scene construction, optimize the collision scene
	dMatrix matrix (dGetIdentityMatrix());


	// create the level body and add it to the world
	NewtonBody* const level = NewtonCreateDynamicBody (world, sceneCollision, &matrix[0][0]);

	// replace the collision with the newly created one the  
	visualMesh->m_sceneCollision = NewtonBodyGetCollision(level);

	// set the reference to the visual
	NewtonBodySetUserData(level, visualMesh);

	// do not forget to release the collision
	NewtonDestroyCollision (sceneCollision);


	// add few objects
	int defaultMaterialID = NewtonMaterialGetDefaultGroupID (world);
	NewtonMaterialSetCollisionCallback (world, defaultMaterialID, defaultMaterialID, OnBodyAABBOverlap, OnContactCollision); 
	NewtonMaterialSetCompoundCollisionCallback(world, defaultMaterialID, defaultMaterialID, OnSubShapeAABBOverlapTest);



	dVector location (0.0f, 0.0f, 0.0f, 0.0f);
	dVector size (0.5f, 0.5f, 0.5f, 0.0f);
	dMatrix shapeOffsetMatrix (dGetIdentityMatrix());

	int count = 5;
	AddPrimitiveArray(scene, 10.0f, location, size, count, count, 1.7f, _SPHERE_PRIMITIVE, defaultMaterialID, shapeOffsetMatrix);
	AddPrimitiveArray(scene, 10.0f, location, size, count, count, 1.7f, _BOX_PRIMITIVE, defaultMaterialID, shapeOffsetMatrix);
	AddPrimitiveArray(scene, 10.0f, location, size, count, count, 1.7f, _CYLINDER_PRIMITIVE, defaultMaterialID, shapeOffsetMatrix);
	AddPrimitiveArray(scene, 10.0f, location, size, count, count, 1.7f, _CHAMFER_CYLINDER_PRIMITIVE, defaultMaterialID, shapeOffsetMatrix);
	AddPrimitiveArray(scene, 10.0f, location, size, count, count, 1.7f, _CONE_PRIMITIVE, defaultMaterialID, shapeOffsetMatrix);
	AddPrimitiveArray(scene, 10.0f, location, size, count, count, 1.7f, _CAPSULE_PRIMITIVE, defaultMaterialID, shapeOffsetMatrix);
	AddPrimitiveArray(scene, 10.0f, location, size, count, count, 1.7f, _RANDOM_CONVEX_HULL_PRIMITIVE, defaultMaterialID, shapeOffsetMatrix);
	AddPrimitiveArray(scene, 10.0f, location, size, count, count, 1.7f, _COMPOUND_CONVEX_CRUZ_PRIMITIVE, defaultMaterialID, shapeOffsetMatrix);
	

	dMatrix camMatrix (dRollMatrix(-20.0f * 3.1416f /180.0f) * dYawMatrix(-45.0f * 3.1416f /180.0f));
	dQuaternion rot (camMatrix);
	dVector origin (-15.0f, 5.0f, 0.0f, 0.0f);
	scene->SetCameraMatrix(rot, origin);

}
Пример #22
0
// create physics scene
void PuckSlide (DemoEntityManager* const scene)
{
	scene->CreateSkyBox();

	NewtonWorld* const world = scene->GetNewton();

	int	materialGroupIDs[SB_NUM_MATERIALS];

	// Create groups
	for (int i = 0; i < SB_NUM_MATERIALS; i++)
	{
		materialGroupIDs[i] = NewtonMaterialCreateGroupID(world);
	}
	
	// Setup the material data
	NewtonMaterialSetDefaultSoftness(world, materialGroupIDs[SBMaterial_WEIGHT], materialGroupIDs[SBMaterial_SURFACE], 0.15f);
	NewtonMaterialSetDefaultElasticity(world, materialGroupIDs[SBMaterial_WEIGHT], materialGroupIDs[SBMaterial_SURFACE], 0.30f);
	NewtonMaterialSetDefaultFriction(world, materialGroupIDs[SBMaterial_WEIGHT], materialGroupIDs[SBMaterial_SURFACE], 0.05f, 0.04f);


	// setup callbacks for collisions between two material groups
	NewtonMaterialSetCollisionCallback(world,materialGroupIDs[SBMaterial_WEIGHT],materialGroupIDs[SBMaterial_SURFACE],NULL,PhysicsNewton_CollisionPuckSurfaceCB);

	///////
	// Add table
	{
		dVector tableSize(TABLE_LENGTH, TABLE_HEIGHT, TABLE_WIDTH, 0.0f);

		// create the shape and visual mesh as a common data to be re used
		NewtonCollision* const collision = CreateConvexCollision (world, dGetIdentityMatrix(), tableSize, _BOX_PRIMITIVE, materialGroupIDs[SBMaterial_SURFACE]);

		DemoMesh* const geometry = new DemoMesh("cylinder_1", collision, "wood_3.tga", "wood_3.tga", "wood_3.tga");

		dMatrix matrix = dGetIdentityMatrix();
		matrix.m_posit.m_x = 0.0f;
		matrix.m_posit.m_z = 0.0f;
		matrix.m_posit.m_y = 0.0f;
		NewtonBody* const tableBody = CreateSimpleSolid (scene, geometry, 0.0, matrix, collision, materialGroupIDs[SBMaterial_SURFACE]);

		// this is deprecated, use NewtonBodySetMassProperties
		//NewtonBodySetMassMatrix(tableBody, 0.0f, 1.0f, 1.0f, 1.0f);
		NewtonBodySetMassProperties(tableBody, 0.0f, NewtonBodyGetCollision(tableBody));

		NewtonBodySetMaterialGroupID(tableBody, materialGroupIDs[SBMaterial_SURFACE]);

		// it is not wise to se static body to continuous collision mode
		//NewtonBodySetContinuousCollisionMode(tableBody, 1);

		// do not forget to release the assets	
		geometry->Release(); 

		// the collision need to be destroy, the body is using an instance no a reference
		NewtonDestroyCollision (collision);
	}
	///////

	// Add puck
	{
		new PuckEntity (scene, materialGroupIDs[SBMaterial_WEIGHT]);
	}

	// place camera into position
	dMatrix camMatrix (dPitchMatrix(20.0f * 3.1416f /180.0f));
	dQuaternion rot (camMatrix);
	dVector origin (CAMERA_Z, CAMERA_Y, CAMERA_X, 0.0f);
	scene->SetCameraMatrix(rot, origin);
}