Esempio n. 1
0
    /// btMotionState override. Called when Bullet wants to tell us the body's current transform
    void setWorldTransform(const btTransform &worldTrans)
    {
        /// \todo For a large scene, applying the changed transforms of rigid bodies is slow (slower than the physics simulation itself,
        /// or handling collisions) due to the large number of signals being fired.
    
        // Cannot modify server-authoritative physics object, rather get the transform changes through placeable attributes
        const bool hasAuthority = rigidBody->HasAuthority();
        if (!hasAuthority && !clientExtrapolating)
            return;
    
        if (placeable.Expired())
            return;
        Placeable* p = placeable;
        // Important: disconnect our own response to attribute changes to not create an endless loop!
        disconnected = true;
    
        AttributeChange::Type changeType = hasAuthority ? AttributeChange::Default : AttributeChange::LocalOnly;

        // Set transform
        float3 position = worldTrans.getOrigin();
        Quat orientation = worldTrans.getRotation();
    
        // Non-parented case
        if (p->parentRef.Get().IsEmpty())
        {
            Transform newTrans = p->transform.Get();
            newTrans.SetPos(position.x, position.y, position.z);
            newTrans.SetOrientation(orientation);
            p->transform.Set(newTrans, changeType);
        }
        else
        // The placeable has a parent itself
        {
            Urho3D::Node* parent = p->UrhoSceneNode()->GetParent();
            if (parent)
            {
                position = parent->WorldToLocal(position);
                orientation = parent->GetWorldRotation().Inverse() * orientation;
                Transform newTrans = p->transform.Get();
                newTrans.SetPos(position);
                newTrans.SetOrientation(orientation);
                p->transform.Set(newTrans, changeType);
            }
        }
        // Set linear & angular velocity
        if (body)
        {
            // Performance optimization: because applying each attribute causes signals to be fired, which is slow in a large scene
            // (and furthermore, on a server, causes each connection's sync state to be accessed), do not set the linear/angular
            // velocities if they haven't changed
            float3 linearVel = body->getLinearVelocity();
            float3 angularVel = RadToDeg(body->getAngularVelocity());
            if (!linearVel.Equals(rigidBody->linearVelocity.Get()))
                rigidBody->linearVelocity.Set(linearVel, changeType);
            if (!angularVel.Equals(rigidBody->angularVelocity.Get()))
                rigidBody->angularVelocity.Set(angularVel, changeType);
        }
    
        disconnected = false;
    }
