示例#1
0
void AddTire (NewtonUserJoint* joint, const char* fileName, dVector position, SceneManager* sceneManager)
{
	dFloat width;
	dFloat radius;
	Entity* tireEnt;
	dVector minBox;
	dVector maxBox;

	tireEnt = sceneManager->CreateEntity();
	tireEnt->LoadMesh (fileName);

	tireEnt->GetBBox (minBox, maxBox);

	// find the width and high of the tire shape fro the graphics file
	width = (maxBox.m_z - minBox.m_z);
	radius = (maxBox.m_x - minBox.m_x) * 0.5f;

	// set at normal tire, with all it car dynamic parameters,
	// these parameter are shosen by some trial and error experimentation
	DGRaycastVehicleAddTire (joint, tireEnt, &position[0], TIRE_MASS, radius, width, 2.5f, 0.25f, 150.0f, 5.0f, 1);
}
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));
}