Exemplo n.º 1
0
CollisionBody::CollisionBody(glm::vec3 passPosition, int type)
{
    trans.setIdentity();
    trans.setOrigin(btVector3(0,0,0));
    CurrentPosition.x = passPosition.x;
    CurrentPosition.y = passPosition.y;
    CurrentPosition.z = passPosition.z;
    swap = 0;

    if(type == 1)
    {
        cout<<"type 1";
        CreateRigidBody(passPosition, 2.5);
    }
    else if(type == 3)
    {
        //createplane();
    }
    else
    {
        cout<<"type2";
        CreateRigidBody(passPosition, 10,10,10);
    }

}
Exemplo n.º 2
0
void CreateScene (NewtonWorld* world, SceneManager* sceneManager)
{
	Entity* floor;
	NewtonBody* floorBody;
	NewtonCollision* shape;

	// initialize the camera
	InitCamera (dVector (-15.0f, 5.0f, 0.0f, 0.0f), dVector (1.0f, 0.0f, 0.0f));

	// Create a large body to be the floor
	floor = sceneManager->CreateEntity();
	floor->LoadMesh ("FloorBox.dat");

	// add static floor Physics
	shape = CreateNewtonBox (world, floor, 0);
	floorBody = CreateRigidBody (world, floor, shape, 0.0f);
	NewtonDestroyCollision(shape);

	// set the Transformation Matrix for this rigid body
	dMatrix matrix (floor->m_curRotation, floor->m_curPosition);
	NewtonBodySetMatrix (floorBody, &matrix[0][0]);


	// find the floor base, and add some distance up;
	dFloat floorY = FindFloor (world, 0.0f, 0.0f) + 2.0f;

	// this is the width of the Box;
	dFloat boxWidth = 0.42f;

	// Make a small pyramid of Boxes
	for (int i = 0; i < PYRAMID_BASE + 1; i ++) {
		for (int j = 0; j < PYRAMID_BASE - i; j ++) {
			Entity* smilly;
			NewtonBody* smillyBody;

			// crate a visual Box and place in a pyramid formation above the floor
			smilly = sceneManager->CreateEntity();
			smilly->LoadMesh ("Smilly.dat");
			smilly->m_curPosition.m_x = 0.0f;
			smilly->m_curPosition.m_z = dFloat (j) * boxWidth + dFloat (i) * (boxWidth * 0.5f);
			smilly->m_curPosition.m_y = floorY + dFloat (i) * boxWidth;
			smilly->m_prevPosition = smilly->m_curPosition;

			// add a body with a box shape
			shape = CreateNewtonBox (world, smilly, 0);
			smillyBody = CreateRigidBody (world, smilly, shape, 10.0f);
			NewtonDestroyCollision(shape);

			// we want some nice object placement, with zero penetration and and zero jitter
			// therefore we are going use use a Convex Cast function to snap the Box to the closest contact surface
			ConvexCastPlacement (smillyBody);
		}
	}
}
Exemplo n.º 3
0
int CcdChapter::Init()
{
	CreateScene();

	//	load resource
	LoadResource();

	CreateBullet();

	Plane plane(Vector3::UNIT_Y, 0);
	MeshManager::getSingleton().createPlane("ground", ResourceGroup, plane, 500, 500, 1, 1, true, 1, 5, 5, Vector3::UNIT_Z);

	Entity *groundEntity = m_pkSceneManager->createEntity("GroundEntity", "ground");
	groundEntity->setMaterialName("BulletPlane", ResourceGroup);
	groundEntity->setCastShadows(true);
	SceneNode *pkNode = m_pkRootNode->createChildSceneNode();
	pkNode->attachObject(groundEntity);

	btCollisionShape *shape = CreateCollisionShape(SHAPE_TYPE::SHAPE_PLANE, Vector3::ZERO);
	m_CollisionShapes.push_back(shape);
	btRigidBody *body = CreateRigidBody(shape, false, pkNode, 100);
	m_RigidBodies.push_back(body);
	m_pkDynamicsWorld->addRigidBody(body);
	body->setUserPointer(pkNode);
	//body->setFriction(100);



	return 0;
}
Exemplo n.º 4
0
	void LoadTetrahedraCube(DemoEntityManager* const scene, int materialID)
	{
		dFloat mass = 5.0f;
		NewtonWorld* const world = scene->GetNewton();

		char name[2048];
		dGetWorkingFileName ("box.tet", name);
		NewtonMesh* const tetraCube = NewtonMeshLoadTetrahedraMesh(scene->GetNewton(), name);

		dMatrix aligmentUV(dGetIdentityMatrix());
		int material = LoadTexture("smilli.tga");
		NewtonMeshApplyBoxMapping(tetraCube, material, material, material, &aligmentUV[0][0]);
		NewtonMeshCalculateVertexNormals(tetraCube, 60.0f * dDegreeToRad);

		// make a deformable collision mesh
		NewtonCollision* const deformableCollision = NewtonCreateDeformableSolid(world, tetraCube, materialID);

		//create a rigid body with a deformable mesh
		m_body = CreateRigidBody(scene, mass, deformableCollision);

		// create the soft body mesh
		//m_mesh = new TetrahedraSoftMesh(tetraCube, m_body);
		DemoMesh* const mesh = new TetrahedraSoftMesh(scene, tetraCube, m_body);
		SetMesh(mesh, dGetIdentityMatrix());

		// do not forget to destroy this objects, else you get bad memory leaks.
		mesh->Release ();
		NewtonDestroyCollision(deformableCollision);
		NewtonMeshDestroy(tetraCube);
	}
Exemplo n.º 5
0
	void BuildRegularTetrahedra (DemoEntityManager* const scene, int materialID)
	{
		dFloat mass = 5.0f;
		NewtonWorld* const world = scene->GetNewton();

		dVector tetra[] = { dVector(-1.0f, 0.0f, -0.71f, 0.0f),
							dVector(1.0f, 0.0f, -0.71f, 0.0f),
							dVector(0.0f, -1.0f, 0.71f, 0.0f),
							dVector(0.0f, 1.0f, 0.71f, 0.0f) };

		NewtonMesh* const tetrahedra = NewtonMeshCreate(scene->GetNewton());
		NewtonMeshBeginBuild(tetrahedra);
			AddTetra (tetrahedra, 0, 1, 2, 3, tetra, 0);
		NewtonMeshEndBuild(tetrahedra);

		dMatrix aligmentUV(dGetIdentityMatrix());
		int material = LoadTexture("smilli.tga");
		NewtonMeshApplyBoxMapping (tetrahedra, material, material, material, &aligmentUV[0][0]);
		NewtonMeshCalculateVertexNormals (tetrahedra, 60.0f * dDegreeToRad);

		// make a deformable collision mesh
		NewtonCollision* const deformableCollision = NewtonCreateDeformableSolid(world, tetrahedra, materialID);

		//create a rigid body with a deformable mesh
		m_body = CreateRigidBody (scene, mass, deformableCollision);

		// create the soft body mesh
		DemoMesh* const mesh = new TetrahedraSoftMesh(scene, tetrahedra, m_body);
		SetMesh(mesh, dGetIdentityMatrix());

		// do not forget to destroy this objects, else you get bad memory leaks.
		mesh->Release ();
		NewtonMeshDestroy (tetrahedra);
		NewtonDestroyCollision(deformableCollision);
	}