Esempio n. 2
0
void MainGameState::Start() {
    std::cout << "starting main game " << std::endl;
    Urho3D::ResourceCache *cache = GetSubsystem<Urho3D::ResourceCache>();
    // We will be needing to load resources.
    // All the resources used in this example comes with Urho3D.
    // If the engine can't find them, check the ResourcePrefixPath.

    // Seems like the mouse must be in cursor mode before creating the UI or it won't work.

    // Let's use the default style that comes with Urho3D.
    // GetSubsystem<Urho3D::UI>()->GetRoot()->AddChild(texts["FPS_text"]);
    // Let's create some text to display.
    // Add a button, just as an interactive UI sample.
    // Note, must be part of the UI system before SetSize calls!

    GetSubsystem<Urho3D::UI>()->GetRoot()->AddChild(ui_elements["side_bar"]);

    // Now we can change the mouse mode.

    // Let's setup a scene to render.
    scene_ = new Urho3D::Scene(context_);
    // Let the scene have an Octree component!
    scene_->CreateComponent<Urho3D::Octree>();
    // Let's add an additional scene component for fun.
    scene_->CreateComponent<Urho3D::DebugRenderer>();

    // Let's put some sky in there.
    // Again, if the engine can't find these resources you need to check
    // the "ResourcePrefixPath". These files come with Urho3D.
    Urho3D::Node *skyNode = scene_->CreateChild("Sky");
    skyNode->SetScale(500.0f); // The scale actually does not matter
    Urho3D::Skybox *skybox = skyNode->CreateComponent<Urho3D::Skybox>();
    skybox->SetModel(cache->GetResource<Urho3D::Model>("Models/Box.mdl"));
    skybox->SetMaterial(cache->GetResource<Urho3D::Material>("Materials/Skybox.xml"));
    int worldsize = 40;
    Urho3D::SharedPtr<Urho3D::Node> bed_rock(new Urho3D::Node(context_));
    for (int x = -worldsize; x < worldsize; x++) {
        for (int y = -worldsize; y < worldsize; y++) {
            Urho3D::Vector3 pos = Urho3D::Vector3(x, y, 0);
            EntityNode *boxNode_ = new EntityNode(context_, &m_entity_manager);
            bed_rock->AddChild(boxNode_);
            boxNode_->SetPosition(pos);
            Urho3D::StaticModel *boxObject = boxNode_->CreateComponent<Urho3D::StaticModel>();
            boxObject->SetModel(cache->GetResource<Urho3D::Model>("Models/Box.mdl"));

            boxObject->SetMaterial(cache->GetResource<Urho3D::Material>("Materials/Mushroom.xml"));
            boxObject->SetCastShadows(true);
            boxNode_->assign<terrain_type>("Stone", STONE);
        }
    }
    z_levels.push_back(bed_rock);
    for (int z = 1; z <= m_world_height; z++) {

        Urho3D::SharedPtr<Urho3D::Node> cur_level(new Urho3D::Node(context_));
        for (int x = -worldsize; x < worldsize; x++)
            for (int y = -worldsize; y < worldsize; y++) {
                /*  if (sqrt(x * x + y * y) / 30 <
                      20 +
                          4 * sin(5 * sin(6 * (glm::dot(glm::normalize(glm::vec2(x, y)),
                                                        glm::vec2(0, 1))))))           */

                double noise = m_noise_gen.noise_3D(0.02 * x, 0.002 * y, 0.02 * z) +
                               m_noise_gen.noise_3D(x, 4 * y, z);

                std::cout << 2 - ((z) / (float)m_world_height) << " "
                          << m_noise_gen.noise_2D(x, y) + 1 << std::endl;
                if ((z / (float)m_world_height) <
                    0.1 * m_noise_gen.noise_2D(0.1 * x, y * 0.1) + 1) {
                    if (noise > 0.2) {

                        Urho3D::Vector3 pos = Urho3D::Vector3(x, y, z);
                        auto lol = new TerrainFactory(context_, &m_entity_manager,cache );
                    
                        cur_level->AddChild(lol->create_stone_terrain(pos));
                         
                    } else if (true) {
                        Urho3D::Vector3 pos = Urho3D::Vector3(x, y, z);
                        EntityNode *boxNode_ = new EntityNode(context_, &m_entity_manager);
                        cur_level->AddChild(boxNode_);
                        boxNode_->SetPosition(pos);
                        Urho3D::StaticModel *boxObject =
                            boxNode_->CreateComponent<Urho3D::StaticModel>();
                        boxObject->SetModel(cache->GetResource<Urho3D::Model>("Models/Box.mdl"));

                        boxObject->SetMaterial(cache->GetResource<Urho3D::Material>(
                            "Materials/NinjaSnowWar/Snow.xml"));
                        boxObject->SetCastShadows(true);
                        boxNode_->assign<terrain_type>("Stone", STONE);
                    }
                }
            }
        z_levels.push_back(cur_level);
        if (z > m_world_height - m_world_render_depth)
            scene_->AddChild(cur_level);
    }
    std::cout << z_levels.size() << std::endl;
    // We need a camera from which the viewport can render.
    cameraNode_ = scene_->CreateChild("Camera");
    m_camera = cameraNode_->CreateComponent<Urho3D::Camera>();
    m_camera->SetFarClip(2000);
    cameraNode_->Pitch(45);
    cameraNode_->Yaw(180);
    cameraNode_->SetPosition(Urho3D::Vector3(0, 0, m_world_height + m_world_render_depth));

    // Create two lights
    {
        Urho3D::Node *lightNode = scene_->CreateChild("Light");
        lightNode->SetPosition(Urho3D::Vector3(-5, 0, m_world_height + 20));
        Urho3D::Light *light = lightNode->CreateComponent<Urho3D::Light>();
        light->SetLightType(Urho3D::LIGHT_POINT);
        light->SetRange(50);
        light->SetBrightness(1.2);
        light->SetColor(Urho3D::Color(1, .5, .8, 1));
        light->SetCastShadows(true);
    }
    // add one to the camera node as well
    // Now we setup the viewport. Ofcourse, you can have more than one!
    Urho3D::Renderer *renderer = GetSubsystem<Urho3D::Renderer>();
    Urho3D::SharedPtr<Urho3D::Viewport> viewport(
        new Urho3D::Viewport(context_, scene_, cameraNode_->GetComponent<Urho3D::Camera>()));
    renderer->SetViewport(0, viewport);

    // We subscribe to the events we'd like to handle.
    // In this example we will be showing what most of them do,
    // but in reality you would only subscribe to the events
    // you really need to handle.
    // These are sort of subscribed in the order in which the engine
    // would send the events. Read each handler method's comment for
    // details.
    std::cout << "start main done" << std::endl;
}
Esempio n. 3
0
void HelloWorld::Start()
{
	Urho3D::Graphics* graphics = GetSubsystem<Urho3D::Graphics>();

	helloScene_ = new Urho3D::Scene(context_);
	helloScene_->CreateComponent<Urho3D::Octree>();
	
	auto objectNode = helloScene_->CreateChild();
	Urho3D::Node* lightNode = helloScene_->CreateChild();
	Urho3D::Node* cameraNode = helloScene_->CreateChild();
	//Urho3D::StaticModel* object = objectNode->CreateComponent<Urho3D::StaticModel>();
	//object->SetModel(GetSubsystem<Urho3D::ResourceCache>()->GetResource<Urho3D::Model>("Models/Mushroom.mdl"));
	//object->SetMaterial(GetSubsystem<Urho3D::ResourceCache>()->GetResource<Urho3D::Material>("Materials/Mushroom.xml"));
	Urho3D::Light* light = lightNode->CreateComponent<Urho3D::Light>();
	light->SetLightType(Urho3D::LIGHT_DIRECTIONAL);
	lightNode->SetDirection(Urho3D::Vector3(-1.0f, -1.0f, -1.0f));
	Urho3D::Camera* camera = cameraNode->CreateComponent<Urho3D::Camera>();
	camera->SetFarClip(10000.f);
	cameraNode->SetPosition(Urho3D::Vector3(0.0f, 0.f, 0.0f));
	cameraNode->SetRotation(Urho3D::Quaternion(5.f, Urho3D::Vector3(0.f, 1.f, 0.f)));
	Urho3D::Viewport* vp = new Urho3D::Viewport(context_, helloScene_, camera);
	GetSubsystem<Urho3D::Renderer>()->SetViewport(0, vp);
	//CreateText();
	SubscribeToEvents();

	//Urho3D::SharedPtr<Urho3D::RenderPath> effectRenderPath = vp->GetRenderPath()->Clone();
	//effectRenderPath->Append(GetSubsystem<Urho3D::ResourceCache>()->GetResource<Urho3D::XMLFile>("PostProcess/ColorCorrection.xml"));
	//effectRenderPath->Append(GetSubsystem<Urho3D::ResourceCache>()->GetResource<Urho3D::XMLFile>("PostProcess/FXAA3.xml"));
	//// Make the bloom mixing parameter more pronounced
	//effectRenderPath->SetShaderParameter("BloomMix", Urho3D::Vector2(0.9f, 0.6f));
	//effectRenderPath->SetEnabled("Bloom", false);
	//effectRenderPath->SetEnabled("FXAA3", false);
	//vp->SetRenderPath(effectRenderPath);

	Urho3D::Node* node = helloScene_->CreateChild();
	//node->SetPosition(Urho3D::Vector3(0.0f, 0.0f, 0.0f));
	//node->SetRotation(Urho3D::Quaternion(0.0f, 0.f, 0.0f));
	//node->CreateComponent<Urho3D::Rocket::RocketDocument2D>();

	// Left
	
	node = helloScene_->CreateChild();
	node->SetPosition(Urho3D::Vector3(0.0f, 0.0f, 250.0f));
	// This will orient the node to "billboard" the document
	node->SetWorldRotation(camera->GetFaceCameraRotation(node->GetPosition(), node->GetRotation(), Urho3D::FC_ROTATE_XYZ));
	//node->SetRotation(Urho3D::Quaternion(30.0f, -60.f, 0.0f));
	node->CreateComponent<Urho3D::Rocket::RocketDocument2D>();


	Urho3D::Node* zoneNode = helloScene_->CreateChild("Zone");
	Urho3D::Zone* zone = zoneNode->CreateComponent<Urho3D::Zone>();
	zone->SetBoundingBox(Urho3D::BoundingBox(-10000.0f, 10000.0f));
	zone->SetAmbientColor(Urho3D::Color(0.35f, 0.35f, 0.25f));
	zone->SetFogColor(Urho3D::Color(0.5f, 0.6f, 0.7f));
	zone->SetFogStart(100.0f);
//	zone->SetFogEnd(300.0f);
	zone->SetFogEnd(3000.0f);

	//node->LookAt(cameraNode->GetPosition());
	//Right
	//node = helloScene_->CreateChild();
	//node->SetPosition(Urho3D::Vector3(100.0f, 0.0f, 0.0f));
	//node->SetRotation(Urho3D::Quaternion(0.0f, 45.f, 0.0f));
	//node->CreateComponent<Urho3D::Rocket::RocketDocument3D>();

	//graphics->SetDefaultTextureFilterMode(Urho3D::FILTER_ANISOTROPIC);
}