static void LoadPlayGroundScene(DemoEntityManager* const scene, TriggerManager* const triggerManager) { NewtonWorld* const world = scene->GetNewton(); // CreateLevelMesh (scene, "flatPlane.ngd", true); // return; // make the body with a dummy null collision, so that we can use for attaching world objects dMatrix matrix (dGetIdentityMatrix()); NewtonCollision* const dommyCollision = NewtonCreateNull(world); NewtonBody* const playGroundBody = NewtonCreateDynamicBody (world, dommyCollision, &matrix[0][0]); NewtonDestroyCollision (dommyCollision); // use a multi shape static scene collision NewtonCollision* const sceneCollision = NewtonCreateSceneCollision (world, 0); // start adding shapes to this scene collisions NewtonSceneCollisionBeginAddRemove (sceneCollision); // populate the scene collision { // load a flat floor LoadFloor(scene, sceneCollision); // load a slide platform dMatrix slideMatrix(dGetIdentityMatrix()); slideMatrix.m_posit.m_x += 80.0f; slideMatrix.m_posit.m_z = -20.0f; //LoadSlide(scene, triggerManager, sceneCollision, "slide.ngd", slideMatrix, playGroundBody); LoadFerryBridge(scene, triggerManager, sceneCollision, "platformBridge.ngd", slideMatrix, playGroundBody); // load another hanging bridge dMatrix bridgeMatrix(dGetIdentityMatrix()); bridgeMatrix.m_posit.m_x += 40.0f; LoadHangingBridge(scene, triggerManager, sceneCollision, "hangingBridge.ngd", bridgeMatrix, playGroundBody); // load another ferry bridge bridgeMatrix.m_posit.m_z += 20.0f; LoadFerryBridge(scene, triggerManager, sceneCollision, "platformBridge.ngd", bridgeMatrix, playGroundBody); } // finalize adding shapes to this scene collisions NewtonSceneCollisionEndAddRemove (sceneCollision); // attach this collision to the playground Body NewtonBodySetCollision(playGroundBody, sceneCollision); // set the reference to the visual //NewtonBodySetUserData(level, visualMesh); // do not forget to release the collision NewtonDestroyCollision (sceneCollision); }
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ void PhysicsActor::setScale(float _x, float _y, float _z) { m_scaleX = _x; m_scaleY = _y; m_scaleZ = _z; #if 0 NewtonCollision* collision = createCollision(); NewtonBodySetCollision(m_pActor, collision); NewtonReleaseCollision(dynamic_cast<PhysicsZone*>(m_pZone.get())->getZonePtr(), collision); #else std::cout << "Warning: PhysicsActor::setScale() not implemented!" << std::endl; #endif }
void SimplePlaneCollision (NewtonFrame& system) { NewtonWorld* world; LevelPrimitive *level; NewtonCollision* planeColl; world = system.m_world; // create a material carrier to collision with this object int defaultMaterialID; defaultMaterialID = NewtonMaterialGetDefaultGroupID (world); // create the sky box and the floor, level = LoadLevelAndSceneRoot (system, "flatplane.dae", 1); // Find the world mesh and replace the collision for a custom plane collision mesh dMatrix matrix; NewtonBodyGetMatrix (level->m_level, &matrix[0][0]); dVector plane (matrix.m_up); plane.m_w = - (plane % matrix.m_posit); planeColl = CreatePlaneCollidion (world, plane); NewtonBodySetCollision(level->m_level, planeColl); NewtonReleaseCollision(world, planeColl); dVector posit (0.0f, 0.0f, 0.0f, 0.0f); posit.m_y = FindFloor (world, posit.m_x, posit.m_z) + 5.0f; InitEyePoint (dVector (1.0f, 0.0f, 0.0f), posit); dVector size (1.0f, 1.0f, 1.0f); dVector location (cameraEyepoint + cameraDir.Scale (10.0f)); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _SPHERE_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _BOX_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _CONE_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _CYLINDER_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _CAPSULE_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _CHAMFER_CYLINDER_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _RANDOM_CONVEX_HULL_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _REGULAR_CONVEX_HULL_PRIMITIVE, defaultMaterialID); }
void CEffectsGame::CreateScene() { pScene->CreateSkybox("clear"); NewtonBody *bFloor = AddBox(pScene, pWorld, Vector3(0,-0.5,0), Vector3(1000,1,1000), Vector3()); NewtonCollision *col = NewtonCreateTreeCollision(pWorld, 0); NewtonTreeCollisionBeginBuild(col); Vector3 v[4] = { Vector3(-1000,0.5f,-1000), Vector3(-1000,0.5f,+1000), Vector3(+1000,0.5f,+1000), Vector3(+1000,0.5f,-1000) }; NewtonTreeCollisionAddFace(col, 4, &v[0][0], sizeof(Vector3), 1); NewtonTreeCollisionEndBuild(col, 0); NewtonBodySetCollision(bFloor, col); CObject3D *f = (CObject3D*)NewtonBodyGetUserData(bFloor); f->visible = false; NewtonBodySetMaterialGroupID(bFloor, gLevelChunksMaterialID); // floor static CTexture *floorTex = new CTexture("textures/512.png"); CMaterial *floorMat = new CMaterial(); floorMat->features = EShaderFeature::LIGHT | EShaderFeature::FOG | EShaderFeature::SHADOW | EShaderFeature::TEXTURE; CPlaneGeometry *floorGeom = new CPlaneGeometry(1000,1000); CObject3D *floor = new CMesh( floorGeom, floorMat ); floor->geometry->materials.AddToTail(floorMat); floorMat->pTexture = floorTex; floorGeom->SetTextureScale(40,40); floor->SetPosition(-500, 0, -500); pScene->Add(floor); pLevel = new CLevel(pScene, pWorld); pLevel->Create(32,80,32); int width = 24; int depth = 24; int storeys = 8; int storyHeight = 8; for (int y=0; y<storeys*storyHeight; y++) for (int x=0; x<width; x++) for (int z=0; z<depth; z++) { int block = 0; if (x==0 || z==0 || x==width-1 || z==depth-1) block = 1; if (y%storyHeight == storyHeight-1) block = 1; if (x>5 && z>5 && x<width-5 && z<depth-5) block = 0; if (y%storyHeight > 2 && y%storyHeight <= 4) { if (x%8 >= 2 && x%8 < 7) block = 0; if (z%8 >= 2 && z%8 < 7) block = 0; } if ((x==5 && z==5) || (x==width-5 && z==depth-5) || (x==5 && z==depth-5) || (x==width-5 && z==5) ) block = 4; if (block > 0) block = 4; pLevel->GetTile(x,y,z)->type = block; } int sx = 55; int sz = 55; pLevel->Recreate(); for (int x=0; x<pLevel->chunksX; x++) for (int y=0; y<pLevel->chunksY; y++) for (int z=0; z<pLevel->chunksZ; z++) { pLevel->GetChunk(x,y,z)->RecreateCollision(); } // once the map has been created, creatie bodies that will collide /*for (int x=0; x<pLevel->sizeX; x++) for (int y=0; y<pLevel->sizeY; y++) for (int z=0; z<pLevel->sizeZ; z++) { if (pLevel->GetTile(x,y,z)->type == 0) continue; CObject3D *o = new CObject3D(); o->SetPosition(Vector3(x+0.5, y+0.5, z+0.5)); NewtonBody *box = CPhysics::CreateBox(pWorld, o, 1,1,1, 0); NewtonBodySetFreezeState(box, 1); delete o; }*/ // let's create a collision tree for each chunk /* for (int x=0; x<pLevel->chunksX; x++) for (int y=0; y<pLevel->chunksY; y++) for (int z=0; z<pLevel->chunksZ; z++) { NewtonCollision * col = NewtonCreateTreeCollision(pWorld, 0); NewtonTreeCollisionBeginBuild(col); CArray<Vector3> &verts = pLevel->GetChunk(x,y,z)->pMesh->geometry->vertices; for (int i=0; i<pLevel->GetChunk(x,y,z)->pMesh->geometry->faces.Size(); i++) { Face3 face = pLevel->GetChunk(x,y,z)->pMesh->geometry->faces[i]; Vector3 v[] = { verts[face.a], verts[face.b], verts[face.c] }; NewtonTreeCollisionAddFace(col, 3, &v[0][0], sizeof(Vector3), 1); } NewtonTreeCollisionEndBuild(col, 1); // create body float m[16] = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 }; NewtonBody *body = NewtonCreateBody(pWorld, col, &m[0]); NewtonReleaseCollision(pWorld, col); NewtonBody } */ /* CreateBuilding(0,0,0, 3,8,3); CreateBuilding(20,0,-10, 2,4,6); CreateBuilding(-5,0,-20, 5,2,3); */ // point light that will circle the building pLight = new CPointLight(Vector3(), SRGBA(255,200,50)); pLight->range = 8.0f; pLight->overbright = true; pScene->Add(pLight); pScene->fog = new SFog( SRGBA(172,201,241, 255), 200, 500); // point light at 0,1,0 CLight *pLight = new CPointLight(Vector3(0,1,0), RED);//SRGBA(255,233,155,255)); //pScene->Add( pLight ); pLight->specular = pLight->color;//SRGBA(255,255,255,100); pLight->range = 1; pLight->intensity = 1; // directional Sun light CDirectionalLight *pDirLight = new CDirectionalLight(Vector3(0,0,0), SRGBA(255,225,175,255)); pDirLight->SetPosition(+70,90,-70); pDirLight->LookAt(Vector3()); pDirLight->UpdateMatrixWorld(true); pDirLight->shadowNear = 20; pDirLight->shadowFar = 200; pDirLight->castShadow = true; float aspect = (float)gEngine.width / gEngine.height; pDirLight->width = 200.0f; pDirLight->height = pDirLight->width / aspect; pScene->Add( pDirLight ); // ambient light pScene->ambientColor = SRGBA(200,200,255,255); pCamera->LookAt(Vector3()); }
//set collision void Body::setCollision( OgreNewt::Collision* col ) { NewtonBodySetCollision( m_body, col->getNewtonCollision() ); m_collision = col; }
void CustomVehicleController::Init (NewtonBody* const body, const dMatrix& vehicleFrame, const dVector& gravityVector) { m_body = body; m_finalized = false; m_externalContactStatesCount = 0; m_sleepCounter = VEHICLE_SLEEP_COUNTER; m_freeContactList = m_externalContactStatesPool.GetFirst(); CustomVehicleControllerManager* const manager = (CustomVehicleControllerManager*) GetManager(); NewtonWorld* const world = manager->GetWorld(); // create a compound collision NewtonCollision* const vehShape = NewtonCreateCompoundCollision(world, 0); NewtonCompoundCollisionBeginAddRemove(vehShape); // if the shape is a compound collision ass all the pieces one at a time NewtonCollision* const chassisShape = NewtonBodyGetCollision (m_body); int shapeType = NewtonCollisionGetType (chassisShape); if (shapeType == SERIALIZE_ID_COMPOUND) { for (void* node = NewtonCompoundCollisionGetFirstNode(chassisShape); node; node = NewtonCompoundCollisionGetNextNode (chassisShape, node)) { NewtonCollision* const subCollision = NewtonCompoundCollisionGetCollisionFromNode(chassisShape, node); NewtonCompoundCollisionAddSubCollision (vehShape, subCollision); } } else { dAssert ((shapeType == SERIALIZE_ID_CONVEXHULL) || (shapeType == SERIALIZE_ID_BOX)); NewtonCompoundCollisionAddSubCollision (vehShape, chassisShape); } NewtonCompoundCollisionEndAddRemove (vehShape); // replace the collision shape of the vehicle with this new one NewtonBodySetCollision(m_body, vehShape); // set linear and angular drag to zero dVector drag(0.0f, 0.0f, 0.0f, 0.0f); NewtonBodySetLinearDamping(m_body, 0); NewtonBodySetAngularDamping(m_body, &drag[0]); // destroy the collision help shape NewtonDestroyCollision (vehShape); // initialize vehicle internal components NewtonBodyGetCentreOfMass (m_body, &m_chassisState.m_com[0]); m_chassisState.m_com.m_w = 0.0f; m_chassisState.m_comOffset = dVector (0.0f, 0.0f, 0.0f, 0.0f); m_chassisState.m_gravity = gravityVector; m_chassisState.m_gravityMag = dSqrt (gravityVector % gravityVector); m_chassisState.Init(this, vehicleFrame); m_stateList.Append(&m_chassisState); // create the normalized size tire shape m_tireCastShape = NewtonCreateChamferCylinder(world, 0.5f, 1.0f, 0, NULL); // initialize all components to empty m_engine = NULL; m_brakes = NULL; m_steering = NULL; m_handBrakes = NULL; m_contactFilter = new CustomVehicleControllerTireCollisionFilter (this); SetDryRollingFrictionTorque (100.0f/4.0f); SetAerodynamicsDownforceCoefficient (0.5f * dSqrt (gravityVector % gravityVector), 60.0f * 0.447f); }