game-camera.cpp
7.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "game-camera.h"
#include <dali/public-api/events/touch-event.h>
#include <dali/public-api/render-tasks/render-task-list.h>
#include <dali/public-api/render-tasks/render-task.h>
using namespace Dali;
namespace
{
// Input sensitivity, the larger value, the more sensitive input
// Default value has been chosen empirically
const float CAMERA_SENSITIVITY(90.0f);
// Vertical angle limit of the camera
const float CAMERA_VERTICAL_LIMIT(80.0f);
// Position where camera is instantiated by default
const Vector3 CAMERA_DEFAULT_POSITION(1.0f, -1.5f, -3.0f);
// Field-of-View in degrees
const float CAMERA_DEFAULT_FOV(60.0f);
// Near plane
const float CAMERA_DEFAULT_NEAR(0.1f);
// Far plane
const float CAMERA_DEFAULT_FAR(100.0f);
// Default forward vector
const Vector3 CAMERA_FORWARD(0.0f, 0.0f, 1.0f);
// Default up vector
const Vector3 CAMERA_UP(Vector3::YAXIS);
} // namespace
GameCamera::GameCamera()
: mFovY(CAMERA_DEFAULT_FOV),
mNear(CAMERA_DEFAULT_NEAR),
mFar(CAMERA_DEFAULT_FAR),
mWalkingTouchId(-1),
mLookingTouchId(-1),
mPortraitMode(false)
{
}
GameCamera::~GameCamera()
{
mTimer.Stop();
mCameraActor.Remove(mInterceptorActor);
}
void GameCamera::Initialise(CameraActor defaultCamera, float fovY, float near, float far, const Vector2& sceneSize)
{
mCameraActor = defaultCamera;
mFovY = fovY;
mNear = near;
mFar = far;
mSceneSize = sceneSize;
mPortraitMode = mSceneSize.x < mSceneSize.y ? true : false;
// Initialise default camera
InitialiseDefaultCamera();
// Create input interceptor actor
CreateInterceptorActor();
// Start timer
mTimer = Timer::New(16);
mTimer.TickSignal().Connect(this, &GameCamera::OnTick);
mTimer.Start();
}
bool GameCamera::OnTick()
{
// ---------------------------------------------------------------------
// update rotation
Vector2 tmp(mScreenLookDelta);
mScreenLookDelta = Vector2::ZERO;
if(mPortraitMode)
{
float yaw = ((tmp.y / mSceneSize.y) * CAMERA_SENSITIVITY);
float pitch = ((tmp.x / mSceneSize.x) * CAMERA_SENSITIVITY);
mCameraYawPitch.y -= yaw;
mCameraYawPitch.x -= pitch;
if(abs(mCameraYawPitch.y) > CAMERA_VERTICAL_LIMIT)
{
mCameraYawPitch.y = CAMERA_VERTICAL_LIMIT * ((mCameraYawPitch.y < 0) ? -1.0f : 1.0f);
}
}
else
{
float yaw = ((tmp.y / mSceneSize.x) * CAMERA_SENSITIVITY);
float pitch = ((tmp.x / mSceneSize.y) * CAMERA_SENSITIVITY);
mCameraYawPitch.x -= yaw;
mCameraYawPitch.y -= pitch;
if(abs(mCameraYawPitch.x) > CAMERA_VERTICAL_LIMIT)
{
mCameraYawPitch.x = CAMERA_VERTICAL_LIMIT * ((mCameraYawPitch.x < 0) ? -1.0f : 1.0f);
}
}
Quaternion rotation;
Quaternion rotX(Degree(mCameraYawPitch.x), Vector3(1.0f, 0.0f, 0.0f));
Quaternion rotY(Degree(mCameraYawPitch.y), Vector3(0.0f, 1.0f, 0.0f));
if(mPortraitMode)
{
Quaternion rotZ(Degree(mPortraitMode ? 90.0 : 0.0f), Vector3(0.0f, 0.0f, 1.0f));
rotation = (rotZ * rotX * rotY);
}
else
{
rotation = (rotY * rotX);
}
mCameraActor.SetProperty(Actor::Property::ORIENTATION, rotation);
// ---------------------------------------------------------------------
// update position
Vector3 position(mCameraPosition);
// Rotate CAMERA_FORWARD vector
Vector3 forwardVector = rotation.Rotate(CAMERA_FORWARD);
// Cancel vertical movement
forwardVector.y = 0.0f;
// Normalize
forwardVector.Normalize();
// compute sideways vector
Vector3 sidewaysVector = forwardVector.Cross(CAMERA_UP);
sidewaysVector.Normalize();
const float forwardSpeed(mScreenWalkDelta.y / mSceneSize.y);
const float sidewaysSpeed(mScreenWalkDelta.x / mSceneSize.x);
// Adjust walking speed
if(mPortraitMode)
{
position += forwardVector * (forwardSpeed * 0.5f);
}
else
{
position += forwardVector * (-forwardSpeed * 0.5f);
}
position += sidewaysVector * (sidewaysSpeed * 0.5f);
mCameraActor.SetProperty(Actor::Property::POSITION, position);
mCameraPosition = position;
return true;
}
void GameCamera::InitialiseDefaultCamera()
{
mCameraActor.SetProperty(Dali::Actor::Property::NAME, "GameCamera");
mCameraActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
mCameraActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
mCameraActor.SetFieldOfView(Radian(Degree(mFovY)));
// should be read from file
mCameraActor.SetNearClippingPlane(mNear);
mCameraActor.SetFarClippingPlane(mFar);
mCameraActor.SetProperty(Actor::Property::POSITION, CAMERA_DEFAULT_POSITION);
// Camera position is shadowed in order to avoid using.GetCurrentProperty< Vector3 >( Actor::Property::POSITION )
mCameraPosition = CAMERA_DEFAULT_POSITION;
}
void GameCamera::CreateInterceptorActor()
{
mInterceptorActor = Actor::New();
mInterceptorActor.SetProperty(Dali::Actor::Property::NAME, "GameInputInterceptor");
mInterceptorActor.SetProperty(Actor::Property::SIZE, Vector3(mSceneSize.x, mSceneSize.y, 1));
mInterceptorActor.SetProperty(Actor::Property::POSITION, Vector3(0.0, 0.0, 1.0));
mInterceptorActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
mInterceptorActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
mCameraActor.Add(mInterceptorActor);
// Connect TouchedSignal to interceptor actor
mInterceptorActor.TouchedSignal().Connect(this, &GameCamera::OnTouch);
}
bool GameCamera::OnTouch(Actor actor, const TouchEvent& touch)
{
for(int i = 0; i < (int)touch.GetPointCount() && i < 3; ++i)
{
int id = touch.GetDeviceId(i);
Vector2 tmp(touch.GetScreenPosition(i));
Vector2 position;
float halfWindowSize;
if(mPortraitMode)
{
position.x = tmp.y;
position.y = tmp.x;
halfWindowSize = mSceneSize.y / 2;
}
else
{
position.x = tmp.x;
position.y = tmp.y;
halfWindowSize = mSceneSize.x / 2;
}
// touch started
if(touch.GetState(i) == PointState::STARTED)
{
// start looking
if(position.x > halfWindowSize && mLookingTouchId < 0)
{
mLookingTouchId = id;
mOldTouchLookPosition = position;
}
// start walking
else if(position.x < halfWindowSize && mWalkingTouchId < 0)
{
mWalkingTouchId = id;
mOldTouchWalkPosition = position;
mScreenWalkDelta = Vector2::ZERO;
}
}
else if(touch.GetState(i) == PointState::FINISHED ||
touch.GetState(i) == PointState::LEAVE ||
touch.GetState(i) == PointState::INTERRUPTED)
{
// terminate look
if(mLookingTouchId == id)
{
mScreenLookDelta = Vector2::ZERO;
mOldTouchLookPosition = Vector2::ZERO;
mLookingTouchId = -1;
}
// terminate walking
else if(mWalkingTouchId == id)
{
mScreenWalkDelta = Vector2::ZERO;
mOldTouchWalkPosition = Vector2::ZERO;
mWalkingTouchId = -1;
}
}
else // on motion
{
// update looking
if(mLookingTouchId == id)
{
mScreenLookDelta.x += (position.x - mOldTouchLookPosition.x);
mScreenLookDelta.y += (position.y - mOldTouchLookPosition.y);
mOldTouchLookPosition = position;
}
// update walking
else if(mWalkingTouchId == id)
{
mScreenWalkDelta.x += (position.x - mOldTouchWalkPosition.x);
mScreenWalkDelta.y += (position.y - mOldTouchWalkPosition.y);
mOldTouchWalkPosition = position;
}
}
}
return true;
}