@@ -46,11 +46,8 @@ TrackballCamera::TrackballCamera(Camera *targetCamera, Entity *trackballShape) :
4646
4747 coreInput = CoreServices::getInstance ()->getCore ()->getInput ();
4848
49- targetCamera->setPosition (cameraDistance, cameraDistance, cameraDistance);
50- trackballRotateEnd = getMouseProjectionOnBall (Vector2 (trackballShape->getWidth ()/2.0 , trackballShape->getHeight ()/2.0 ));
51- trackballRotateStart = trackballRotateEnd;
52-
53- updateCamera ();
49+ targetCamera->lookAt (orbitingCenter);
50+ updateCamera ();
5451}
5552
5653Number TrackballCamera::getCameraDistance () {
@@ -98,7 +95,10 @@ void TrackballCamera::handleEvent(Event *event) {
9895 } else {
9996 if (!rotationDisabled) {
10097 mouseMode = MOUSE_MODE_ORBITING;
101- trackballRotateStart = trackballRotateEnd = getMouseProjectionOnBall (inputEvent->getMousePosition ());
98+ trackBallMouseStart = trackBallMouseEnd = Vector2 (
99+ inputEvent->getMousePosition ().x / trackballShape->getWidth (),
100+ inputEvent->getMousePosition ().y / trackballShape->getHeight ()
101+ );
102102 }
103103 }
104104 }
@@ -125,15 +125,18 @@ void TrackballCamera::handleEvent(Event *event) {
125125
126126void TrackballCamera::setOrbitingCenter (const Vector3 &newCenter) {
127127 orbitingCenter = newCenter;
128- updateCamera ();
129128}
130129
131130void TrackballCamera::processMouseMovement (const Vector2 &newPosition) {
132131 switch (mouseMode) {
133132 case MOUSE_MODE_ORBITING:
134133 {
135- trackballRotateEnd = getMouseProjectionOnBall (newPosition);
134+ trackBallMouseEnd = Vector2 (
135+ newPosition.x / trackballShape->getWidth (),
136+ newPosition.y / trackballShape->getHeight ()
137+ );
136138 updateCamera ();
139+ trackBallMouseStart = trackBallMouseEnd;
137140 }
138141 break ;
139142 case MOUSE_MODE_PANNING:
@@ -183,6 +186,7 @@ void TrackballCamera::processMouseMovement(const Vector2 &newPosition) {
183186
184187void TrackballCamera::setCameraPosition (Vector3 cameraPosition) {
185188 targetCamera->setPosition (cameraPosition);
189+ targetCamera->lookAt (orbitingCenter);
186190 updateCamera ();
187191}
188192
@@ -192,60 +196,37 @@ Vector3 TrackballCamera::getOribitingCenter() {
192196
193197void TrackballCamera::updateCamera () {
194198
199+ Quaternion currentCamQuat = targetCamera->getRotationQuat ();
195200 trackballEye = targetCamera->getPosition () - orbitingCenter;
196201 trackballEye.setLength (cameraDistance);
197-
198-
199- Number angle = acos (trackballRotateStart.dot (trackballRotateEnd) / trackballRotateStart.length () / trackballRotateEnd.length ());
200-
201- if (angle == angle) {
202- Vector3 axis = trackballRotateStart.crossProduct (trackballRotateEnd);
203- axis.Normalize ();
202+
203+ if (mouseMode == MOUSE_MODE_ORBITING) {
204+ Vector2 localMouse = trackBallMouseEnd - trackBallMouseStart;
205+
204206 Quaternion q;
205-
206- angle *= trackballRotateSpeed;
207- q.fromAngleAxis (angle, axis);
208-
207+ // yaw
208+ if (Vector3 (0.0 , 1.0 , 0.0 ).dot (currentCamQuat.applyTo (Vector3 (0.0 , 0.0 , -1.0 ))) > 0.0 )
209+ q.fromAngleAxis (localMouse.x *2.0 , Vector3 (0.0 , 1.0 , 0.0 ));
210+ else
211+ q.fromAngleAxis (localMouse.x *-2.0 , Vector3 (0.0 , 1.0 , 0.0 ));
212+ currentCamQuat = q * currentCamQuat;
213+ trackballEye = q.applyTo (trackballEye);
214+
215+ // local pitch
216+ q.fromAngleAxis (localMouse.y *-2.0 , currentCamQuat.applyTo (Vector3 (1.0 , 0.0 , 0.0 )));
217+ currentCamQuat = q * currentCamQuat;
209218 trackballEye = q.applyTo (trackballEye);
210- trackballRotateEnd = q.applyTo (trackballRotateEnd);
211-
212- trackballRotateStart = trackballRotateEnd;
213219 }
214220
215221 targetCamera->setPosition (orbitingCenter + trackballEye);
216- targetCamera->lookAt (orbitingCenter );
222+ targetCamera->setRotationByQuaternion (currentCamQuat );
217223 dispatchEvent (new Event (), Event::CHANGE_EVENT);
218224}
219225
220226void TrackballCamera::disableRotation (bool val) {
221227 rotationDisabled = val;
222228}
223229
224- Vector3 TrackballCamera::getMouseProjectionOnBall (const Vector2 &mousePosition) {
225-
226- Vector3 mouseOnBall = Vector3 ((mousePosition.x - (trackballShape->getWidth () * 0.5 )) / (trackballShape->getWidth () * 0.5 ),(mousePosition.y - (trackballShape->getHeight () * 0.5 )) / (trackballShape->getHeight () * 0.5 ), 0.0 );
227- mouseOnBall.x *= -1 ;
228-
229- Number length = mouseOnBall.length ();
230-
231-
232- if (length < sqrt (0.5 )) {
233- mouseOnBall.z = sqrt (1.0 - length*length);
234- } else {
235- mouseOnBall.z = 0.5 / length;
236- }
237-
238- trackballEye = targetCamera->getPosition () - orbitingCenter;
239-
240- Vector3 projection = Vector3 (0.0 , 1.0 , 0.0 ).setLength (mouseOnBall.y );
241-
242- projection = projection + (Vector3 (0.0 , 1.0 , 0.0 ).crossProduct (trackballEye).setLength (mouseOnBall.x ));
243-
244- trackballEye.setLength (mouseOnBall.z );
245- projection = projection + (trackballEye);
246-
247- return projection;
248- }
249230/*
250231void TrackballCamera::setCameraPosition(Vector3 cameraPosition) {
251232
0 commit comments