Exemplo n.º 6
0
//	LinkGeometry()
//	this function will link the geometry to the correct rigid body object
//	it will search up in the tree until it find a valid rigid object
//	then it will make its geometry transformed relative to it
//	this will add the geometry (and the mass) to it
void VDSGeom::LinkGeometry()
{
    //	if this is a root.. create a body
    if ( m_pParent == NULL )
    {
        CreateRigidBody();
    }
}
Exemplo n.º 7
0
NewtonBody* CPhysics::CreateBox(NewtonWorld *world, CObject3D *object, float x, float y, float z, float mass, Vector3 offset)
{
	Matrix4 offsetMat;
	offsetMat.SetPosition(offset);
	float m[16];
	offsetMat.FlattenToArray(m);

	NewtonCollision *collision = NewtonCreateBox(world, x, y, z, 0, m);
	return CreateRigidBody(world, object, collision, mass);	
}
Exemplo n.º 8
0
CollisionBody::CollisionBody(glm::vec3 passPosition,float length, float width, float height)
{
    trans.setIdentity();
    trans.setOrigin(btVector3(0,0,0));
    CurrentPosition.x = passPosition.x;
    CurrentPosition.y = passPosition.y;
    CurrentPosition.z = passPosition.z;
    swap = 0;
    CreateRigidBody(passPosition, length,width,height);
}
Exemplo n.º 9
0
// Constructor
_Collision::_Collision(const SpawnStruct &Object)
:	_Object(),
	TriangleIndexVertexArray(NULL),
	TriangleInfoMap(NULL),
	VertexList(NULL),
	FaceList(NULL) {
	
	gContactAddedCallback = CustomMaterialCallback;

	// Load collision mesh file
	_File MeshFile;
	if(MeshFile.OpenForRead(Object.Template->CollisionFile.c_str())) {

		// Read header
		int VertCount = MeshFile.ReadInt();
		int FaceCount = MeshFile.ReadInt();

		// Allocate memory for lists
		VertexList = new float[VertCount * 3];
		FaceList = new int[FaceCount * 3];

		// Read vertices
		int VertexIndex = 0;
		for(int i = 0; i < VertCount; i++) {
			VertexList[VertexIndex++] = MeshFile.ReadFloat();
			VertexList[VertexIndex++] = MeshFile.ReadFloat();
			VertexList[VertexIndex++] = -MeshFile.ReadFloat();
		}

		// Read faces
		int FaceIndex = 0;
		for(int i = 0; i < FaceCount; i++) {
			FaceList[FaceIndex+2] = MeshFile.ReadInt();
			FaceList[FaceIndex+1] = MeshFile.ReadInt();
			FaceList[FaceIndex+0] = MeshFile.ReadInt();
			FaceIndex += 3;
		}

		// Create triangle array
		TriangleIndexVertexArray = new btTriangleIndexVertexArray(FaceCount, FaceList, 3 * sizeof(int), VertCount * 3, VertexList, 3 * sizeof(float));

		// Create bvh shape
		btBvhTriangleMeshShape *Shape = new btBvhTriangleMeshShape(TriangleIndexVertexArray, true);
		TriangleInfoMap = new btTriangleInfoMap();
		btGenerateInternalEdgeInfo(Shape, TriangleInfoMap);

		// Create physics body
		CreateRigidBody(Object, Shape);
		SetProperties(Object);
		
		RigidBody->setCollisionFlags(RigidBody->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);

		MeshFile.Close();
	}
}
Exemplo n.º 10
0
// Constructor
_Player::_Player(const SpawnStruct &Object)
:	_Object(),
	Sound(NULL),
	Camera(NULL),
	Light(NULL),
	JumpTimer(0.0f),
	TorqueFactor(4.0f) {

	// Graphics
	Node = irrScene->addSphereSceneNode(Object.Template->Radius, 24);
	Node->setMaterialTexture(0, irrDriver->getTexture("textures/player_outer0.png"));
	Node->setMaterialType(EMT_TRANSPARENT_ADD_COLOR);
	Node->setMaterialFlag(EMF_LIGHTING, false);
	Node->setMaterialType(EMT_ONETEXTURE_BLEND);
	Node->getMaterial(0).MaterialTypeParam = pack_textureBlendFunc(EBF_ONE, EBF_ONE);

	// Emit Light
	if(Object.Template->EmitLight) {
		Light = irrScene->addLightSceneNode(0, vector3df(Object.Position[0], Object.Position[1], Object.Position[2]), video::SColorf(1.0f, 1.0f, 1.0f), 15.0f);
		Light->getLightData().Attenuation.set(0.5f, 0.05f, 0.05f);
		Light->getLightData().DiffuseColor.set(1.0f, 0.75f, 0.75f, 1.0f);
	}

	// Add glow
	ISceneNode *InnerNode;
	InnerNode = irrScene->addBillboardSceneNode(Node, dimension2df(1.5f, 1.5f));
	InnerNode->setMaterialFlag(EMF_LIGHTING, false);
	InnerNode->setMaterialFlag(EMF_ZBUFFER, false);
	InnerNode->setMaterialTexture(0, irrDriver->getTexture("textures/player_glow0.png"));
	InnerNode->setMaterialType(EMT_ONETEXTURE_BLEND);
	InnerNode->getMaterial(0).MaterialTypeParam = pack_textureBlendFunc(EBF_ONE, EBF_ONE);

	if(Physics.IsEnabled()) {

		// Create shape
		btSphereShape *Shape = new btSphereShape(Object.Template->Radius);

		// Set up physics
		CreateRigidBody(Object, Shape);
		RigidBody->setSleepingThresholds(0.1f, 0.1f);

		// Audio
		Sound = new _AudioSource(Audio.GetBuffer("player.ogg"), true, 0.0, 0.50f);
		Sound->SetPosition(Object.Position[0], Object.Position[1], Object.Position[2]);
		Sound->Play();
	}

	SetProperties(Object);
	Hookable = false;
	if(CollisionCallback == "")
		CollisionCallback = "OnHitPlayer";
}
Exemplo n.º 11
0
void BulletDynamicsBody::Initialize( const Integration::DynamicsBodySettings& settings, Integration::DynamicsShape* shape,
                                     Integration::DynamicsWorld& dynamicsWorld,
                                     const Vector3& startPosition, const Quaternion& startRotation )
{
  DEBUG_PRINTF("%s type:%d\n", __func__, settings.type );

  mSettings = new Integration::DynamicsBodySettings( settings );
  mWorld = static_cast< BulletDynamicsWorld* >( &dynamicsWorld );

  if( Dali::DynamicsBodyConfig::RIGID == mSettings->type )
  {
    CreateRigidBody( settings, shape, startPosition, startRotation );
  }
  else
  {
    CreateSoftBody( settings, shape, startPosition, startRotation );
  }
}
Exemplo n.º 12
0
	void CreateTetrahedraPrimitive(DemoEntityManager* const scene, int materialID)
	{
		dFloat mass = 5.0f;
		dVector size (1.0f);

		NewtonWorld* const world = scene->GetNewton();
		NewtonCollision* const primitiveShape = CreateConvexCollision (world, dGetIdentityMatrix(), size, _SPHERE_PRIMITIVE, materialID);
		//NewtonCollision* const primitiveShape = CreateConvexCollision (world, dGetIdentityMatrix(), size, _BOX_PRIMITIVE, materialID);
		NewtonMesh* const skinMesh = NewtonMeshCreateFromCollision(primitiveShape);

		dMatrix aligmentUV(dGetIdentityMatrix());
		int material = LoadTexture("smilli.tga");
		NewtonMeshApplySphericalMapping(skinMesh, material, &aligmentUV[0][0]);

		// now now make an tetrahedra iso surface approximation of this mesh
		NewtonMesh* const tetraIsoSurface = NewtonMeshCreateTetrahedraIsoSurface(skinMesh);

		// calculate the linear blend weight for the tetrahedra mesh
		NewtonCreateTetrahedraLinearBlendSkinWeightsChannel (tetraIsoSurface, skinMesh);
		NewtonDestroyCollision(primitiveShape);

		// make a deformable collision mesh
		NewtonCollision* const deformableCollision = NewtonCreateDeformableSolid(world, tetraIsoSurface, materialID);

		//create a rigid body with a deformable mesh
		m_body = CreateRigidBody(scene, mass, deformableCollision);

		// create the soft body mesh
		//DemoMesh* const mesh = new TetrahedraSoftMesh(tetraIsoSurface, m_body);
		DemoMesh* const mesh = new LinearBlendMeshTetra(scene, skinMesh, m_body);
		SetMesh(mesh, dGetIdentityMatrix());
		
		// do not forget to destroy this objects, else you get bad memory leaks.
		mesh->Release ();
		NewtonMeshDestroy(skinMesh);
		NewtonDestroyCollision(deformableCollision);
		NewtonMeshDestroy(tetraIsoSurface);
	}
