문제

I am working on C++ game using android NDK and JNI system using SDL (Simple DirectMedia Layer). I am done with most of the parts but i am not able to figure how to implement tilt control (tilt control as in temple run).I have googled a lot without any relevant information. Please help me i am stuck.

Raghuvendra Kumar

도움이 되었습니까?

해결책 2

I am able to use tilt control in SDL though i still don't understand how to relate x,y,z values against the screen pixel position. But at least basic operation can be performed using x,y,z values. Thanks to aeror_ for right pointers. It's very simple. Sample test code as follow:-

#include "../SDL/src/core/android/SDL_android.h"

//Get Tilt values though SDL implementation
void InputHandler::HandleTitltEvt()
{

   float accelValues[3];
   Android_JNI_GetAccelerometerValues(accelValues);

   stTileValues_.x = accelValues[0];
   stTileValues_.y = accelValues[1];
   stTileValues_.z = accelValues[2];

   return ;

}

//Handle TiltValues to Move your player across the screen
void Player::HandleInput_()
{

    InputHandler::Instance()->HandleTitltEvt();

    Axis_t tiltValues = InputHandler::Instance()->getTiltValue();  
    if( (tiltValues.y > 0) && ((vPosition_.getY() + iHeight_) < (Game::Instance()->getGameHeight() - 15)))
    {

       vVelocity_.setY(iMoveSpeed + 5);


       if (tiltValues.x > 0)
       {
                vVelocity_.setX(iMoveSpeed+5);

       }
       else if (tiltValues.x < 0)
       {
          vVelocity_.setX(-(iMoveSpeed+5));

       }

    }
    else if ((tiltValues.y < 0) && ((vPosition_.getY() + iHeight_) > 0))
    {
           vVelocity_.setY(-(iMoveSpeed+5));


           if (tiltValues.x > 0)
       {
                vVelocity_.setX(iMoveSpeed+5);

       }
       else if (tiltValues.x < 0)
       {
          vVelocity_.setX(-(iMoveSpeed+5));

       }

    }
    else if (tiltValues.x > 0)
    {
           vVelocity_.setX(iMoveSpeed+5);

    }
    else if (tiltValues.x < 0)
    {
       vVelocity_.setX(-(iMoveSpeed+5));

    }

}

다른 팁

It is already included in the sample android project of SDL2. In SDLActivity.java the onSensorChanged method registers all tilt movements:

    public void onSensorChanged(SensorEvent event) {
      if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        SDLActivity.onNativeAccel(event.values[0] / SensorManager.GRAVITY_EARTH,
        event.values[1] / SensorManager.GRAVITY_EARTH,
        event.values[2] / SensorManager.GRAVITY_EARTH);
      }
    }

Then these values can then be read in your C++ code, by calling the function Android_JNI_GetAccelerometerValues(float values[3]) in sdl_android.c.

So for example your code could look like:

   #include "../SDL/src/core/android/SDL_android.h"
   float accelValues[3];
   Android_JNI_GetAccelerometerValues(accelValues);

after which accelValues will contain the accelerometer values you can use for controlling your game.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top