-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathorbiting target.cpp
executable file
·151 lines (120 loc) · 4.26 KB
/
orbiting target.cpp
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
#include <irrlicht.h>
#pragma comment(lib, "Irrlicht.lib")
using namespace irr;
class MyEventReceiver : public IEventReceiver
{
public:
MyEventReceiver()
{
u32 k;
for (k = 0; k < sizeof(Keys) / sizeof(*Keys); ++k)
Keys[k] = false;
}
virtual ~MyEventReceiver()
{
}
// you may need to change the parameter type depending on your Irrlicht version
virtual bool OnEvent(const SEvent& event)
{
if (event.EventType == EET_KEY_INPUT_EVENT)
{
Keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
return true;
}
return false;
}
public:
bool Keys[KEY_KEY_CODES_COUNT];
};
int main()
{
// create device and exit if creation failed
IrrlichtDevice *device = createDevice(EDT_DIRECT3D9, core::dimension2d<s32>(800, 600), 32, false, false, false);
if (device == 0)
return 1; // could not create selected driver.
MyEventReceiver receiver;
device->setEventReceiver(&receiver);
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
// the thing we want to look at
scene::ISceneNode* box =
smgr->addCubeSceneNode();
// add camera
scene::ICameraSceneNode* cam =
smgr->addCameraSceneNode(box);
cam->setTarget( box->getAbsolutePosition() );
// add a dynamic light
smgr->addLightSceneNode(0, core::vector3df(-20, 0, 0), video::SColorf(1.f, 0.f, 0.f, 1.f));
smgr->addLightSceneNode(0, core::vector3df(0, -20, 0), video::SColorf(0.f, 1.f, 0.f, 1.f));
smgr->addLightSceneNode(0, core::vector3df(0, 0, -20), video::SColorf(0.f, 0.f, 1.f, 1.f));
f32 Radius = 20.f;
f32 Theta = 180.f; // degrees
f32 Phi = 90.f; // degrees
f32 LinearVelocity = 20.f;
f32 AngularVelocity = 40.f;
u32 oldNow = device->getTimer()->getRealTime();
while(device->run())
{
// calculate elapsed time as fractional seconds
u32 now = device->getTimer()->getRealTime();
f32 Elapsed = (now - oldNow) / 1000.f;
oldNow = now;
// window is active, so render scene
if (device->isWindowActive())
{
if (driver->beginScene(true, true, video::SColor(255, 100, 100, 140)))
{
smgr->drawAll();
driver->endScene();
}
// adjust camera position based on current key input
if (receiver.Keys[KEY_NUMPAD1])
Radius -= (LinearVelocity * Elapsed);
if (receiver.Keys[KEY_NUMPAD3])
Radius += (LinearVelocity * Elapsed);
if (receiver.Keys[KEY_NUMPAD4])
Theta += (AngularVelocity * Elapsed);
if (receiver.Keys[KEY_NUMPAD6])
Theta -= (AngularVelocity * Elapsed);
if (receiver.Keys[KEY_NUMPAD8])
Phi += (AngularVelocity * Elapsed);
if (receiver.Keys[KEY_NUMPAD2])
Phi -= (AngularVelocity * Elapsed);
if (receiver.Keys[KEY_NUMPAD5])
{
Theta = 180.f;
Phi = 90.f;
}
// prevent camera from walking into our box
if (Radius < 10.f)
Radius = 10.f;
// lame ass gimble lock prevention. if you don't want to do this
// you need to adjust the up vector of the camera so it never is
// parallel to the look at vector
if (Phi < 10.f)
Phi = 10.f;
else if (170.f < Phi)
Phi = 170.f;
f32 sinOfPhi = sinf(Phi * core::DEGTORAD);
f32 cosOfPhi = cosf(Phi * core::DEGTORAD);
f32 sinOfTheta = sinf(Theta * core::DEGTORAD);
f32 cosOfTheta = cosf(Theta * core::DEGTORAD);
core::vector3df offset;
// these are switched around a little from what i posted above
offset.X = Radius * sinOfTheta * sinOfPhi;
offset.Y = Radius * cosOfPhi;
offset.Z = Radius * cosOfTheta * sinOfPhi;
// camera is a child of the cube, so our offset is actually
// the position of the camera
cam->setPosition(offset);
cam->setTarget( box->getAbsolutePosition() );
cam->updateAbsolutePosition();
wchar_t caption[128];
swprintf(caption, 128, L"r=%f theta=%f phi=%f",
Radius, Theta, Phi);
device->setWindowCaption(caption);
}
}
device->drop();
return 0;
}