Exemplo n.º 13
0
void CreateScene (NewtonWorld* world, SceneManager* sceneManager)
{
	Entity* floor;
	Entity* smilly;
	Entity* frowny;
	NewtonBody* floorBody;
	NewtonBody* smillyBody;
	NewtonBody* frownyBody;
	NewtonCollision* shape;

	// Create the material for this scene
	CreateMateials (world, sceneManager);

	// Create a large body to be the floor
	floor = sceneManager->CreateEntity();

	// add scene collision from a level mesh
	shape = CreateHeightFieldCollision (world, "h2.raw", 0);
	floorBody = CreateRigidBody (world, floor, shape, 0.0f);
	NewtonReleaseCollision (world, shape);

	// make a visual mesh for the collision data
	CreateHeightFieldMesh (shape, floor);

	// set the matrix at the origin
	dVector boxP0; 
	dVector boxP1; 
	dMatrix matrix (floor->m_curRotation, floor->m_curPosition);
	NewtonCollisionCalculateAABB (shape, &matrix[0][0], &boxP0.m_x, &boxP1.m_x); 

	// place the origin of the visual mesh at the center of the height field
	matrix.m_posit = (boxP0 + boxP1).Scale (-0.5f);
	matrix.m_posit.m_w = 1.0f;
	floor->m_curPosition = matrix.m_posit;
	floor->m_prevPosition = matrix.m_posit;

	// relocate the body;
	NewtonBodySetMatrix (floorBody, &matrix[0][0]);

	// now we will use the properties of this body to set a proper world size.
	NewtonCollisionCalculateAABB (shape, &matrix[0][0], &boxP0.m_x, &boxP1.m_x); 

	// add some extra padding
	boxP0.m_x -=  50.0f;
	boxP0.m_y -= 500.0f;
	boxP0.m_z -=  50.0f;

	boxP1.m_x +=  50.0f;
	boxP1.m_y += 500.0f;
	boxP1.m_z +=  50.0f;

	// set the new world size
	NewtonSetWorldSize (world, &boxP0[0], &boxP1[0]);


	// assign an Material ID to this body
	NewtonBodySetMaterialGroupID (floorBody, g_floorMaterial);


	// add some visual entities.
	dFloat y0 = FindFloor (world, 0.0f, 0.0f) + 10.0f;
	for (int i = 0; i < 5; i ++) {
		smilly = sceneManager->CreateEntity();
		smilly->LoadMesh ("Smilly.dat");
		smilly->m_curPosition.m_y = y0;
		y0 += 2.0f;
		smilly->m_prevPosition = smilly->m_curPosition;

		// add a body with a box shape
		shape = CreateNewtonBox (world, smilly, 0);
		smillyBody = CreateRigidBody (world, smilly, shape, 10.0f);
		NewtonReleaseCollision (world, shape);

		// assign an Material ID to this body
		NewtonBodySetMaterialGroupID (smillyBody, g_metalMaterial);
	}


	// add some visual entities.
	y0 = FindFloor (world, 0.0f, 0.4f) + 10.5f;
	for (int i = 0; i < 5; i ++) {
		frowny = sceneManager->CreateEntity();
		frowny->LoadMesh ("Frowny.dat");
		frowny->m_curPosition.m_z = 0.4f;
		frowny->m_curPosition.m_y = y0;
		y0 += 2.0f;
		frowny->m_prevPosition = frowny->m_curPosition;

		// add a body with a Convex hull shape
		shape = CreateNewtonConvex (world, frowny, 0);
		frownyBody = CreateRigidBody (world, frowny, shape, 10.0f);
		NewtonReleaseCollision (world, shape);

		// assign an Material ID to this body
		NewtonBodySetMaterialGroupID (frownyBody, g_woodMaterial);
	}

	// set the Camera EyePoint close to the scene action
	InitCamera (dVector (-15.0f, FindFloor (world, -15.0f, 0.0f) + 5.0f, 0.0f), dVector (1.0f, 0.0f, 0.0f));
}
Exemplo n.º 14
0
void CreateScene (NewtonWorld* world, SceneManager* sceneManager)
{
	Entity* floor;
	Entity* smilly;
	Entity* frowny;
	NewtonBody* floorBody;
	NewtonBody* smillyBody;
	NewtonBody* frownyBody;
	NewtonCollision* shape;



////////////////////////////////////////////////////////////////////////////////
// STATIC BODY 1 - FLOOR 
////////////////////////////////////////////////////////////////////////////////

	// Create a large body to be the floor
	floor = sceneManager->CreateEntity();
	floor->LoadMesh ("FloorBox.dat");
 
	// add static floor Physics
	shape = CreateNewtonBox (world, floor, 0);
	floorBody = CreateRigidBody (world, floor, shape, 0.0f);
	NewtonReleaseCollision (world, shape);
 
	// set the Transformation Matrix for this rigid body
	dMatrix matrix (floor->m_curRotation, floor->m_curPosition);
	NewtonBodySetMatrix (floorBody, &matrix[0][0]);
 
	// now we will use the properties of this body to set a proper world size.
	dVector minBox;
	dVector maxBox;
	NewtonCollisionCalculateAABB (shape, &matrix[0][0], &minBox[0], &maxBox[0]);
 
	// add some extra padding
	minBox.m_x -=  50.0f;
	minBox.m_y -= 500.0f;
	minBox.m_z -=  50.0f;
 
	maxBox.m_x +=  50.0f;
	maxBox.m_y += 500.0f;
	maxBox.m_z +=  50.0f;
 
	// set the new world size
	NewtonSetWorldSize (world, &minBox[0], &maxBox[0]);


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////




 
////////////////////////////////////////////////////////////////////////////////
// DYNAMIC BODY 1
////////////////////////////////////////////////////////////////////////////////
 
	// add some visual entities.
	smilly = sceneManager->CreateEntity();
	smilly->LoadMesh ("Smilly.dat");
	smilly->m_curPosition.m_y = 10.0f;
	smilly->m_prevPosition = smilly->m_curPosition;
 
	// add a body with a box shape
	shape = CreateNewtonBox (world, smilly, 0);
	smillyBody = CreateRigidBody (world, smilly, shape, 10.0f);
	NewtonReleaseCollision (world, shape);
 
 
////////////////////////////////////////////////////////////////////////////////
// DYNAMIC BODY 2
////////////////////////////////////////////////////////////////////////////////
 
	// add some visual entities.
	frowny = sceneManager->CreateEntity();
	frowny->LoadMesh ("Frowny.dat");
	frowny->m_curPosition.m_z = 0.4f;
	frowny->m_curPosition.m_y = 10.0f + 0.4f;
	frowny->m_prevPosition = frowny->m_curPosition;
 
	// add a body with a Convex hull shape
	shape = CreateNewtonConvex (world, frowny, 0);
	frownyBody = CreateRigidBody (world, frowny, shape, 10.0f);
	NewtonReleaseCollision (world, shape);
}
Exemplo n.º 15
0
void CreateScene (NewtonWorld* world, SceneManager* sceneManager)
{
    Entity* floor;
    NewtonBody* floorBody;
    NewtonCollision* shape;

    /*
    	void* materialManager;
    	SoundManager* sndManager;
    	PhysicsMaterialInteration matInterations;

    	sndManager = sceneManager->GetSoundManager();

    	// Create the material for this scene, and attach it to the Newton World
    	materialManager = CreateMaterialManager (world, sndManager);

    	// add the Material table
    	matInterations.m_restitution = 0.6f;
    	matInterations.m_staticFriction = 0.6f;
    	matInterations.m_kineticFriction = 0.3f;
    	matInterations.m_scrapingSound = NULL;

    	matInterations.m_impactSound = sndManager->LoadSound ("metalMetal.wav");
    	AddMaterilInteraction (materialManager, m_metal, m_metal, &matInterations);

    	matInterations.m_impactSound = sndManager->LoadSound ("boxBox.wav");
    	AddMaterilInteraction (materialManager, m_wood, m_wood, &matInterations);

    	matInterations.m_impactSound = sndManager->LoadSound ("metalBox.wav");
    	AddMaterilInteraction (materialManager, m_metal, m_wood, &matInterations);

    	matInterations.m_impactSound = sndManager->LoadSound ("grass0.wav");
    	AddMaterilInteraction (materialManager, m_wood, m_grass, &matInterations);

    	matInterations.m_impactSound = sndManager->LoadSound ("boxHit.wav");
    	AddMaterilInteraction (materialManager, m_wood, m_bricks, &matInterations);

    	matInterations.m_impactSound = sndManager->LoadSound ("grass1.wav");
    	AddMaterilInteraction (materialManager, m_metal, m_grass, &matInterations);

    	matInterations.m_impactSound = sndManager->LoadSound ("metal.wav");
    	AddMaterilInteraction (materialManager, m_metal, m_bricks, &matInterations);
    */


    // Create a large body to be the floor
    floor = sceneManager->CreateEntity();
    int materialMap[] = {m_bricks, m_grass, m_wood,	m_metal};

#ifdef USE_HEIGHT_FIELD_LEVEL

    // add scene collision from a level m*esh
    shape = CreateHeightFieldCollision (world, "h2.raw", materialMap);
    floorBody = CreateRigidBody (world, floor, shape, 0.0f);
    NewtonDestroyCollision(shape);

    // make a visual mesh for the collision data
    CreateHeightFieldMesh (shape, floor);

    // set the matrix at the origin
    dVector boxP0;
    dVector boxP1;
    dMatrix matrix (floor->m_curRotation, floor->m_curPosition);
    NewtonCollisionCalculateAABB (shape, &matrix[0][0], &boxP0.m_x, &boxP1.m_x);

    // place the origin of the visual mesh at the center of the height field
    matrix.m_posit = (boxP0 + boxP1).Scale (-0.5f);
    matrix.m_posit.m_w = 1.0f;
    floor->m_curPosition = matrix.m_posit;
    floor->m_prevPosition = matrix.m_posit;

    // relocate the body;
    NewtonBodySetMatrix (floorBody, &matrix[0][0]);

#else

    floor->LoadMesh ("LevelMesh.dat");

    // add static floor Physics

    shape = CreateMeshCollision (world, floor, materialMap);
    floorBody = CreateRigidBody (world, floor, shape, 0.0f);
    NewtonDestroyCollision(shape);

    // set the Transformation Matrix for this rigid body
    dMatrix matrix (floor->m_curRotation, floor->m_curPosition);
    NewtonBodySetMatrix (floorBody, &matrix[0][0]);

#endif

    // now we will use the properties of this body to set a proper world size.
    dVector minBox;
    dVector maxBox;
    NewtonCollisionCalculateAABB (shape, &matrix[0][0], &minBox[0], &maxBox[0]);

    // add some extra padding
    minBox.m_x -=  50.0f;
    minBox.m_y -= 500.0f;
    minBox.m_z -=  50.0f;

    maxBox.m_x +=  50.0f;
    maxBox.m_y += 500.0f;
    maxBox.m_z +=  50.0f;

    // set the new world size
    NewtonSetWorldSize (world, &minBox[0], &maxBox[0]);


    // Create a Body and attach a player controller joint
    {
        dFloat y0;
        Entity* player;
        NewtonBody* playerBody;
        NewtonCollision* shape;

        // find  the a floor to place the player
        y0 = FindFloor (world, 0.0f, 0.0f) + 1.0f;

        // load the player mesh
        player = sceneManager->CreateEntity();
        player->LoadMesh ("gymnast.dat");
        player->m_curPosition.m_y = y0;
        player->m_prevPosition = player->m_curPosition;

        // get the bounding Box of the player to get the collision shape dimensions
        dVector minBox;
        dVector maxBox;
        player->GetBBox (minBox, maxBox);

        // calculate player high and width
        dFloat padding = 1.0f / 64.0f;  // this si the default padding, for teh palye joint, we must subtract it from the shape
        dFloat playerHigh = (maxBox.m_y - minBox.m_y) - padding;
        dFloat playerRadius0 = (maxBox.m_z - minBox.m_z) * 0.5f;
        dFloat playerRadius1 = (maxBox.m_x - minBox.m_x) * 0.5f;
        dFloat playerRadius = (playerRadius0 > playerRadius1 ? playerRadius0 : playerRadius1) - padding;

        // No we make and make a upright capsule for the collision mesh
        dMatrix orientation;
        orientation.m_front = dVector (0.0f, 1.0f, 0.0f, 0.0f);			// this is the player up direction
        orientation.m_up    = dVector (1.0f, 0.0f, 0.0f, 0.0f);			// this is the player front direction
        orientation.m_right = orientation.m_front * orientation.m_up;   // this is the player sideway direction
        orientation.m_posit = dVector (0.0f, 0.0f, 0.0f, 1.0f);

        // add a body with a box shape
        //shape = CreateNewtonCapsule (world, player, playerHigh, playerRadius, m_wood, orientation);
        shape = CreateNewtonCylinder (world, player, playerHigh, playerRadius, m_wood, orientation);
        playerBody = CreateRigidBody (world, player, shape, 10.0f);
        NewtonDestroyCollision(shape);

        // make sure the player does not go to sleep
        NewtonBodySetAutoSleep (playerBody, 0);

        // now we will attach a player controller to the body
        NewtonUserJoint* playerController;
        // the player can take step up to 0.7 units;
        dFloat maxStairStepFactor = 0.7f / playerHigh;
        playerController = CreateCustomPlayerController (&orientation[0][0], playerBody, maxStairStepFactor, padding);

        // set the Max Slope the player can climb to PLAYER_MAX_SLOPE degree
        CustomPlayerControllerSetMaxSlope (playerController, PLAYER_MAX_SLOPE * 3.1416f / 180.0f);


        // now we will append some application data for the application to control the player
        PlayerController* userControl = (PlayerController*) malloc (sizeof (PlayerController));
        userControl->m_isThirdView = 1;
        userControl->m_point = dVector (0.0f, playerHigh, 0.0f,0.0f);

        // set the user data for the application to control the player
        CustomSetUserData (playerController, userControl);

        // set the destruction call back so that the application can destroy local used data
        CustomSetDestructorCallback (playerController, PlayerController::Destroy);

        // set a call back to control the player
        CustomSetSubmitContraintCallback (playerController, PlayerController::ApplyPlayerInput);

        // we also need to set override the transform call back so the we can set the Camera
        userControl->m_setTransformOriginal = NewtonBodyGetTransformCallback(playerBody);
        NewtonBodySetTransformCallback (playerBody, PlayerController::SetTransform);

        // we will need some ID to fin this joint in the transform Callback
        CustomSetJointID (playerController, PLAYER_JOINT_ID);
    }

    /*
    	{
    		// add some visual entities.
    		dFloat y0;
    		y0 = FindFloor (world, 0.0f, 0.4f) + 10.5f;
    		for (int i = 0; i < 5; i ++) {
    			Entity* frowny;
    			NewtonBody* frownyBody;
    			NewtonCollision* shape;

    			frowny = sceneManager->CreateEntity();
    			frowny->LoadMesh ("Frowny.dat");
    			frowny->m_curPosition.m_z = 0.4f;
    			frowny->m_curPosition.m_y = y0;
    			y0 += 2.0f;
    			frowny->m_prevPosition = frowny->m_curPosition;

    			// add a body with a Convex hull shape
    			shape = CreateNewtonConvex (world, frowny, m_wood);
    			frownyBody = CreateRigidBody (world, frowny, shape, 10.0f);
    			NewtonDestroyCollision(shape);
    		}
    	}
    */
    // set the Camera EyePoint close to the scene action
    SetCameraEyePoint (dVector (-15.0f, FindFloor (world, -15.0f, 0.0f) + 5.0f, 0.0f));
}
CStdSerialize *RbClassFactory::CreateObject(std::string strClassType, std::string strObjectType, bool bThrowError)
{
	CStdSerialize *lpObject=NULL;

	strClassType = Std_ToUpper(Std_Trim(strClassType));

	if(strClassType == "RIGIDBODY")
		lpObject = CreateRigidBody(strObjectType, bThrowError);
	else if(strClassType == "JOINT")
		lpObject = CreateJoint(strObjectType, bThrowError);
	else if(strClassType == "ORGANISM")
		lpObject = CreateStructure(strObjectType, bThrowError);
	else if(strClassType == "STRUCTURE")
		lpObject = CreateStructure(strObjectType, bThrowError);
	else if(strClassType == "SIMULATOR")
		lpObject = CreateSimulator(strObjectType, bThrowError);
	else if(strClassType == "KEYFRAME")
		lpObject = CreateKeyFrame(strObjectType, bThrowError);
	else if(strClassType == "DATACHART")
		lpObject = CreateDataChart(strObjectType, bThrowError);
	else if(strClassType == "DATACOLUMN")
		lpObject = CreateDataColumn(strObjectType, bThrowError);
	else if(strClassType == "EXTERNALSTIMULUS")
		lpObject = CreateExternalStimulus(strObjectType, bThrowError);
	else if(strClassType == "ADAPTER")
		lpObject = CreateAdapter(strObjectType, bThrowError);
	else if(strClassType == "GAIN")
		lpObject = CreateGain(strObjectType, bThrowError);
	else if(strClassType == "HUDITEM")
		lpObject = CreateHudItem(strObjectType, bThrowError);
	else if(strClassType == "HUD")
		lpObject = CreateHud(strObjectType, bThrowError);
	else if(strClassType == "MATERIAL")
		lpObject = CreateMaterialItem(strObjectType, bThrowError);
	else if(strClassType == "SIMULATIONWINDOW")
		lpObject = CreateWindowItem(strObjectType, bThrowError);
	else if(strClassType == "LIGHT")
		lpObject = CreateLight(strObjectType, bThrowError);
	else if(strClassType == "NEURALMODULE")
		lpObject = CreateNeuralModule(strObjectType, bThrowError);
	else if(strClassType == "CONSTRAINTRELAXATION")
		lpObject = CreateConstraintRelaxation(strObjectType, bThrowError);
	else if(strClassType == "CONSTRAINTFRICTION")
		lpObject = CreateConstraintFriction(strObjectType, bThrowError);
	else if(strClassType == "ROBOTINTERFACE")
		lpObject = CreateRobotInterface(strObjectType, bThrowError);
	else if(strClassType == "ROBOTIOCONTROL")
		lpObject = CreateRobotIOControl(strObjectType, bThrowError);
	else if(strClassType == "ROBOTPARTINTERFACE")
		lpObject = CreateRobotPartInterface(strObjectType, bThrowError);
	else if(strClassType == "REMOTECONTROLLINKAGE")
		lpObject = CreateRemoteControlLinkage(strObjectType, bThrowError);
	else
	{
		lpObject = NULL;
		if(bThrowError)
			THROW_PARAM_ERROR(Std_Err_lInvalidClassType, Std_Err_strInvalidClassType, "ClassType", strClassType);
	}

	return lpObject;
}
Exemplo n.º 17
0
void CreateScene (NewtonWorld* world, SceneManager* sceneManager)
{
	Entity* floor;
	Entity* smilly;
	Entity* frowny;
	NewtonBody* floorBody;
	NewtonBody* smillyBody;
	NewtonBody* frownyBody;
	NewtonCollision* shape;

	// initialize the camera
	InitCamera (dVector (-15.0f, 5.0f, 0.0f, 0.0f), dVector (1.0f, 0.0f, 0.0f));


	// Create a large body to be the floor
	floor = sceneManager->CreateEntity();
	floor->LoadMesh ("FloorBox.dat");

	// add static floor Physics
	shape = CreateNewtonBox (world, floor, 0);
	floorBody = CreateRigidBody (world, floor, shape, 0.0f);
	NewtonDestroyCollision(shape);

	// set the Transformation Matrix for this rigid body
	dMatrix matrix (floor->m_curRotation, floor->m_curPosition);
	NewtonBodySetMatrix (floorBody, &matrix[0][0]);

	// add some visual entities.
	smilly = sceneManager->CreateEntity();
	smilly->LoadMesh ("Smilly.dat");
	smilly->m_curPosition.m_y = 10.0f;
	smilly->m_prevPosition = smilly->m_curPosition;

	// add a body with a box shape
	shape = CreateNewtonBox (world, smilly, 0);
	smillyBody = CreateRigidBody (world, smilly, shape, 10.0f);
	NewtonDestroyCollision(shape);


	// add some visual entities.
	frowny = sceneManager->CreateEntity();
	frowny->LoadMesh ("Frowny.dat");
	frowny->m_curPosition.m_z = 0.4f;
	frowny->m_curPosition.m_y = 10.0f + 0.4f;
	frowny->m_prevPosition = frowny->m_curPosition;

	// add a body with a Convex hull shape
	shape = CreateNewtonConvex (world, frowny, 0);
	frownyBody = CreateRigidBody (world, frowny, shape, 10.0f);
	NewtonDestroyCollision(shape);

	
	// we will Link the Frowny Body to the world with Hinge regenerated from a Generic 6DOF joint.
	 matrix = GetIdentityMatrix();
	 matrix.m_posit = frowny->m_curPosition;
	 matrix.m_posit.m_z += 0.2f;
	 matrix.m_posit.m_y += 0.4f;

	 // specify the limits for defining a Hinge around the x axis
	 dVector minLinearLimits (0.0f, 0.0f, 0.0f, 0.0f);
	 dVector maxLinearLimits (0.0f, 0.0f, 0.0f, 0.0f);

	 dVector minAngulaLimits (-0.5f * 3.1416f, 0.0f, 0.0f, 0.0f);
	 dVector maxAngulaLimits ( 0.5f * 3.1416f, 0.0f, 0.0f, 0.0f);

	 NewtonUserJoint* frownyHinge;
	 // Create a 6DOF joint 
	 frownyHinge = CreateCustomJoint6DOF (&matrix[0][0], &matrix[0][0], frownyBody, NULL);

	 // set the hinge Limits
 	 CustomJoint6DOF_SetLinearLimits (frownyHinge, &minLinearLimits[0], &maxLinearLimits[0]);
 	 CustomJoint6DOF_SetAngularLimits (frownyHinge, &minAngulaLimits[0], &maxAngulaLimits[0]);

	
	// now we will link the body of Smilly and Frowny with a specialize Hinge Joint 
	 NewtonUserJoint* smillyFrownyHinge;
	 matrix.m_posit = smilly->m_curPosition;
	 matrix.m_posit.m_z += 0.2f;
	 matrix.m_posit.m_y += 0.4f;

	 smillyFrownyHinge = CreateCustomHinge (&matrix[0][0], smillyBody, frownyBody);
	 HingeEnableLimits (smillyFrownyHinge, 1);
	 HingeSetLimits (smillyFrownyHinge, -0.5f * 3.1416f, 0.5f * 3.1416f);

//	 smillyFrownyHinge = CreateCustomBallAndSocket (&matrix[0][0], smillyBody, frownyBody);
//	 BallAndSocketSetConeAngle (smillyFrownyHinge, 0.5f * 3.1416f);
//	 BallAndSocketSetTwistAngle (smillyFrownyHinge, -0.5f * 3.1416f, 0.5f * 3.1416f);

//	 smillyFrownyHinge = CreateCustomSlider (&matrix[0][0], smillyBody, frownyBody);
//	 SliderEnableLimits (smillyFrownyHinge, 1);
//	 SliderSetLimis (smillyFrownyHinge, -0.5f * 3.1416f, 0.5f * 3.1416f);

	 {
		// adding two Kinematic controlled object
		frowny = sceneManager->CreateEntity();
		frowny->LoadMesh ("Frowny.dat");
		frowny->m_curPosition.m_z = 4.0f;
		frowny->m_curPosition.m_y = 7.0f;
		frowny->m_prevPosition = frowny->m_curPosition;
		shape = CreateNewtonConvex (world, frowny, 0);
		frownyBody = CreateRigidBody (world, frowny, shape, 10.0f);
		NewtonDestroyCollision(shape);
		 
		 
		FollowPath* path;
		NewtonUserJoint* g_pathFollow;

		path = (FollowPath*) malloc (sizeof (FollowPath));

		NewtonBodyGetMatrix (frownyBody, &matrix[0][0]);
		path->m_origin = matrix.m_posit;
		path->m_angle = 0.0f;
		path->radius = 3.0f;

		g_pathFollow = CreateCustomKinematicController (frownyBody, &matrix.m_posit[0]);
		CustomKinematicControllerSetPickMode (g_pathFollow, 1);
		CustomKinematicControllerSetMaxAngularFriction (g_pathFollow, 200.0f);
		CustomKinematicControllerSetMaxLinearFriction (g_pathFollow, 1000.0f);
		CustomSetUserData(g_pathFollow, path);
		CustomSetDestructorCallback(g_pathFollow, FollowPath::Destroy);
		CustomSetSubmitContraintCallback (g_pathFollow, FollowPath::EvaluatePath);

		matrix = path->BuildMatrix (0.0f);
		NewtonBodySetMatrix(frownyBody, &matrix[0][0]);
	 }

	 {
		// adding two Kinematic controlled object
		frowny = sceneManager->CreateEntity();
		frowny->LoadMesh ("Frowny.dat");
		frowny->m_curPosition.m_z = -4.0f;
		frowny->m_curPosition.m_y =  6.0f;
		frowny->m_prevPosition = frowny->m_curPosition;
		shape = CreateNewtonConvex (world, frowny, 0);
		frownyBody = CreateRigidBody (world, frowny, shape, 10.0f);
		NewtonDestroyCollision(shape);


		FollowPath* path;
		NewtonUserJoint* g_pathFollow;

		path = (FollowPath*) malloc (sizeof (FollowPath));

		NewtonBodyGetMatrix (frownyBody, &matrix[0][0]);
		path->m_origin = matrix.m_posit;
		path->m_angle = 3.1416f;
		path->radius = 3.0f;

		g_pathFollow = CreateCustomKinematicController (frownyBody, &matrix.m_posit[0]);
		CustomKinematicControllerSetPickMode (g_pathFollow, 1);
		CustomKinematicControllerSetMaxAngularFriction (g_pathFollow, 200.0f);
		CustomKinematicControllerSetMaxLinearFriction (g_pathFollow, 1000.0f);
		CustomSetUserData(g_pathFollow, path);
		CustomSetDestructorCallback(g_pathFollow, FollowPath::Destroy);
		CustomSetSubmitContraintCallback (g_pathFollow, FollowPath::EvaluatePath);

		matrix = path->BuildMatrix (0.0f);
		NewtonBodySetMatrix(frownyBody, &matrix[0][0]);
	 }
}
Exemplo n.º 18
0
World::World():
	dynamicsWorld(0),
	collisionShapes(),
	overlappingPairCache(0),
	dispatcher(0),
	constraintSolver(0),
	collisionConfiguration(0),
	indexVertexArrays(0),
	vertices(0),
	clock(),
	groundTexture(0),

	screenAspect(1.f),
	cameraHeight(4.f),
	minCameraDistance(3.f),
	maxCameraDistance(10.f),
	cameraPosition(-100.f, 30.f, 100.f),

	cameraType(CT_3RD),
	idle(false)

