-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathOrbitActor.cpp
More file actions
64 lines (58 loc) · 1.67 KB
/
OrbitActor.cpp
File metadata and controls
64 lines (58 loc) · 1.67 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
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
#include "OrbitActor.h"
#include "MeshComponent.h"
#include "Game.h"
#include "Renderer.h"
#include "OrbitCamera.h"
#include "MoveComponent.h"
OrbitActor::OrbitActor(Game* game)
:Actor(game)
{
mMeshComp = new MeshComponent(this);
mMeshComp->SetMesh(game->GetRenderer()->GetMesh("Assets/RacingCar.gpmesh"));
SetPosition(Vector3(0.0f, 0.0f, -100.0f));
mCameraComp = new OrbitCamera(this);
}
void OrbitActor::ActorInput(const uint8_t* keys)
{
// Mouse rotation
// Get relative movement from SDL
int x, y;
Uint32 buttons = SDL_GetRelativeMouseState(&x, &y);
// Only apply rotation if right-click is held
if (buttons & SDL_BUTTON(SDL_BUTTON_RIGHT))
{
// Assume mouse movement is usually between -500 and +500
const int maxMouseSpeed = 500;
// Rotation/sec at maximum speed
const float maxOrbitSpeed = Math::Pi * 8;
float yawSpeed = 0.0f;
if (x != 0)
{
// Convert to ~[-1.0, 1.0]
yawSpeed = static_cast<float>(x) / maxMouseSpeed;
// Multiply by rotation/sec
yawSpeed *= maxOrbitSpeed;
}
mCameraComp->SetYawSpeed(-yawSpeed);
// Compute pitch
float pitchSpeed = 0.0f;
if (y != 0)
{
// Convert to ~[-1.0, 1.0]
pitchSpeed = static_cast<float>(y) / maxMouseSpeed;
pitchSpeed *= maxOrbitSpeed;
}
mCameraComp->SetPitchSpeed(pitchSpeed);
}
}
void OrbitActor::SetVisible(bool visible)
{
mMeshComp->SetVisible(visible);
}