2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "game-camera.h"
20 #include <dali/public-api/common/stage.h>
21 #include <dali/public-api/render-tasks/render-task-list.h>
22 #include <dali/public-api/render-tasks/render-task.h>
23 #include <dali/public-api/events/touch-data.h>
24 #include <dali/public-api/events/touch-event.h>
30 // Input sensitivity, the larger value, the more sensitive input
31 // Default value has been chosen empirically
32 const float CAMERA_SENSITIVITY ( 90.0f );
34 // Vertical angle limit of the camera
35 const float CAMERA_VERTICAL_LIMIT ( 80.0f );
37 // Position where camera is instantiated by default
38 const Vector3 CAMERA_DEFAULT_POSITION ( 1.0f, -1.5f, -3.0f );
40 // Field-of-View in degrees
41 const float CAMERA_DEFAULT_FOV ( 60.0f );
44 const float CAMERA_DEFAULT_NEAR ( 0.1f );
47 const float CAMERA_DEFAULT_FAR ( 100.0f );
49 // Default forward vector
50 const Vector3 CAMERA_FORWARD ( 0.0f, 0.0f, 1.0f );
53 const Vector3 CAMERA_UP ( Vector3::YAXIS );
56 GameCamera::GameCamera()
57 : mFovY( CAMERA_DEFAULT_FOV ),
58 mNear( CAMERA_DEFAULT_NEAR ),
59 mFar( CAMERA_DEFAULT_FAR ),
60 mWalkingTouchId( -1 ),
61 mLookingTouchId( -1 ),
62 mPortraitMode( false )
66 GameCamera::~GameCamera()
69 mCameraActor.Remove( mInterceptorActor );
72 void GameCamera::Initialise( float fovY, float near, float far )
78 Vector2 stageSize = Stage::GetCurrent().GetSize();
79 mPortraitMode = stageSize.x < stageSize.y ? true : false;
81 // Initialise default camera
82 InitialiseDefaultCamera();
84 // Create input interceptor actor
85 CreateInterceptorActor();
88 mTimer = Timer::New( 16 );
89 mTimer.TickSignal().Connect( this, &GameCamera::OnTick );
93 bool GameCamera::OnTick()
95 Vector2 stageSize = Stage::GetCurrent().GetSize();
97 // ---------------------------------------------------------------------
99 Vector2 tmp( mScreenLookDelta );
100 mScreenLookDelta = Vector2::ZERO;
104 float yaw = ( (tmp.y / stageSize.y ) * CAMERA_SENSITIVITY );
105 float pitch = ( (tmp.x / stageSize.x ) * CAMERA_SENSITIVITY );
106 mCameraYawPitch.y -= yaw;
107 mCameraYawPitch.x -= pitch;
108 if( abs( mCameraYawPitch.y ) > CAMERA_VERTICAL_LIMIT )
110 mCameraYawPitch.y = CAMERA_VERTICAL_LIMIT * ((mCameraYawPitch.y < 0) ? -1.0f : 1.0f );
115 float yaw = ( (tmp.y / stageSize.x ) * CAMERA_SENSITIVITY );
116 float pitch = ( (tmp.x / stageSize.y ) * CAMERA_SENSITIVITY );
117 mCameraYawPitch.x -= yaw;
118 mCameraYawPitch.y -= pitch;
119 if( abs( mCameraYawPitch.x ) > CAMERA_VERTICAL_LIMIT )
121 mCameraYawPitch.x = CAMERA_VERTICAL_LIMIT * ((mCameraYawPitch.x < 0) ? -1.0f : 1.0f );
126 Quaternion rotX( Degree( mCameraYawPitch.x), Vector3( 1.0f, 0.0f, 0.0f ) );
127 Quaternion rotY( Degree( mCameraYawPitch.y), Vector3( 0.0f, 1.0f, 0.0f ) );
130 Quaternion rotZ( Degree( mPortraitMode ? 90.0 : 0.0f), Vector3( 0.0f, 0.0f, 1.0f ) );
131 rotation = ( rotZ * rotX * rotY );
135 rotation = ( rotY * rotX );
137 mCameraActor.SetOrientation( rotation );
139 // ---------------------------------------------------------------------
141 Vector3 position( mCameraPosition );
143 // Rotate CAMERA_FORWARD vector
144 Vector3 forwardVector = rotation.Rotate( CAMERA_FORWARD );
146 // Cancel vertical movement
147 forwardVector.y = 0.0f;
150 forwardVector.Normalize();
152 // compute sideways vector
153 Vector3 sidewaysVector = forwardVector.Cross( CAMERA_UP );
155 sidewaysVector.Normalize();
157 const float forwardSpeed( mScreenWalkDelta.y / stageSize.y );
158 const float sidewaysSpeed( mScreenWalkDelta.x / stageSize.x );
160 // Adjust walking speed
163 position += forwardVector * (forwardSpeed * 0.5f);
167 position += forwardVector * (-forwardSpeed * 0.5f);
170 position += sidewaysVector * (sidewaysSpeed * 0.5f);
172 mCameraActor.SetPosition( position );
174 mCameraPosition = position;
179 void GameCamera::InitialiseDefaultCamera()
181 Stage stage = Stage::GetCurrent();
182 mCameraActor = stage.GetRenderTaskList().GetTask(0).GetCameraActor();
183 mCameraActor.SetName( "GameCamera" );
184 mCameraActor.SetAnchorPoint( AnchorPoint::CENTER );
185 mCameraActor.SetParentOrigin( ParentOrigin::CENTER );
186 mCameraActor.SetFieldOfView( Radian( Degree( mFovY ) ) );
188 // should be read from file
189 mCameraActor.SetNearClippingPlane( mNear );
190 mCameraActor.SetFarClippingPlane( mFar );
191 mCameraActor.SetPosition( CAMERA_DEFAULT_POSITION );
193 // Camera position is shadowed in order to avoid using GetCurrentPosition()
194 mCameraPosition = CAMERA_DEFAULT_POSITION;
197 void GameCamera::CreateInterceptorActor()
199 Stage stage = Stage::GetCurrent();
201 mInterceptorActor = Actor::New();
202 mInterceptorActor.SetName( "GameInputInterceptor" );
203 mInterceptorActor.SetSize( Vector3( stage.GetSize().x, stage.GetSize().y, 1 ) );
204 mInterceptorActor.SetPosition( Vector3( 0.0, 0.0, 1.0 ) );
205 mInterceptorActor.SetAnchorPoint( AnchorPoint::CENTER );
206 mInterceptorActor.SetParentOrigin( ParentOrigin::CENTER );
207 mCameraActor.Add( mInterceptorActor );
209 // Connect TouchSignal to interceptor actor
210 mInterceptorActor.TouchSignal().Connect( this, &GameCamera::OnTouch );
213 bool GameCamera::OnTouch( Actor actor, const TouchData& touch )
215 Stage stage = Stage::GetCurrent();
217 for( int i = 0; i < (int)touch.GetPointCount() && i < 3; ++i )
219 int id = touch.GetDeviceId( i );
220 Vector2 tmp( touch.GetScreenPosition( i ) );
222 float halfWindowSize;
227 halfWindowSize = stage.GetSize().y / 2;
233 halfWindowSize = stage.GetSize().x / 2;
237 if( touch.GetState( i ) == PointState::STARTED )
240 if( position.x > halfWindowSize && mLookingTouchId < 0 )
242 mLookingTouchId = id;
243 mOldTouchLookPosition = position;
246 else if( position.x < halfWindowSize && mWalkingTouchId < 0 )
248 mWalkingTouchId = id;
249 mOldTouchWalkPosition = position;
250 mScreenWalkDelta = Vector2::ZERO;
253 else if( touch.GetState( i ) == PointState::FINISHED ||
254 touch.GetState( i ) == PointState::LEAVE ||
255 touch.GetState( i ) == PointState::INTERRUPTED
259 if( mLookingTouchId == id )
261 mScreenLookDelta = Vector2::ZERO;
262 mOldTouchLookPosition = Vector2::ZERO;
263 mLookingTouchId = -1;
266 else if( mWalkingTouchId == id )
268 mScreenWalkDelta = Vector2::ZERO;
269 mOldTouchWalkPosition = Vector2::ZERO;
270 mWalkingTouchId = -1;
276 if( mLookingTouchId == id )
278 mScreenLookDelta.x += ( position.x - mOldTouchLookPosition.x );
279 mScreenLookDelta.y += ( position.y - mOldTouchLookPosition.y );
280 mOldTouchLookPosition = position;
283 else if ( mWalkingTouchId == id )
285 mScreenWalkDelta.x += ( position.x - mOldTouchWalkPosition.x );
286 mScreenWalkDelta.y += ( position.y - mOldTouchWalkPosition.y );
287 mOldTouchWalkPosition = position;