{
	shapeDrawer = new GL_ShapeDrawer();
	shapeDrawer->enableTexture(true);

	btCollisionShape* groundShape = new btBoxShape(btVector3(50,3,50));
	collisionShapes.push_back(groundShape);
	collisionConfiguration = new btDefaultCollisionConfiguration();
	dispatcher = new btCollisionDispatcher(collisionConfiguration);
	btVector3 worldMin(-1000,-1000,-1000);
	btVector3 worldMax(1000,1000,1000);
	overlappingPairCache = new btAxisSweep3(worldMin,worldMax);
	constraintSolver = new btSequentialImpulseConstraintSolver();

	dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, constraintSolver, collisionConfiguration);
	dynamicsWorld->setGravity(btVector3(0,-10,0));

	btTransform tr;
	tr.setIdentity();

	//triangle mesh
	int i;

	const float TRIANGLE_SIZE=20.f;

	//create a triangle-mesh ground
	int vertStride = sizeof(btVector3);
	int indexStride = 3*sizeof(int);

	const int NUVERTS_X = 21;
	const int NUVERTS_Y = 21;
	const int totalVerts = NUVERTS_X*NUVERTS_Y;
	
	const int totalTriangles = 2*(NUVERTS_X-1)*(NUVERTS_Y-1);

	vertices = new btVector3[totalVerts];
	int*	gIndices = new int[totalTriangles*3];

	
	//build terrain mesh
	for ( i=0;i<NUVERTS_X;i++)
	{
		for (int j=0;j<NUVERTS_Y;j++)
		{
			float wl = .2f;
			//height set to zero, but can also use curved landscape, just uncomment out the code
			float height = 20.f*sinf(float(i)*wl)*cosf(float(j)*wl);
			vertices[i+j*NUVERTS_X].setValue(
				(i-NUVERTS_X*0.5f)*TRIANGLE_SIZE,
				height,
				(j-NUVERTS_Y*0.5f)*TRIANGLE_SIZE);

		}
	}

	int index=0;
	for ( i=0;i<NUVERTS_X-1;i++)
	{
		for (int j=0;j<NUVERTS_Y-1;j++)
		{
			gIndices[index++] = j*NUVERTS_X+i;
			gIndices[index++] = j*NUVERTS_X+i+1;
			gIndices[index++] = (j+1)*NUVERTS_X+i+1;

			gIndices[index++] = j*NUVERTS_X+i;
			gIndices[index++] = (j+1)*NUVERTS_X+i+1;
			gIndices[index++] = (j+1)*NUVERTS_X+i;
		}
	}
	
	indexVertexArrays = new btTriangleIndexVertexArray(totalTriangles,
		gIndices,
		indexStride,
		totalVerts,(btScalar*) &vertices[0].x(),vertStride);

	bool useQuantizedAabbCompression = true;
	groundShape = new btBvhTriangleMeshShape(indexVertexArrays,useQuantizedAabbCompression);
	
	tr.setOrigin(btVector3(0,-4.5f,0));

	collisionShapes.push_back(groundShape);

	//create ground object
	groundRigidBody = CreateRigidBody(0,tr,groundShape);
	dynamicsWorld->addRigidBody(groundRigidBody);

	float wallLength = (NUVERTS_X-1)*TRIANGLE_SIZE;
	float wallHeight = 40.f;
	float wallWidth = 5.f;

	btCollisionShape* wall = new btBoxShape(btVector3(wallLength/2, wallHeight/2, wallWidth/2));
	collisionShapes.push_back(wall);
	tr.setOrigin(btVector3(-TRIANGLE_SIZE/2.f, 0.f, wallLength/2-wallWidth/2-TRIANGLE_SIZE/2));
	dynamicsWorld->addRigidBody(CreateRigidBody(0.f, tr, wall));

	wall = new btBoxShape(btVector3(wallLength/2, wallHeight/2, wallWidth/2));
	collisionShapes.push_back(wall);
	tr.setOrigin(btVector3(-TRIANGLE_SIZE/2.f, 0.f, -wallLength/2-wallWidth/2-TRIANGLE_SIZE/2));
	dynamicsWorld->addRigidBody(CreateRigidBody(0.f, tr, wall));

	wall = new btBoxShape(btVector3(wallWidth/2, wallHeight/2, wallLength/2));
	collisionShapes.push_back(wall);
	tr.setOrigin(btVector3(wallLength/2-wallWidth/2-TRIANGLE_SIZE/2, 0.f, -TRIANGLE_SIZE/2.f));
	dynamicsWorld->addRigidBody(CreateRigidBody(0.f, tr, wall));

	wall = new btBoxShape(btVector3(wallWidth/2, wallHeight/2, wallLength/2));
	collisionShapes.push_back(wall);
	tr.setOrigin(btVector3(-wallLength/2-wallWidth/2-TRIANGLE_SIZE/2, 0.f, -TRIANGLE_SIZE/2.f));
	dynamicsWorld->addRigidBody(CreateRigidBody(0.f, tr, wall));

	//create vehicle
	vehicle = new Vehicle(dynamicsWorld);
	
	cameraDistance = 26.f;
}
Exemplo n.º 19
0
void CreateScene (NewtonWorld* world, SceneManager* sceneManager)
{
	Entity* floor;
	NewtonCollision* shape;
	NewtonBody* floorBody;
	void* materialManager;
	SoundManager* sndManager;
	PhysicsMaterialInteration matInterations;
	
	sndManager = sceneManager->GetSoundManager();

	// Create the material for this scene, and attach it to the Newton World
	materialManager = CreateMaterialManager (world, sndManager);

	// add the Material table
	matInterations.m_restitution = 0.6f;
	matInterations.m_staticFriction = 0.6f;
	matInterations.m_kineticFriction = 0.3f;
	matInterations.m_scrapingSound = NULL;

	matInterations.m_impactSound = sndManager->LoadSound ("metalMetal.wav");
	AddMaterilInteraction (materialManager, m_metal, m_metal, &matInterations);

	matInterations.m_impactSound = sndManager->LoadSound ("boxBox.wav");
	AddMaterilInteraction (materialManager, m_wood, m_wood, &matInterations);

	matInterations.m_impactSound = sndManager->LoadSound ("metalBox.wav");
	AddMaterilInteraction (materialManager, m_metal, m_wood, &matInterations);
	
	matInterations.m_impactSound = sndManager->LoadSound ("grass0.wav");
	AddMaterilInteraction (materialManager, m_wood, m_grass, &matInterations);

	matInterations.m_impactSound = sndManager->LoadSound ("boxHit.wav");
	AddMaterilInteraction (materialManager, m_wood, m_bricks, &matInterations);

	matInterations.m_impactSound = sndManager->LoadSound ("grass1.wav");
	AddMaterilInteraction (materialManager, m_metal, m_grass, &matInterations);
	AddMaterilInteraction (materialManager, m_grass, m_bricks, &matInterations);

	matInterations.m_impactSound = sndManager->LoadSound ("metal.wav");
	AddMaterilInteraction (materialManager, m_metal, m_bricks, &matInterations);
	AddMaterilInteraction (materialManager, m_grass, m_grass, &matInterations);
	
	

	// Create a large body to be the floor
	floor = sceneManager->CreateEntity();
	floor->LoadMesh ("LevelMesh.dat");

	// add static floor Physics
	int materialMap[] = {m_bricks, m_grass, m_wood,	m_metal};
	shape = CreateMeshCollision (world, floor, materialMap);
	floorBody = CreateRigidBody (world, floor, shape, 0.0f);
	NewtonReleaseCollision (world, shape);


	// set the Transformation Matrix for this rigid body
	dMatrix matrix (floor->m_curRotation, floor->m_curPosition);
	NewtonBodySetMatrix (floorBody, &matrix[0][0]);

	// now we will use the properties of this body to set a proper world size.
	dVector minBox;
	dVector maxBox;
	NewtonCollisionCalculateAABB (shape, &matrix[0][0], &minBox[0], &maxBox[0]);

	// add some extra padding
	minBox.m_x -=  50.0f;
	minBox.m_y -= 500.0f;
	minBox.m_z -=  50.0f;

	maxBox.m_x +=  50.0f;
	maxBox.m_y += 500.0f;
	maxBox.m_z +=  50.0f;

	// set the new world size
	NewtonSetWorldSize (world, &minBox[0], &maxBox[0]);

	// add some visual entities.
	dFloat y0 = FindFloor (world, 0.0f, 0.0f) + 10.0f;
	for (int i = 0; i < 5; i ++) {
		Entity* smilly;
		NewtonBody* smillyBody;
		smilly = sceneManager->CreateEntity();
		smilly->LoadMesh ("Smilly.dat");
		smilly->m_curPosition.m_y = y0;
		y0 += 2.0f;
		smilly->m_prevPosition = smilly->m_curPosition;

		// add a body with a box shape
		shape = CreateNewtonBox (world, smilly, m_metal);
		smillyBody = CreateRigidBody (world, smilly, shape, 10.0f);
		NewtonReleaseCollision (world, shape);
	}

	// add some visual entities.
	y0 = FindFloor (world, 0.0f, 0.4f) + 10.5f;
	for (int i = 0; i < 5; i ++) {
		Entity* frowny;
		NewtonBody* frownyBody;
		frowny = sceneManager->CreateEntity();
		frowny->LoadMesh ("Frowny.dat");
		frowny->m_curPosition.m_z = 0.4f;
		frowny->m_curPosition.m_y = y0;
		y0 += 2.0f;
		frowny->m_prevPosition = frowny->m_curPosition;

		// add a body with a Convex hull shape
		shape = CreateNewtonConvex (world, frowny, m_wood);
		frownyBody = CreateRigidBody (world, frowny, shape, 10.0f);
		NewtonReleaseCollision (world, shape);
	}

	y0 = FindFloor (world, 0.0f, 2.0f) + 10.5f;
	for (int i = 0; i < 5; i ++) {
		Entity* frowny;
		NewtonBody* frownyBody;

		frowny = sceneManager->CreateEntity();
		frowny->LoadMesh ("dumb.dat");
		frowny->m_curPosition.m_z = 2.0f;
		frowny->m_curPosition.m_y = y0;
		y0 += 2.0f;
		frowny->m_prevPosition = frowny->m_curPosition;

		// add a body with a Convex hull shape
		int materialMap[] = {m_grass, m_wood, m_metal, m_metal};
		shape = CreateNewtonCompoundFromEntitySubmesh (world, frowny, materialMap);
		frownyBody = CreateRigidBody (world, frowny, shape, 10.0f);
		NewtonReleaseCollision (world, shape);
	}


	// set the Camera EyePoint close to the scene action
	InitCamera (dVector (-15.0f, FindFloor (world, -15.0f, 0.0f) + 5.0f, 0.0f), dVector (1.0f, 0.0f, 0.0f));
}
Exemplo n.º 20
0
void CreateScene (NewtonWorld* world, SceneManager* sceneManager)
{
	Entity* floor;
	NewtonBody* floorBody;
	NewtonCollision* shape;
	void* materialManager;
	SoundManager* sndManager;
	PhysicsMaterialInteration matInterations;

	sndManager = sceneManager->GetSoundManager();

	// Create the material for this scene, and attach it to the Newton World
	materialManager = CreateMaterialManager (world, sndManager);

	// add the Material table
	matInterations.m_restitution = 0.6f;
	matInterations.m_staticFriction = 0.6f;
	matInterations.m_kineticFriction = 0.3f;
	matInterations.m_scrapingSound = NULL;

	matInterations.m_impactSound = sndManager->LoadSound ("metalMetal.wav");
	AddMaterilInteraction (materialManager, m_metal, m_metal, &matInterations);

	matInterations.m_impactSound = sndManager->LoadSound ("boxBox.wav");
	AddMaterilInteraction (materialManager, m_wood, m_wood, &matInterations);

	matInterations.m_impactSound = sndManager->LoadSound ("metalBox.wav");
	AddMaterilInteraction (materialManager, m_metal, m_wood, &matInterations);

	matInterations.m_impactSound = sndManager->LoadSound ("grass0.wav");
	AddMaterilInteraction (materialManager, m_wood, m_grass, &matInterations);

	matInterations.m_impactSound = sndManager->LoadSound ("boxHit.wav");
	AddMaterilInteraction (materialManager, m_wood, m_bricks, &matInterations);

	matInterations.m_impactSound = sndManager->LoadSound ("grass1.wav");
	AddMaterilInteraction (materialManager, m_metal, m_grass, &matInterations);

	matInterations.m_impactSound = sndManager->LoadSound ("metal.wav");
	AddMaterilInteraction (materialManager, m_metal, m_bricks, &matInterations);


	// Create a large body to be the floor
	floor = sceneManager->CreateEntity();

	// add scene collision from a level mesh
	int materialMap[] = {m_bricks, m_grass, m_wood,	m_metal};
	shape = CreateHeightFieldCollision (world, "h2.raw", materialMap);
	floorBody = CreateRigidBody (world, floor, shape, 0.0f);
	NewtonDestroyCollision(shape);

	// make a visual mesh for the collision data
	CreateHeightFieldMesh (shape, floor);

	// set the matrix at the origin
	dVector boxP0; 
	dVector boxP1; 
	dMatrix matrix (floor->m_curRotation, floor->m_curPosition);
	NewtonCollisionCalculateAABB (shape, &matrix[0][0], &boxP0.m_x, &boxP1.m_x); 

	// place the origin of the visual mesh at the center of the height field
	matrix.m_posit = (boxP0 + boxP1).Scale (-0.5f);
	matrix.m_posit.m_w = 1.0f;
	floor->m_curPosition = matrix.m_posit;
	floor->m_prevPosition = matrix.m_posit;

	// relocate the body;
	NewtonBodySetMatrix (floorBody, &matrix[0][0]);

	// now we will use the properties of this body to set a proper world size.
	NewtonCollisionCalculateAABB (shape, &matrix[0][0], &boxP0.m_x, &boxP1.m_x); 

	// add some extra padding
	boxP0.m_x -=  50.0f;
	boxP0.m_y -= 500.0f;
	boxP0.m_z -=  50.0f;

	boxP1.m_x +=  50.0f;
	boxP1.m_y += 500.0f;
	boxP1.m_z +=  50.0f;

	// set the new world size
	NewtonSetWorldSize (world, &boxP0[0], &boxP1[0]);


	// add some visual entities.
	dFloat y0 = FindFloor (world, 0.0f, 0.0f) + 2.0f;
	for (int i = 0; i < 1; i ++) {
		Entity* carEnt;
		NewtonBody* carBody;
		NewtonUserJoint* carController;

		carEnt = sceneManager->CreateEntity();
		carEnt->LoadMesh ("porche.dat");
		carEnt->m_curPosition.m_y = y0;
		y0 += 2.0f;
		carEnt->m_prevPosition = carEnt->m_curPosition;

		// add a body with a box shape
		shape = CreateNewtonBox (world, carEnt, m_metal);
		// the mass is the Car weight divide by the Gravity 
		carBody = CreateRigidBody (world, carEnt, shape, CAR_WEIGHT / 10.0f);
		NewtonDestroyCollision(shape);

		// Now create a RayCast Car for to control this body
		carController = CreateRayCastCast (carBody, sceneManager, porcheTirePositions);
	}


	y0 = FindFloor (world, 4.0f, 0.0f) + 2.0f;
	for (int i = 0; i < 1; i ++) {
		Entity* carEnt;
		NewtonBody* carBody;
		NewtonUserJoint* carController;

		carEnt = sceneManager->CreateEntity();
		carEnt->LoadMesh ("cadillac.dat");
		carEnt->m_curPosition.m_z = 4.0f;
		carEnt->m_curPosition.m_y = y0;
		y0 += 2.0f;
		carEnt->m_prevPosition = carEnt->m_curPosition;

		// add a body with a box shape
		shape = CreateNewtonBox (world, carEnt, m_metal);
		// the mass is the Car weight divide by the Gravity 
		carBody = CreateRigidBody (world, carEnt, shape, CAR_WEIGHT / 10.0f);
		NewtonDestroyCollision(shape);

		// Now create a RayCast Car for to control this body
		carController = CreateRayCastCast (carBody, sceneManager, cadillackirePositions);
	}

	// initialize the camera
	InitCamera (dVector (-15.0f, FindFloor (world, -15.0f, 0.0f) + 5.0f, 0.0f), dVector (1.0f, 0.0f, 0.0f));
}
Exemplo n.º 21
0
void CcdChapter::CreateBullet()
{

	m_pkCollisionConfiguration = new btDefaultCollisionConfiguration();
	m_pkDispatcher = new btCollisionDispatcher(m_pkCollisionConfiguration);

	m_pkBroadphase = new btDbvtBroadphase();
	m_pkSolver = new btSequentialImpulseConstraintSolver();

	m_pkDynamicsWorld = new btDiscreteDynamicsWorld(m_pkDispatcher, m_pkBroadphase, m_pkSolver, m_pkCollisionConfiguration);
	m_pkDynamicsWorld->getSolverInfo().m_solverMode |= SOLVER_RANDMIZE_ORDER;

	m_pkDynamicsWorld->getDispatchInfo().m_enableSPU = true;
	m_pkDynamicsWorld->setGravity(btVector3(0, -10, 0));


	gContactAddedCallback = contactAddedCallback;


	//	DebugDraw
	m_pkDebugDraw = new BulletDebugDraw();
	m_pkDebugDraw->setVisible(false);
	m_pkDynamicsWorld->setDebugDrawer(m_pkDebugDraw);
	m_pkDebugDraw->setDebugMode(BulletDebugDraw::DBG_DrawText | BulletDebugDraw::DBG_DrawContactPoints);

	m_pkDebugNode = m_pkRootNode->createChildSceneNode();
	m_pkDebugNode->attachObject(m_pkDebugDraw);






	int i;
	btTransform tr;
	tr.setIdentity();

	for(i=0; i<120; ++i)
	{
		if(i>0)
		{
			m_nShapeIndex[i] = 1;
		}
		else
			m_nShapeIndex[i] = 0;
	}

	for(i=0; i<32760; ++i)
	{
		btCollisionShape *shape = m_CollisionShapes[m_nShapeIndex[i]];
		shape->setMargin(0.05f);

		bool isDyna = 1>0;

		btTransform trans;
		trans.setIdentity();

		if(i>0)
		{
			int colsize = 10;
			int row = (i*0.5f*2)/(colsize*2*0.5f);
			int row2 = row;
			int col = (i)%(colsize)-colsize/2;

			if(col>3)
			{
				col = 11;
				row2 |= 1;
			}

			btVector3 pos(col*2*0.5f + (row2%2)*0.5f, row*2*0.5f+0.5f+ -10.f, 0);

			trans.setOrigin(pos);
		}
		else
		{
			trans.setOrigin(btVector3(0, -10.0f-0.5f, 0));
		}

		float mass = 1.0f;

		if( !isDyna )
			mass = 0.0f;

		btRigidBody *body = CreateRigidBody(shape, isDyna, trans, mass);

		body->setCcdMotionThreshold( 0.5f );

		body->setCcdSweptSphereRadius( 0.2f * 0.5f );
	}
}
Exemplo n.º 22
0
The second part uses the bounding Box of the collision mesh to set the final size of the physics world. This is also an important step because in the Newton world the size of the world is finite. The consequence of not doing this is that when a body move to a position the is outside the default world size the physics silently stop working on that body.


