Esempio n. 1
0
void Touch::UpdateTouches(Controls& controls) // Called from HandleUpdate
{
    zoom_ = false; // reset bool

    Input* input = GetSubsystem<Input>();

    // Zoom in/out
    if (input->GetNumTouches() == 2)
    {
        TouchState* touch1 = input->GetTouch(0);
        TouchState* touch2 = input->GetTouch(1);

        // Check for zoom pattern (touches moving in opposite directions and on empty space)
        if (!touch1->touchedElement_ && !touch2->touchedElement_ && ((touch1->delta_.y_ > 0 && touch2->delta_.y_ < 0) || (touch1->delta_.y_ < 0 && touch2->delta_.y_ > 0)))
            zoom_ = true;
        else
            zoom_ = false;

        if (zoom_)
        {
            int sens = 0;
            // Check for zoom direction (in/out)
            if (Abs(touch1->position_.y_ - touch2->position_.y_) > Abs(touch1->lastPosition_.y_ - touch2->lastPosition_.y_))
                sens = -1;
            else
                sens = 1;
            cameraDistance_ += Abs(touch1->delta_.y_ - touch2->delta_.y_) * sens * touchSensitivity_ / 50.0f;
            cameraDistance_ = Clamp(cameraDistance_, CAMERA_MIN_DIST, CAMERA_MAX_DIST); // Restrict zoom range to [1;20]
        }
    }

    // Gyroscope (emulated by SDL through a virtual joystick)
    if (useGyroscope_ && input->GetNumJoysticks() > 0)  // numJoysticks = 1 on iOS & Android
    {
        JoystickState* joystick = input->GetJoystickByIndex(0);
        if (joystick->GetNumAxes() >= 2)
        {
            if (joystick->GetAxisPosition(0) < -GYROSCOPE_THRESHOLD)
                controls.Set(CTRL_LEFT, true);
            if (joystick->GetAxisPosition(0) > GYROSCOPE_THRESHOLD)
                controls.Set(CTRL_RIGHT, true);
            if (joystick->GetAxisPosition(1) < -GYROSCOPE_THRESHOLD)
                controls.Set(CTRL_FORWARD, true);
            if (joystick->GetAxisPosition(1) > GYROSCOPE_THRESHOLD)
                controls.Set(CTRL_BACK, true);
        }
    }
}
Esempio n. 2
0
void SceneReplication::HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData)
{
    // This function is different on the client and server. The client collects controls (WASD controls + yaw angle)
    // and sets them to its server connection object, so that they will be sent to the server automatically at a
    // fixed rate, by default 30 FPS. The server will actually apply the controls (authoritative simulation.)
    Network* network = GetSubsystem<Network>();
    Connection* serverConnection = network->GetServerConnection();
    
    // Client: collect controls
    if (serverConnection)
    {
        UI* ui = GetSubsystem<UI>();
        Input* input = GetSubsystem<Input>();
        Controls controls;
        
        // Copy mouse yaw
        controls.yaw_ = yaw_;
        
        // Only apply WASD controls if there is no focused UI element
        if (!ui->GetFocusElement())
        {
            controls.Set(CTRL_FORWARD, input->GetKeyDown('W'));
            controls.Set(CTRL_BACK, input->GetKeyDown('S'));
            controls.Set(CTRL_LEFT, input->GetKeyDown('A'));
            controls.Set(CTRL_RIGHT, input->GetKeyDown('D'));
        }
        
        serverConnection->SetControls(controls);
        // In case the server wants to do position-based interest management using the NetworkPriority components, we should also
        // tell it our observer (camera) position. In this sample it is not in use, but eg. the NinjaSnowWar game uses it
        serverConnection->SetPosition(cameraNode_->GetPosition());
    }
    // Server: apply controls to client objects
    else if (network->IsServerRunning())
    {
        const Vector<SharedPtr<Connection> >& connections = network->GetClientConnections();
        
        for (unsigned i = 0; i < connections.Size(); ++i)
        {
            Connection* connection = connections[i];
            // Get the object this connection is controlling
            Node* ballNode = serverObjects_[connection];
            if (!ballNode)
                continue;
            
            RigidBody* body = ballNode->GetComponent<RigidBody>();
            
            // Get the last controls sent by the client
            const Controls& controls = connection->GetControls();
            // Torque is relative to the forward vector
            Quaternion rotation(0.0f, controls.yaw_, 0.0f);
            
            const float MOVE_TORQUE = 3.0f;
            
            // Movement torque is applied before each simulation step, which happen at 60 FPS. This makes the simulation
            // independent from rendering framerate. We could also apply forces (which would enable in-air control),
            // but want to emphasize that it's a ball which should only control its motion by rolling along the ground
            if (controls.buttons_ & CTRL_FORWARD)
                body->ApplyTorque(rotation * Vector3::RIGHT * MOVE_TORQUE);
            if (controls.buttons_ & CTRL_BACK)
                body->ApplyTorque(rotation * Vector3::LEFT * MOVE_TORQUE);
            if (controls.buttons_ & CTRL_LEFT)
                body->ApplyTorque(rotation * Vector3::FORWARD * MOVE_TORQUE);
            if (controls.buttons_ & CTRL_RIGHT)
                body->ApplyTorque(rotation * Vector3::BACK * MOVE_TORQUE);
        }
    }
}