void Ragdolls::SpawnObject() { ResourceCache* cache = GetContext()->m_ResourceCache.get(); Node* boxNode = scene_->CreateChild("Sphere"); boxNode->SetPosition(cameraNode_->GetPosition()); boxNode->SetRotation(cameraNode_->GetRotation()); boxNode->SetScale(0.25f); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Sphere.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/StoneSmall.xml")); boxObject->SetCastShadows(true); RigidBody* body = boxNode->CreateComponent<RigidBody>(); body->SetMass(1.0f); body->SetRollingFriction(0.15f); CollisionShape* shape = boxNode->CreateComponent<CollisionShape>(); shape->SetSphere(1.0f); const float OBJECT_VELOCITY = 10.0f; // Set initial velocity for the RigidBody based on camera forward vector. Add also a slight up component // to overcome gravity better body->SetLinearVelocity(cameraNode_->GetRotation() * Vector3(0.0f, 0.25f, 1.0f) * OBJECT_VELOCITY); }
void Vehicle::Init() { // This function is called only from the main program when initially creating the vehicle, not on scene load ResourceCache* cache = GetSubsystem<ResourceCache>(); StaticModel* hullObject = node_->CreateComponent<StaticModel>(); hullBody_ = node_->CreateComponent<RigidBody>(); CollisionShape* hullShape = node_->CreateComponent<CollisionShape>(); node_->SetScale(Vector3(1.5f, 1.0f, 3.0f)); hullObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); hullObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); hullObject->SetCastShadows(true); hullShape->SetBox(Vector3::ONE); hullBody_->SetMass(4.0f); hullBody_->SetLinearDamping(0.2f); // Some air resistance hullBody_->SetAngularDamping(0.5f); hullBody_->SetCollisionLayer(1); InitWheel("FrontLeft", Vector3(-0.6f, -0.4f, 0.3f), frontLeft_, frontLeftID_); InitWheel("FrontRight", Vector3(0.6f, -0.4f, 0.3f), frontRight_, frontRightID_); InitWheel("RearLeft", Vector3(-0.6f, -0.4f, -0.3f), rearLeft_, rearLeftID_); InitWheel("RearRight", Vector3(0.6f, -0.4f, -0.3f), rearRight_, rearRightID_); GetWheelComponents(); }
void PhysicsStressTest::SpawnObject() { ResourceCache* cache = GetSubsystem<ResourceCache>(); // Create a smaller box at camera position Node* boxNode = scene_->CreateChild("SmallBox"); boxNode->SetPosition(cameraNode_->GetPosition()); boxNode->SetRotation(cameraNode_->GetRotation()); boxNode->SetScale(0.25f); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/StoneSmall.xml")); boxObject->SetCastShadows(true); // Create physics components, use a smaller mass also RigidBody* body = boxNode->CreateComponent<RigidBody>(); body->SetMass(0.25f); body->SetFriction(0.75f); CollisionShape* shape = boxNode->CreateComponent<CollisionShape>(); shape->SetBox(Vector3::ONE); const float OBJECT_VELOCITY = 10.0f; // Set initial velocity for the RigidBody based on camera forward vector. Add also a slight up component // to overcome gravity better body->SetLinearVelocity(cameraNode_->GetRotation() * Vector3(0.0f, 0.25f, 1.0f) * OBJECT_VELOCITY); }
Node* GameApplication::add_object(Node* pParentNode, const String& nodeName,enObjectType type,float x,float y,float z,const char* modelUrl,const char* material) { ResourceCache* cache = GetSubsystem<ResourceCache>(); Node* pNode = pParentNode->CreateChild(nodeName); pNode->SetPosition(Vector3(x, y, z)); if(type == enObjectType_StaticModel) { StaticModel* pModel = pNode->CreateComponent<StaticModel>(); pModel->SetModel(cache->GetResource<Model>(modelUrl)); if(material != NULL) pModel->SetMaterial(0,cache->GetResource<Material>(material)); pModel->SetCastShadows(true); } else { AnimatedModel* pAniModel = pNode->CreateComponent<AnimatedModel>(); pAniModel->SetModel(cache->GetResource<Model>(modelUrl)); if(material != NULL) pAniModel->SetMaterial(0,cache->GetResource<Material>(material)); pAniModel->SetCastShadows(true); } return pNode; }
void Board::InitSelection() { selectedBall_ = nullptr; selectionNode_ = node_->CreateChild("Selection"); selectionNode_->SetRotation(Quaternion(-90.f, 0.0f, 0.0f)); selectionNode_->SetEnabled(false); StaticModel* staticModel = selectionNode_->CreateComponent<StaticModel>(); staticModel->SetModel(SelectionModel); staticModel->SetMaterial(0, SelectionMaterial0); staticModel->SetMaterial(1, SelectionMaterial1); staticModel->SetCastShadows(true); }
Node* Urho3DTemplate::CreateMushroom(const Vector3 &pos) { ResourceCache* cache = GetSubsystem<ResourceCache>(); Node* mushroomNode = scene_->CreateChild("Mushroom"); mushroomNode->SetPosition(pos); mushroomNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f)); mushroomNode->SetScale(2.0f + Random(0.5f)); StaticModel* mushroomObject = mushroomNode->CreateComponent<StaticModel>(); mushroomObject->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl")); mushroomObject->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml")); mushroomObject->SetCastShadows(true); return mushroomNode; }
void Weapon::Setup() { ResourceCache* cache = GetSubsystem<ResourceCache>(); node_->SetPosition(Vector3(0.2f, 0.2f, 0.2f));//objectNode // Create the rendering component + animation controller //AnimatedModel* object = node_->CreateComponent<AnimatedModel>(); StaticModel* object = node_->CreateComponent<StaticModel>(); object->SetModel(cache->GetResource<Model>("Models/"+mesh_)); object->SetMaterial(cache->GetResource<Material>("Materials/"+material_+".xml")); object->SetCastShadows(true); lefthand_grip_ = node_->CreateChild("lefthand_grip"); lefthand_grip_->SetPosition(lefthand_off_); }
void Vehicle::InitWheel(const String& name, const Vector3& offset, WeakPtr<Node>& wheelNode, unsigned& wheelNodeID) { ResourceCache* cache = GetSubsystem<ResourceCache>(); // Note: do not parent the wheel to the hull scene node. Instead create it on the root level and let the physics // constraint keep it together wheelNode = GetScene()->CreateChild(name); wheelNode->SetPosition(node_->LocalToWorld(offset)); wheelNode->SetRotation(node_->GetRotation() * (offset.x_ >= 0.0 ? Quaternion(0.0f, 0.0f, -90.0f) : Quaternion(0.0f, 0.0f, 90.0f))); wheelNode->SetScale(Vector3(0.8f, 0.5f, 0.8f)); // Remember the ID for serialization wheelNodeID = wheelNode->GetID(); StaticModel* wheelObject = wheelNode->CreateComponent<StaticModel>(); RigidBody* wheelBody = wheelNode->CreateComponent<RigidBody>(); CollisionShape* wheelShape = wheelNode->CreateComponent<CollisionShape>(); Constraint* wheelConstraint = wheelNode->CreateComponent<Constraint>(); wheelObject->SetModel(cache->GetResource<Model>("Models/Cylinder.mdl")); wheelObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); wheelObject->SetCastShadows(true); wheelShape->SetSphere(1.0f); wheelBody->SetFriction(1.0f); wheelBody->SetMass(1.0f); wheelBody->SetLinearDamping(0.2f); // Some air resistance wheelBody->SetAngularDamping(0.75f); // Could also use rolling friction wheelBody->SetCollisionLayer(1); wheelConstraint->SetConstraintType(CONSTRAINT_HINGE); wheelConstraint->SetOtherBody(GetComponent<RigidBody>()); // Connect to the hull body wheelConstraint->SetWorldPosition(wheelNode->GetPosition()); // Set constraint's both ends at wheel's location wheelConstraint->SetAxis(Vector3::UP); // Wheel rotates around its local Y-axis wheelConstraint->SetOtherAxis(offset.x_ >= 0.0 ? Vector3::RIGHT : Vector3::LEFT); // Wheel's hull axis points either left or right wheelConstraint->SetLowLimit(Vector2(-180.0f, 0.0f)); // Let the wheel rotate freely around the axis wheelConstraint->SetHighLimit(Vector2(180.0f, 0.0f)); wheelConstraint->SetDisableCollision(true); // Let the wheel intersect the vehicle hull }
void PhysicsStressTest::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000) // Create a physics simulation world with default parameters, which will update at 60fps. Like the Octree must // exist before creating drawable components, the PhysicsWorld must exist before creating physics components. // Finally, create a DebugRenderer component so that we can draw physics debug geometry scene_->CreateComponent<Octree>(); scene_->CreateComponent<PhysicsWorld>(); scene_->CreateComponent<DebugRenderer>(); // Create a Zone component for ambient lighting & fog control Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.5f, 0.5f, 0.7f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.0f); // Create a directional light to the world. Enable cascaded shadows on it Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.00025f, 0.5f)); // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f)); { // Create a floor object, 500 x 500 world units. Adjust position so that the ground is at zero Y Node* floorNode = scene_->CreateChild("Floor"); floorNode->SetPosition(Vector3(0.0f, -0.5f, 0.0f)); floorNode->SetScale(Vector3(500.0f, 1.0f, 500.0f)); StaticModel* floorObject = floorNode->CreateComponent<StaticModel>(); floorObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); floorObject->SetMaterial(cache->GetResource<Material>("Materials/StoneTiled.xml")); // Make the floor physical by adding RigidBody and CollisionShape components RigidBody* body = floorNode->CreateComponent<RigidBody>(); CollisionShape* shape = floorNode->CreateComponent<CollisionShape>(); shape->SetBox(Vector3::ONE); } { // Create static mushrooms with triangle mesh collision const unsigned NUM_MUSHROOMS = 50; for (unsigned i = 0; i < NUM_MUSHROOMS; ++i) { Node* mushroomNode = scene_->CreateChild("Mushroom"); mushroomNode->SetPosition(Vector3(Random(400.0f) - 200.0f, 0.0f, Random(400.0f) - 200.0f)); mushroomNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f)); mushroomNode->SetScale(5.0f + Random(5.0f)); StaticModel* mushroomObject = mushroomNode->CreateComponent<StaticModel>(); mushroomObject->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl")); mushroomObject->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml")); mushroomObject->SetCastShadows(true); RigidBody* body = mushroomNode->CreateComponent<RigidBody>(); CollisionShape* shape = mushroomNode->CreateComponent<CollisionShape>(); // By default the highest LOD level will be used, the LOD level can be passed as an optional parameter shape->SetTriangleMesh(mushroomObject->GetModel()); } } { // Create a large amount of falling physics objects const unsigned NUM_OBJECTS = 1000; for (unsigned i = 0; i < NUM_OBJECTS; ++i) { Node* boxNode = scene_->CreateChild("Box"); boxNode->SetPosition(Vector3(0.0f, i * 2.0f + 100.0f, 0.0f)); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/StoneSmall.xml")); boxObject->SetCastShadows(true); // Give the RigidBody mass to make it movable and also adjust friction RigidBody* body = boxNode->CreateComponent<RigidBody>(); body->SetMass(1.0f); body->SetFriction(1.0f); // Disable collision event signaling to reduce CPU load of the physics simulation body->SetCollisionEventMode(COLLISION_NEVER); CollisionShape* shape = boxNode->CreateComponent<CollisionShape>(); shape->SetBox(Vector3::ONE); } } // Create the camera. Limit far clip distance to match the fog. Note: now we actually create the camera node outside // the scene, because we want it to be unaffected by scene load / save cameraNode_ = new Node(context_); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(300.0f); // Set an initial position for the camera scene node above the floor cameraNode_->SetPosition(Vector3(0.0f, 3.0f, -20.0f)); }
void Physics::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000) // Create a physics simulation world with default parameters, which will update at 60fps. Like the Octree must // exist before creating drawable components, the PhysicsWorld must exist before creating physics components. // Finally, create a DebugRenderer component so that we can draw physics debug geometry scene_->CreateComponent<Octree>(); scene_->CreateComponent<PhysicsWorld>(); scene_->CreateComponent<DebugRenderer>(); // Create a Zone component for ambient lighting & fog control Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(1.0f, 1.0f, 1.0f)); zone->SetFogStart(300.0f); zone->SetFogEnd(500.0f); // Create a directional light to the world. Enable cascaded shadows on it Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.00025f, 0.5f)); // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f)); // Create skybox. The Skybox component is used like StaticModel, but it will be always located at the camera, giving the // illusion of the box planes being far away. Use just the ordinary Box model and a suitable material, whose shader will // generate the necessary 3D texture coordinates for cube mapping Node* skyNode = scene_->CreateChild("Sky"); skyNode->SetScale(500.0f); // The scale actually does not matter Skybox* skybox = skyNode->CreateComponent<Skybox>(); skybox->SetModel(cache->GetResource<Model>("Models/Box.mdl")); skybox->SetMaterial(cache->GetResource<Material>("Materials/Skybox.xml")); { // Create a floor object, 1000 x 1000 world units. Adjust position so that the ground is at zero Y Node* floorNode = scene_->CreateChild("Floor"); floorNode->SetPosition(Vector3(0.0f, -0.5f, 0.0f)); floorNode->SetScale(Vector3(1000.0f, 1.0f, 1000.0f)); StaticModel* floorObject = floorNode->CreateComponent<StaticModel>(); floorObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); floorObject->SetMaterial(cache->GetResource<Material>("Materials/StoneTiled.xml")); // Make the floor physical by adding RigidBody and CollisionShape components. The RigidBody's default // parameters make the object static (zero mass.) Note that a CollisionShape by itself will not participate // in the physics simulation /*RigidBody* body = */floorNode->CreateComponent<RigidBody>(); CollisionShape* shape = floorNode->CreateComponent<CollisionShape>(); // Set a box shape of size 1 x 1 x 1 for collision. The shape will be scaled with the scene node scale, so the // rendering and physics representation sizes should match (the box model is also 1 x 1 x 1.) shape->SetBox(Vector3::ONE); } { // Create a pyramid of movable physics objects for (int y = 0; y < 8; ++y) { for (int x = -y; x <= y; ++x) { Node* boxNode = scene_->CreateChild("Box"); boxNode->SetPosition(Vector3((float)x, -(float)y + 8.0f, 0.0f)); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/StoneEnvMapSmall.xml")); boxObject->SetCastShadows(true); // Create RigidBody and CollisionShape components like above. Give the RigidBody mass to make it movable // and also adjust friction. The actual mass is not important; only the mass ratios between colliding // objects are significant RigidBody* body = boxNode->CreateComponent<RigidBody>(); body->SetMass(1.0f); body->SetFriction(0.75f); CollisionShape* shape = boxNode->CreateComponent<CollisionShape>(); shape->SetBox(Vector3::ONE); } } } // Create the camera. Set far clip to match the fog. Note: now we actually create the camera node outside the scene, because // we want it to be unaffected by scene load / save cameraNode_ = new Node(context_); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(500.0f); // Set an initial position for the camera scene node above the floor cameraNode_->SetPosition(Vector3(0.0f, 5.0f, -20.0f)); }
void VehicleDemo::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // Create scene subsystem components scene_->CreateComponent<Octree>(); scene_->CreateComponent<PhysicsWorld>(); // Create camera and define viewport. We will be doing load / save, so it's convenient to create the camera outside the scene, // so that it won't be destroyed and recreated, and we don't have to redefine the viewport on load cameraNode_ = new Node(context_); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(500.0f); GetSubsystem<Renderer>()->SetViewport(0, new Viewport(context_, scene_, camera)); // Create static scene content. First create a zone for ambient lighting and fog control Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.5f, 0.5f, 0.7f)); zone->SetFogStart(300.0f); zone->SetFogEnd(500.0f); zone->SetBoundingBox(BoundingBox(-2000.0f, 2000.0f)); // Create a directional light with cascaded shadow mapping Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.3f, -0.5f, 0.425f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.00025f, 0.5f)); light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f)); light->SetSpecularIntensity(0.5f); // Create heightmap terrain with collision Node* terrainNode = scene_->CreateChild("Terrain"); terrainNode->SetPosition(Vector3::ZERO); Terrain* terrain = terrainNode->CreateComponent<Terrain>(); terrain->SetPatchSize(64); terrain->SetSpacing(Vector3(2.0f, 0.1f, 2.0f)); // Spacing between vertices and vertical resolution of the height map terrain->SetSmoothing(true); terrain->SetHeightMap(cache->GetResource<Image>("Textures/HeightMap.png")); terrain->SetMaterial(cache->GetResource<Material>("Materials/Terrain.xml")); // The terrain consists of large triangles, which fits well for occlusion rendering, as a hill can occlude all // terrain patches and other objects behind it terrain->SetOccluder(true); RigidBody* body = terrainNode->CreateComponent<RigidBody>(); body->SetCollisionLayer(2); // Use layer bitmask 2 for static geometry CollisionShape* shape = terrainNode->CreateComponent<CollisionShape>(); shape->SetTerrain(); // Create 1000 mushrooms in the terrain. Always face outward along the terrain normal const unsigned NUM_MUSHROOMS = 1000; for (unsigned i = 0; i < NUM_MUSHROOMS; ++i) { Node* objectNode = scene_->CreateChild("Mushroom"); Vector3 position(Random(2000.0f) - 1000.0f, 0.0f, Random(2000.0f) - 1000.0f); position.y_ = terrain->GetHeight(position) - 0.1f; objectNode->SetPosition(position); // Create a rotation quaternion from up vector to terrain normal objectNode->SetRotation(Quaternion(Vector3::UP, terrain->GetNormal(position))); objectNode->SetScale(3.0f); StaticModel* object = objectNode->CreateComponent<StaticModel>(); object->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl")); object->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml")); object->SetCastShadows(true); RigidBody* body = objectNode->CreateComponent<RigidBody>(); body->SetCollisionLayer(2); CollisionShape* shape = objectNode->CreateComponent<CollisionShape>(); shape->SetTriangleMesh(object->GetModel(), 0); } }
void MultipleViewports::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000) // Also create a DebugRenderer component so that we can draw debug geometry scene_->CreateComponent<Octree>(); scene_->CreateComponent<DebugRenderer>(); // Create scene node & StaticModel component for showing a static plane Node* planeNode = scene_->CreateChild("Plane"); planeNode->SetScale(Vector3(100.0f, 1.0f, 100.0f)); StaticModel* planeObject = planeNode->CreateComponent<StaticModel>(); planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl")); planeObject->SetMaterial(cache->GetResource<Material>("Materials/StoneTiled.xml")); // Create a Zone component for ambient lighting & fog control Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.5f, 0.5f, 0.7f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.0f); // Create a directional light to the world. Enable cascaded shadows on it Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.00025f, 0.5f)); // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f)); // Create some mushrooms const unsigned NUM_MUSHROOMS = 240; for (unsigned i = 0; i < NUM_MUSHROOMS; ++i) { Node* mushroomNode = scene_->CreateChild("Mushroom"); mushroomNode->SetPosition(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f)); mushroomNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f)); mushroomNode->SetScale(0.5f + Random(2.0f)); StaticModel* mushroomObject = mushroomNode->CreateComponent<StaticModel>(); mushroomObject->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl")); mushroomObject->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml")); mushroomObject->SetCastShadows(true); } // Create randomly sized boxes. If boxes are big enough, make them occluders const unsigned NUM_BOXES = 20; for (unsigned i = 0; i < NUM_BOXES; ++i) { Node* boxNode = scene_->CreateChild("Box"); float size = 1.0f + Random(10.0f); boxNode->SetPosition(Vector3(Random(80.0f) - 40.0f, size * 0.5f, Random(80.0f) - 40.0f)); boxNode->SetScale(size); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); boxObject->SetCastShadows(true); if (size >= 3.0f) boxObject->SetOccluder(true); } // Create the cameras. Limit far clip distance to match the fog cameraNode_ = scene_->CreateChild("Camera"); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(300.0f); // Parent the rear camera node to the front camera node and turn it 180 degrees to face backward // Here, we use the angle-axis constructor for Quaternion instead of the usual Euler angles rearCameraNode_ = cameraNode_->CreateChild("RearCamera"); rearCameraNode_->Rotate(Quaternion(180.0f, Vector3::UP)); Camera* rearCamera = rearCameraNode_->CreateComponent<Camera>(); rearCamera->SetFarClip(300.0f); // Because the rear viewport is rather small, disable occlusion culling from it. Use the camera's // "view override flags" for this. We could also disable eg. shadows or force low material quality // if we wanted rearCamera->SetViewOverrideFlags(VO_DISABLE_OCCLUSION); // Set an initial position for the front camera scene node above the plane cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f)); }
void Decals::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000) // Also create a DebugRenderer component so that we can draw debug geometry scene_->CreateComponent<Octree>(); scene_->CreateComponent<DebugRenderer>(); // Create scene node & StaticModel component for showing a static plane Node* planeNode = scene_->CreateChild("Plane"); planeNode->SetScale(Vector3(100.0f, 1.0f, 100.0f)); StaticModel* planeObject = planeNode->CreateComponent<StaticModel>(); planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl")); planeObject->SetMaterial(cache->GetResource<Material>("Materials/StoneTiled.xml")); // Create a Zone component for ambient lighting & fog control Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.5f, 0.5f, 0.7f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.0f); // Create a directional light to the world. Enable cascaded shadows on it Node* lightNode = scene_->CreateChild("Directional light"); lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.0001f, 0.5f)); // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f)); // Create some mushrooms const unsigned NUM_MUSHROOMS = 240; for (unsigned i = 0; i < NUM_MUSHROOMS; ++i) { Node* mushroomNode = scene_->CreateChild("Mushroom"); mushroomNode->SetPosition(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f)); mushroomNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f)); mushroomNode->SetScale(0.5f + Random(2.0f)); StaticModel* mushroomObject = mushroomNode->CreateComponent<StaticModel>(); mushroomObject->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl")); mushroomObject->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml")); mushroomObject->SetCastShadows(true); } // Create randomly sized boxes. If boxes are big enough, make them occluders. Occluders will be software rasterized before // rendering to a low-resolution depth-only buffer to test the objects in the view frustum for visibility const unsigned NUM_BOXES = 20; for (unsigned i = 0; i < NUM_BOXES; ++i) { Node* boxNode = scene_->CreateChild("Box"); float size = 1.0f + Random(10.0f); boxNode->SetPosition(Vector3(Random(80.0f) - 40.0f, size * 0.5f, Random(80.0f) - 40.0f)); boxNode->SetScale(size); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); boxObject->SetCastShadows(true); if (size >= 5.0f) boxObject->SetOccluder(true); } // Create the camera. Limit far clip distance to match the fog cameraNode_ = scene_->CreateChild("Camera"); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(300.0f); // Set an initial position for the camera scene node above the plane cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f)); }
void Urho3DTemplate::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); //Create octree, use default volume (-1000, -1000, -1000) to (1000,1000,1000) //Also create a DebugRenderer component so that we can draw debug geometry scene_->CreateComponent<Octree>(); scene_->CreateComponent<DebugRenderer>(); //Create scene node & StaticModel component for showing a static plane Node* planeNode = scene_->CreateChild("Plane"); planeNode->SetScale(Vector3(100.0f, 1.0f, 100.0f)); StaticModel* planeObject = planeNode->CreateComponent<StaticModel>(); planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl")); planeObject->SetMaterial(cache->GetResource<Material>("Materials/StoneTiled.xml")); //Create a Zone component for ambient lighting & fog control Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.5f, 0.5f, 0.7f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.0f); //Create a directional light to the world. Enable cascaded shadows on it Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.00025f, 0.5f)); //Set cascade splits at 10, 50, 200 world unitys, fade shadows at 80% of maximum shadow distance light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f)); //Create some mushrooms const unsigned NUM_MUSHROOMS = 100; for (unsigned i = 0; i < NUM_MUSHROOMS; ++i) CreateMushroom(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f)); //Create randomly sized boxes. If boxes are big enough make them occluders const unsigned NUM_BOXES = 20; for (unsigned i = 0; i <NUM_BOXES; ++i) { Node* boxNode = scene_->CreateChild("Box"); float size = 1.0f + Random(10.0f); boxNode->SetPosition(Vector3(Random(80.0f) - 40.0f, size * 0.5f, Random(80.0f) - 40.0f)); boxNode->SetScale(size); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); boxObject->SetCastShadows(true); if (size >= 3.0f) boxObject->SetOccluder(true); } //Create Jack node that will follow the path jackNode_ = scene_->CreateChild("Jack"); jackNode_->SetPosition(Vector3(-5.0f, 0.0f, 20.0f)); AnimatedModel* modelObject = jackNode_->CreateComponent<AnimatedModel>(); modelObject->SetModel(cache->GetResource<Model>("Model/Jack.mdl")); modelObject->SetMaterial(cache->GetResource<Material>("Materials/Jack.xml")); modelObject->SetCastShadows(true); //Create the camera. Limit far clip distance to match the fog cameraNode_ = scene_->CreateChild("Camera"); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(300.0f); //Set an initial position for the camera scene node above the plane cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f)); }
void Navigation::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000) // Also create a DebugRenderer component so that we can draw debug geometry scene_->CreateComponent<Octree>(); scene_->CreateComponent<DebugRenderer>(); // Create scene node & StaticModel component for showing a static plane Node* planeNode = scene_->CreateChild("Plane"); planeNode->SetScale(Vector3(100.0f, 1.0f, 100.0f)); StaticModel* planeObject = planeNode->CreateComponent<StaticModel>(); planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl")); planeObject->SetMaterial(cache->GetResource<Material>("Materials/StoneTiled.xml")); // Create a Zone component for ambient lighting & fog control Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.5f, 0.5f, 0.7f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.0f); // Create a directional light to the world. Enable cascaded shadows on it Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.0001f, 0.5f)); // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f)); // Create some mushrooms const unsigned NUM_MUSHROOMS = 100; for (unsigned i = 0; i < NUM_MUSHROOMS; ++i) CreateMushroom(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f)); // Create randomly sized boxes. If boxes are big enough, make them occluders const unsigned NUM_BOXES = 20; for (unsigned i = 0; i < NUM_BOXES; ++i) { Node* boxNode = scene_->CreateChild("Box"); float size = 1.0f + Random(10.0f); boxNode->SetPosition(Vector3(Random(80.0f) - 40.0f, size * 0.5f, Random(80.0f) - 40.0f)); boxNode->SetScale(size); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); boxObject->SetCastShadows(true); if (size >= 3.0f) boxObject->SetOccluder(true); } // Create a NavigationMesh component to the scene root NavigationMesh* navMesh = scene_->CreateComponent<NavigationMesh>(); // Create a Navigable component to the scene root. This tags all of the geometry in the scene as being part of the // navigation mesh. By default this is recursive, but the recursion could be turned off from Navigable scene_->CreateComponent<Navigable>(); // Add padding to the navigation mesh in Y-direction so that we can add objects on top of the tallest boxes // in the scene and still update the mesh correctly navMesh->SetPadding(Vector3(0.0f, 10.0f, 0.0f)); // Now build the navigation geometry. This will take some time. Note that the navigation mesh will prefer to use // physics geometry from the scene nodes, as it often is simpler, but if it can not find any (like in this example) // it will use renderable geometry instead navMesh->Build(); // Create the camera. Limit far clip distance to match the fog cameraNode_ = scene_->CreateChild("Camera"); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(300.0f); // Set an initial position for the camera scene node above the plane cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f)); }
void Water::CreateScene() { ResourceCache* cache = GetContext()->m_ResourceCache.get(); scene_ = new Scene(GetContext()); // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000) scene_->CreateComponent<Octree>(); // Create a Zone component for ambient lighting & fog control Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(1.0f, 1.0f, 1.0f)); zone->SetFogStart(500.0f); zone->SetFogEnd(750.0f); // Create a directional light to the world. Enable cascaded shadows on it Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.00025f, 0.5f)); light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f)); light->SetSpecularIntensity(0.5f); // Apply slightly overbright lighting to match the skybox light->SetColor(Color(1.2f, 1.2f, 1.2f)); // Create skybox. The Skybox component is used like StaticModel, but it will be always located at the camera, giving the // illusion of the box planes being far away. Use just the ordinary Box model and a suitable material, whose shader will // generate the necessary 3D texture coordinates for cube mapping Node* skyNode = scene_->CreateChild("Sky"); skyNode->SetScale(500.0f); // The scale actually does not matter Skybox* skybox = skyNode->CreateComponent<Skybox>(); skybox->SetModel(cache->GetResource<Model>("Models/Box.mdl")); skybox->SetMaterial(cache->GetResource<Material>("Materials/Skybox.xml")); // Create heightmap terrain Node* terrainNode = scene_->CreateChild("Terrain"); terrainNode->SetPosition(Vector3(0.0f, 0.0f, 0.0f)); Terrain* terrain = terrainNode->CreateComponent<Terrain>(); terrain->SetPatchSize(64); terrain->SetSpacing(Vector3(2.0f, 0.5f, 2.0f)); // Spacing between vertices and vertical resolution of the height map terrain->SetSmoothing(true); terrain->SetHeightMap(cache->GetResource<Image>("Textures/HeightMap.png")); terrain->SetMaterial(cache->GetResource<Material>("Materials/Terrain.xml")); // The terrain consists of large triangles, which fits well for occlusion rendering, as a hill can occlude all // terrain patches and other objects behind it terrain->SetOccluder(true); // Create 1000 boxes in the terrain. Always face outward along the terrain normal unsigned NUM_OBJECTS = 1000; for (unsigned i = 0; i < NUM_OBJECTS; ++i) { Node* objectNode = scene_->CreateChild("Box"); Vector3 position(Random(2000.0f) - 1000.0f, 0.0f, Random(2000.0f) - 1000.0f); position.y_ = terrain->GetHeight(position) + 2.25f; objectNode->SetPosition(position); // Create a rotation quaternion from up vector to terrain normal objectNode->SetRotation(Quaternion(Vector3(0.0f, 1.0f, 0.0f), terrain->GetNormal(position))); objectNode->SetScale(5.0f); StaticModel* object = objectNode->CreateComponent<StaticModel>(); object->SetModel(cache->GetResource<Model>("Models/Box.mdl")); object->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); object->SetCastShadows(true); } Node* shipNode = scene_->CreateChild("Ship"); shipNode->SetPosition(Vector3(0.0f, 4.6f, 0.0f)); //shipNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f)); shipNode->SetScale(0.5f + Random(2.0f)); StaticModel* shipObject = shipNode->CreateComponent<StaticModel>(); shipObject->SetModel(cache->GetResource<Model>("Models/ship04.mdl")); shipObject->SetMaterial(0,cache->GetResource<Material>("Materials/ship04_Material0.xml")); shipObject->SetMaterial(1,cache->GetResource<Material>("Materials/ship04_Material1.xml")); shipObject->SetMaterial(2,cache->GetResource<Material>("Materials/ship04_Material2.xml")); shipObject->SetCastShadows(true); // Create a water plane object that is as large as the terrain waterNode_ = scene_->CreateChild("Water"); waterNode_->SetScale(Vector3(2048.0f, 1.0f, 2048.0f)); waterNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f)); StaticModel* water = waterNode_->CreateComponent<StaticModel>(); water->SetModel(cache->GetResource<Model>("Models/Plane.mdl")); water->SetMaterial(cache->GetResource<Material>("Materials/Water.xml")); // Set a different viewmask on the water plane to be able to hide it from the reflection camera water->SetViewMask(0x80000000); // Create the camera. Set far clip to match the fog. Note: now we actually create the camera node outside // the scene, because we want it to be unaffected by scene load / save cameraNode_ = new Node(GetContext()); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->setFarClipDistance(750.0f); // Set an initial position for the camera scene node above the ground cameraNode_->SetPosition(Vector3(0.0f, 7.0f, -20.0f)); }
//============================================================================= //============================================================================= void Vehicle::Init() { // This function is called only from the main program when initially creating the vehicle, not on scene load ResourceCache* cache = GetSubsystem<ResourceCache>(); StaticModel* hullObject = node_->CreateComponent<StaticModel>(); hullBody_ = node_->CreateComponent<RigidBody>(); CollisionShape* hullColShape = node_->CreateComponent<CollisionShape>(); hullBody_->SetMass(1200.0f); hullBody_->SetLinearDamping(0.2f); // Some air resistance hullBody_->SetAngularDamping(0.5f); hullBody_->SetCollisionLayer(1); int rightIndex = 0; int upIndex = 1; int forwardIndex = 2; PhysicsWorld *pPhysWorld = GetScene()->GetComponent<PhysicsWorld>(); btDynamicsWorld *pbtDynWorld = (btDynamicsWorld*)pPhysWorld->GetWorld(); m_vehicleRayCaster = new btDefaultVehicleRaycaster( pbtDynWorld ); m_vehicle = new btRaycastVehicle( m_tuning, hullBody_->GetBody(), m_vehicleRayCaster ); pbtDynWorld->addVehicle( m_vehicle ); m_vehicle->setCoordinateSystem( rightIndex, upIndex, forwardIndex ); //Vector3 v3BoxExtents = Vector3::ONE;//Vector3(1.5f, 1.0f, 3.0f); hullObject->SetModel(cache->GetResource<Model>("MyProjects/MiniCooper/test/Chassis_001.mdl")); node_->SetScale( Vector3(1.0f, 1.0f, 1.0f) ); hullColShape->SetConvexHull(cache->GetResource<Model>("MyProjects/MiniCooper/test/collision.mdl")); //hullColShape->SetConvexHull(cache->GetResource<Model>("Models/Box.mdl")); //hullColShape->SetBox((hullObject->GetBoundingBox()).Size()); //hullColShape->SetTriangleMesh(cache->GetResource<Model>("Models/Box.mdl")); //hullColShape->SetSize(Vector3(1.0f, 1.0f, 3.0f)); //hullColShape->SetPosition((hullObject->GetBoundingBox()).Center() - Vector3(0.0f, 0.0f, 0.0f) ); //hullColShape->SetPosition( Vector3(0.0f, +0.2f, 0.0f) ); hullObject->SetCastShadows(true); Node* patricleTest = node_->CreateChild("patricleTest"); patricleTest->LoadXML(cache->GetResource<XMLFile>("MyProjects/MiniCooper/particleTest.xml")->GetRoot()); patricleTest->SetPosition(Vector3(0.0f, 0.2f, -2.0f)); Node* lightTest = node_->CreateChild("lightTest"); lightTest->LoadXML(cache->GetResource<XMLFile>("MyProjects/MiniCooper/lightTest.xml")->GetRoot()); lightTest->SetPosition(Vector3(0.0f, 0.2f, 2.0f)); //float connectionHeight = -0.4f;//1.2f; //float connectionHeight = 0.0f; bool isFrontWheel=true; btVector3 wheelDirectionCS0(0,-1,0); btVector3 wheelAxleCS(1,0,0); double wheelOffset = 0.5; // front right ////////////// Node* node_wheel_temp0 = GetScene()->CreateChild("node_wheel_temp0"); StaticModel* model_wheel_temp0 = node_wheel_temp0->CreateComponent<StaticModel>(); model_wheel_temp0->SetModel(cache->GetResource<Model>("MyProjects/MiniCooper/test/wheel_000.mdl")); model_wheel_temp0->ApplyMaterialList("MyProjects/MiniCooper/test/wheel_000.txt"); model_wheel_temp0->SetCastShadows(true); //btVector3 connectionPointCS0(((model_wheel_temp0->GetBoundingBox()).Center()).x_, ((model_wheel_temp0->GetBoundingBox()).Center()).y_, ((model_wheel_temp0->GetBoundingBox()).Center()).z_); //wheelRadius = model_wheel_temp0->GetBoundingBox().HalfSize().y_; //m_vehicle->addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, m_tuning, isFrontWheel); btVector3 connectionPointCS0((model_wheel_temp0->GetBoundingBox()).Center().x_,((model_wheel_temp0->GetBoundingBox()).Center()).y_ + wheelOffset,((model_wheel_temp0->GetBoundingBox()).Center()).z_); m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,btVector3(-1,0,0),suspensionRestLength,wheelRadius,m_tuning,isFrontWheel); Node* node_wheel_0 = GetScene()->CreateChild("node_wheel_0"); node_wheel_0->SetPosition((model_wheel_temp0->GetBoundingBox()).Center()); node_wheel_0->SetRotation(Quaternion(0.0f, 0.0f, 90.0f)); node_wheel_temp0->SetParent(node_wheel_0); m_vpNodeWheel.Push( node_wheel_0 ); //SDL_Log( "node_wheel_temp0: %f, %f, %f \n", ((model_wheel_temp0->GetBoundingBox()).Center()).x_, ((model_wheel_temp0->GetBoundingBox()).Center()).y_, ((model_wheel_temp0->GetBoundingBox()).Center()).z_); //SDL_Log( "node_wheel_temp0.HalfSize: %s \n", model_wheel_temp0->GetBoundingBox().HalfSize().ToString().CString() ); //SDL_Log("##################### \n"); //SDL_Log("node_wheel_temp0WorldP: %f, %f, %f \n", node_wheel_temp0->GetWorldPosition().x_, node_wheel_temp0->GetWorldPosition().y_, node_wheel_temp0->GetWorldPosition().z_); //SDL_Log("node_wheel_temp0P: %f, %f, %f \n", node_wheel_temp0->GetPosition().x_, node_wheel_temp0->GetPosition().y_, node_wheel_temp0->GetPosition().z_); //SDL_Log( "model_wheel_temp0BoundP: %f, %f, %f \n", ((model_wheel_temp0->GetBoundingBox()).Center()).x_, ((model_wheel_temp0->GetBoundingBox()).Center()).y_, ((model_wheel_temp0->GetBoundingBox()).Center()).z_); //node_wheel_temp0->SetPosition(node_wheel_temp0->GetPosition() - (model_wheel_temp0->GetBoundingBox()).Center()); //SDL_Log("transform: %f, %f, %f \n", v3Origin.x_, v3Origin.y_, v3Origin.z_); //v3Origin = v3Origin - Vector3(0.704, 0.379, 1.19); //SDL_Log("transform: %f, %f, %f \n", v3Origin.x_, v3Origin.y_, v3Origin.z_); // front left ///////////// Node* node_wheel_temp1 = GetScene()->CreateChild("node_wheel_temp1"); StaticModel* model_wheel_temp1 = node_wheel_temp1->CreateComponent<StaticModel>(); model_wheel_temp1->SetModel(cache->GetResource<Model>("MyProjects/MiniCooper/test/wheel_001.mdl")); model_wheel_temp1->SetCastShadows(true); //btVector3 connectionPointCS0(((model_wheel_temp0->GetBoundingBox()).Center()).x_, ((model_wheel_temp0->GetBoundingBox()).Center()).y_, ((model_wheel_temp0->GetBoundingBox()).Center()).z_); //wheelRadius = model_wheel_temp0->GetBoundingBox().HalfSize().y_; //m_vehicle->addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, m_tuning, isFrontWheel); connectionPointCS0 = btVector3((model_wheel_temp1->GetBoundingBox()).Center().x_,((model_wheel_temp1->GetBoundingBox()).Center()).y_ + wheelOffset,((model_wheel_temp1->GetBoundingBox()).Center()).z_); m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,btVector3(-1,0,0),suspensionRestLength,wheelRadius,m_tuning,isFrontWheel); Node* node_wheel_1 = GetScene()->CreateChild("node_wheel_1"); node_wheel_1->SetPosition((model_wheel_temp1->GetBoundingBox()).Center()); node_wheel_1->SetRotation(Quaternion(0.0f, 0.0f, -90.0f)); node_wheel_temp1->SetParent(node_wheel_1); m_vpNodeWheel.Push( node_wheel_1 ); isFrontWheel = false; // back right ///////////// Node* node_wheel_temp2 = GetScene()->CreateChild("node_wheel_temp2"); StaticModel* model_wheel_temp2 = node_wheel_temp2->CreateComponent<StaticModel>(); model_wheel_temp2->SetModel(cache->GetResource<Model>("MyProjects/MiniCooper/test/wheel_002.mdl")); model_wheel_temp2->SetCastShadows(true); //btVector3 connectionPointCS0(((model_wheel_temp0->GetBoundingBox()).Center()).x_, ((model_wheel_temp0->GetBoundingBox()).Center()).y_, ((model_wheel_temp0->GetBoundingBox()).Center()).z_); //wheelRadius = model_wheel_temp0->GetBoundingBox().HalfSize().y_; //m_vehicle->addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, m_tuning, isFrontWheel); connectionPointCS0 = btVector3((model_wheel_temp2->GetBoundingBox()).Center().x_,((model_wheel_temp2->GetBoundingBox()).Center()).y_ + wheelOffset,((model_wheel_temp2->GetBoundingBox()).Center()).z_); m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,btVector3(-1,0,0),suspensionRestLength,wheelRadius,m_tuning,isFrontWheel); Node* node_wheel_2 = GetScene()->CreateChild("node_wheel_2"); node_wheel_2->SetPosition((model_wheel_temp2->GetBoundingBox()).Center()); node_wheel_2->SetRotation(Quaternion(0.0f, 0.0f, 90.0f)); node_wheel_temp2->SetParent(node_wheel_2); m_vpNodeWheel.Push( node_wheel_2 ); // back left ///////////// /// \brief objectNode3 Node* node_wheel_temp3 = GetScene()->CreateChild("node_wheel_temp3"); StaticModel* model_wheel_temp3 = node_wheel_temp3->CreateComponent<StaticModel>(); model_wheel_temp3->SetModel(cache->GetResource<Model>("MyProjects/MiniCooper/test/wheel_003.mdl")); model_wheel_temp3->SetCastShadows(true); //btVector3 connectionPointCS0(((model_wheel_temp0->GetBoundingBox()).Center()).x_, ((model_wheel_temp0->GetBoundingBox()).Center()).y_, ((model_wheel_temp0->GetBoundingBox()).Center()).z_); //wheelRadius = model_wheel_temp0->GetBoundingBox().HalfSize().y_; //m_vehicle->addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, m_tuning, isFrontWheel); connectionPointCS0 = btVector3((model_wheel_temp3->GetBoundingBox()).Center().x_,((model_wheel_temp3->GetBoundingBox()).Center()).y_ + wheelOffset,((model_wheel_temp3->GetBoundingBox()).Center()).z_); m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,btVector3(-1,0,0),suspensionRestLength,wheelRadius,m_tuning,isFrontWheel); Node* node_wheel_3 = GetScene()->CreateChild("node_wheel_2"); node_wheel_3->SetPosition((model_wheel_temp3->GetBoundingBox()).Center()); node_wheel_3->SetRotation(Quaternion(0.0f, 0.0f, -90.0f)); node_wheel_temp3->SetParent(node_wheel_3); m_vpNodeWheel.Push( node_wheel_3 ); /* for ( int i = 0; i < m_vehicle->getNumWheels(); i++ ) { btWheelInfo& wheel = m_vehicle->getWheelInfo( i ); wheel.m_suspensionStiffness = suspensionStiffness; wheel.m_wheelsDampingRelaxation = suspensionDamping; wheel.m_wheelsDampingCompression = suspensionCompression; wheel.m_frictionSlip = wheelFriction; wheel.m_rollInfluence = rollInfluence; } */ if ( m_vehicle ) { m_vehicle->resetSuspension(); for ( int i = 0; i < m_vehicle->getNumWheels(); i++ ) { //synchronize the wheels with the (interpolated) chassis worldtransform m_vehicle->updateWheelTransform(i,true); btTransform transform = m_vehicle->getWheelTransformWS( i ); /* //Vector3 v3Origin = ToVector3( transform.getOrigin() ); //Quaternion qRot = ToQuaternion( transform.getRotation() ); // create wheel node Node *wheelNode = GetScene()->CreateChild(); //wheelNode->SetPosition( v3Origin ); //btWheelInfo whInfo = m_vehicle->getWheelInfo( i ); //Vector3 v3PosLS = ToVector3( whInfo.m_chassisConnectionPointCS ); //wheelNode->SetRotation( v3PosLS.x_ >= 0.0 ? Quaternion(0.0f, 0.0f, -90.0f) : Quaternion(0.0f, 0.0f, 90.0f) ); //wheelNode->SetScale(Vector3(1.0f, 0.65f, 1.0f)); StaticModel *pWheel = wheelNode->CreateComponent<StaticModel>(); if(i == 0) pWheel->SetModel(cache->GetResource<Model>("MyProjects/SimpeCar/wheel.mdl")); else pWheel->SetModel(cache->GetResource<Model>("MyProjects/SimpeCar/wheel.mdl")); if(i == 0) { //wheelNode->SetPosition( Vector3(50,50,50) ); //wheelNode->SetRotation(Quaternion(0.0f, 0.0f, 0.0f)); }else { //wheelNode->SetPosition( v3Origin ); //wheelNode->SetRotation(Quaternion(0.0f, 0.0f, 0.0f)); } m_vpNodeWheel.Push( wheelNode ); //pWheel->SetModel(cache->GetResource<Model>("MyProjects/MiniCooper/test/wheel_004.mdl")); pWheel->SetMaterial(cache->GetResource<Material>("MyProjects/SimpeCar/TireTextureMaterial.xml")); pWheel->SetCastShadows(true); */ } } }
void CharacterDemo::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // Create scene subsystem components scene_->CreateComponent<Octree>(); scene_->CreateComponent<PhysicsWorld>(); // Create camera and define viewport. We will be doing load / save, so it's convenient to create the camera outside the scene, // so that it won't be destroyed and recreated, and we don't have to redefine the viewport on load cameraNode_ = new Node(context_); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(300.0f); GetSubsystem<Renderer>()->SetViewport(0, new Viewport(context_, scene_, camera)); // Create static scene content. First create a zone for ambient lighting and fog control Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.5f, 0.5f, 0.7f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.0f); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); // Create a directional light with cascaded shadow mapping Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.3f, -0.5f, 0.425f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.00025f, 0.5f)); light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f)); light->SetSpecularIntensity(0.5f); // Create the floor object Node* floorNode = scene_->CreateChild("Floor"); floorNode->SetPosition(Vector3(0.0f, -0.5f, 0.0f)); floorNode->SetScale(Vector3(200.0f, 1.0f, 200.0f)); StaticModel* object = floorNode->CreateComponent<StaticModel>(); object->SetModel(cache->GetResource<Model>("Models/Box.mdl")); object->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); RigidBody* body = floorNode->CreateComponent<RigidBody>(); // Use collision layer bit 2 to mark world scenery. This is what we will raycast against to prevent camera from going // inside geometry body->SetCollisionLayer(2); CollisionShape* shape = floorNode->CreateComponent<CollisionShape>(); shape->SetBox(Vector3::ONE); // Create mushrooms of varying sizes const unsigned NUM_MUSHROOMS = 60; for (unsigned i = 0; i < NUM_MUSHROOMS; ++i) { Node* objectNode = scene_->CreateChild("Mushroom"); objectNode->SetPosition(Vector3(Random(180.0f) - 90.0f, 0.0f, Random(180.0f) - 90.0f)); objectNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f)); objectNode->SetScale(2.0f + Random(5.0f)); StaticModel* object = objectNode->CreateComponent<StaticModel>(); object->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl")); object->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml")); object->SetCastShadows(true); RigidBody* body = objectNode->CreateComponent<RigidBody>(); body->SetCollisionLayer(2); CollisionShape* shape = objectNode->CreateComponent<CollisionShape>(); shape->SetTriangleMesh(object->GetModel(), 0); } // Create movable boxes. Let them fall from the sky at first const unsigned NUM_BOXES = 100; for (unsigned i = 0; i < NUM_BOXES; ++i) { float scale = Random(2.0f) + 0.5f; Node* objectNode = scene_->CreateChild("Box"); objectNode->SetPosition(Vector3(Random(180.0f) - 90.0f, Random(10.0f) + 10.0f, Random(180.0f) - 90.0f)); objectNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f))); objectNode->SetScale(scale); StaticModel* object = objectNode->CreateComponent<StaticModel>(); object->SetModel(cache->GetResource<Model>("Models/Box.mdl")); object->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); object->SetCastShadows(true); RigidBody* body = objectNode->CreateComponent<RigidBody>(); body->SetCollisionLayer(2); // Bigger boxes will be heavier and harder to move body->SetMass(scale * 2.0f); CollisionShape* shape = objectNode->CreateComponent<CollisionShape>(); shape->SetBox(Vector3::ONE); } }