The final line of function CreateScene, just repeat the same process of adding more physics bodies to the scene

*/

	// add some visual entities.
	smilly = sceneManager->CreateEntity();
	smilly->LoadMesh ("Smilly.dat");
	smilly->m_curPosition.m_y = 10.0f;
	smilly->m_prevPosition = smilly->m_curPosition;
 
	// add a body with a box shape
	shape = CreateNewtonBox (world, smilly, 0);
	smillyBody = CreateRigidBody (world, smilly, shape, 10.0f);
	NewtonReleaseCollision (world, shape);
 
 
	// add some visual entities.
	frowny = sceneManager->CreateEntity();
	frowny->LoadMesh ("Frowny.dat");
	frowny->m_curPosition.m_z = 0.4f;
	frowny->m_curPosition.m_y = 10.0f + 0.4f;
	frowny->m_prevPosition = frowny->m_curPosition;
 
	// add a body with a Convex hull shape
	shape = CreateNewtonConvex (world, frowny, 0);
	frownyBody = CreateRigidBody (world, frowny, shape, 10.0f);
	NewtonReleaseCollision (world, shape);
/*
Exemplo n.º 23
0
	void BuildClothPatch (DemoEntityManager* const scene, int size_x, int size_z)
	{
		NewtonWorld* const world = scene->GetNewton();
		
		NewtonMesh* const clothPatch = CreateQuadClothPatch(scene, size_x, size_z);

		// create the array of points;
		int vertexCount = NewtonMeshGetVertexCount(clothPatch);
		int stride = NewtonMeshGetVertexStrideInByte (clothPatch) / sizeof (dFloat64); 
		const dFloat64* const meshPoints = NewtonMeshGetVertexArray (clothPatch); 

		dVector* const points = new dVector[vertexCount];
		for (int i =0; i < vertexCount; i ++ ) {
			points[i].m_x = dFloat (meshPoints[i * stride + 0]);
			points[i].m_y = dFloat (meshPoints[i * stride + 1]);
			points[i].m_z = dFloat (meshPoints[i * stride + 2]);
			points[i].m_w = 0.0f;
		}

		dFloat mass = 8.0f;
		// set the particle masses 
		dFloat unitMass = mass / vertexCount;
		dFloat* const clothMass = new dFloat[vertexCount];
		for (int i =0; i < vertexCount; i ++ ) {			
			clothMass[i] = unitMass;
		}

		int linksCount = 0;
		const int maxLinkCount = size_x * size_z * 16;

		// create the structual constation array;
		dFloat structuralSpring = dAbs(mass * DEMO_GRAVITY) / 0.01f;
		dFloat structuralDamper = 30.0f;

		int* const links = new int[2 * maxLinkCount];
		dFloat* const spring = new dFloat[maxLinkCount];
		dFloat* const damper = new dFloat[maxLinkCount];
		for (void* edgeNode = NewtonMeshGetFirstEdge (clothPatch); edgeNode; edgeNode = NewtonMeshGetNextEdge (clothPatch, edgeNode)) {
			int v0;
			int v1;
			NewtonMeshGetEdgeIndices (clothPatch, edgeNode, &v0, &v1);
			links[linksCount * 2 + 0] = v0;
			links[linksCount * 2 + 1] = v1;
			spring[linksCount] = structuralSpring;
			damper[linksCount] = structuralDamper;
			linksCount ++;
			dAssert (linksCount <= maxLinkCount);
		}
		

		// add shear constraints
		dFloat shearSpring = structuralSpring;
		dFloat shearDamper = structuralDamper;
		for (void* faceNode = NewtonMeshGetFirstFace (clothPatch); faceNode; faceNode = NewtonMeshGetNextFace (clothPatch, faceNode)) {
			if (!NewtonMeshIsFaceOpen(clothPatch, faceNode)) {
				int face[8];
				int indexCount = NewtonMeshGetFaceIndexCount (clothPatch, faceNode);
				NewtonMeshGetFaceIndices (clothPatch, faceNode, face);
				for (int i = 2; i < indexCount - 1; i ++) {
					links[linksCount * 2 + 0] = face[0];
					links[linksCount * 2 + 1] = face[i];
					spring[linksCount] = shearSpring;
					damper[linksCount] = shearDamper;

					linksCount ++;
					dAssert (linksCount <= maxLinkCount);
				}
				for (int i = 3; i < indexCount; i ++) {
					links[linksCount * 2 + 0] = face[1];
					links[linksCount * 2 + 1] = face[i];
					spring[linksCount] = shearSpring;
					damper[linksCount] = shearDamper;
					linksCount ++;
					dAssert (linksCount <= maxLinkCount);
				}
			}
		}

//linksCount = 0;
		NewtonCollision* const deformableCollision = NewtonCreateMassSpringDamperSystem(world, 0, 
													 &points[0].m_x, vertexCount, sizeof (dVector), clothMass,
													 links, linksCount, spring, damper);
		
		m_body = CreateRigidBody(scene, mass, deformableCollision);

		DemoMesh* const mesh = new ClothPatchMesh (scene, clothPatch, m_body);
		SetMesh(mesh, dGetIdentityMatrix());

		// do not forget to destroy this objects, else you get bad memory leaks.
		mesh->Release();
		NewtonDestroyCollision(deformableCollision);
		NewtonMeshDestroy(clothPatch);
		delete[] links;
		delete[] damper;
		delete[] spring;
		delete[] clothMass;
		delete[] points;
	}
Exemplo n.º 24
0
NewtonBody* CPhysics::CreateSphere( NewtonWorld *world, CObject3D *object, float radius, float mass /*= 0.0f*/ )
{
	NewtonCollision *collision = NewtonCreateSphere(world, radius, radius, radius, 0, NULL);
	return CreateRigidBody(world, object, collision, mass);	
}