Skip to content

Commit 2c1afa0

Browse files
committed
Merge pull request #584 from randomteddybear/IDEImprovements
Gizmo and camera improvements.
2 parents f26962c + 336eb51 commit 2c1afa0

File tree

6 files changed

+459
-141
lines changed

6 files changed

+459
-141
lines changed

Core/Contents/Include/PolyRay.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,18 @@ namespace Polycode {
3838
Number boxIntersect(const Vector3 &box, const Matrix4 &transformMatrix, float near = 0.0, float far = 9999.0) const;
3939

4040
Vector3 planeIntersectPoint(const Vector3 &planeNormal, Number planeDistance) const;
41+
Vector3 planeIntersectPoint(const Vector3 &planeNormal, const Vector3 &planePosition) const;
4142
Ray tranformByMatrix(const Matrix4& matrix) const;
43+
44+
/**
45+
* finds the two closest point on the ray to an arbitrary point space.
46+
*/
47+
Vector3 closestPointOnRay(const Vector3 &point) const;
48+
49+
/**
50+
* finds the two closest points between two rays, returns false if they're parallel.
51+
*/
52+
bool closestPointsBetween(const Ray &ray2, Vector3 *point1, Vector3 *point2);
4253

4354
bool polygonIntersect(const Vector3 &v1, const Vector3 &v2, const Vector3 &v3) const;
4455

Core/Contents/Source/PolyRay.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ Vector3 Ray::planeIntersectPoint(const Vector3 &planeNormal, Number planeDistanc
5555
return origin + direction * (-distanceToOrigin / direction.dot(planeNormal));
5656
}
5757

58+
Vector3 Ray::planeIntersectPoint(const Vector3 &planeNormal, const Vector3 &planePosition) const {
59+
Number d = (planePosition - origin).dot(planeNormal) / direction.dot(planeNormal);
60+
return origin + direction * d;
61+
}
62+
5863
bool Ray::polygonIntersect(const Vector3 &v1, const Vector3 &v2, const Vector3 &v3) const {
5964

6065
Number t,u,v;
@@ -94,6 +99,40 @@ bool Ray::polygonIntersect(const Vector3 &v1, const Vector3 &v2, const Vector3 &
9499
return true;
95100
}
96101

102+
Vector3 Ray::closestPointOnRay(const Vector3 &point) const {
103+
Number b = (point - origin).dot(direction)/ direction.dot(direction);
104+
return origin + direction*b;
105+
}
106+
107+
bool Ray::closestPointsBetween(const Ray &ray2, Vector3 *point1, Vector3 *point2) {
108+
Vector3 wOrigin = origin - ray2.origin;
109+
110+
Number a = direction.dot(direction);
111+
Number b = direction.dot(ray2.direction);
112+
Number c = ray2.direction.dot(ray2.direction);
113+
Number d = direction.dot(wOrigin);
114+
Number e = ray2.direction.dot(wOrigin);
115+
Number denom = a*c - b*b;
116+
117+
if(denom < 0.00001) {
118+
if(point1)
119+
*point1 = Vector3(0);
120+
if(point2)
121+
*point2 = Vector3(0);
122+
return false;
123+
}
124+
125+
Number s = (b*e - c*d)/denom;
126+
Number t = (a*e - b*d)/denom;
127+
128+
129+
if(point1)
130+
*point1 = origin + direction*s;
131+
if(point2)
132+
*point2 = ray2.origin + ray2.direction*t;
133+
return true;
134+
}
135+
97136
Number Ray::boxIntersect(const Vector3 &box, const Matrix4 &transformMatrix, float near, float far) const {
98137

99138
if(box.x == 0 || box.y == 0 || box.z == 0)

IDE/Contents/Include/TrackballCamera.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ class TrackballCamera : public EventDispatcher {
6060

6161
int mouseMode;
6262

63-
Vector3 getMouseProjectionOnBall(const Vector2 &mousePosition);
6463
void updateCamera();
6564
void processMouseMovement(const Vector2 &newPosition);
6665

@@ -72,8 +71,6 @@ class TrackballCamera : public EventDispatcher {
7271
Vector2 trackBallMouseStart;
7372
Vector2 trackBallMouseEnd;
7473
Vector3 orbitingCenter;
75-
Vector3 trackballRotateStart;
76-
Vector3 trackballRotateEnd;
7774
Vector3 trackballEye;
7875
Number cameraDistance;
7976
CoreInput *coreInput;

IDE/Contents/Include/TransformGizmo.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,26 @@ class TransformGizmo : public Entity {
9494

9595
void transformSelectedEntities(const Vector3 &move, const Vector3 &scale, Number rotate);
9696
Vector3 getTransformPlanePosition();
97-
97+
Vector3 getPositionAlongAxis();
98+
bool isConstrainedToSingleAxis();
99+
98100
Number getTransformPlaneAngle();
101+
Number get2dAngle();
102+
Vector2 getCorrectedMousePosition();
103+
104+
void resetTransform();
99105

100106
void setTransformOrientation(int orientation);
101107

102108
void updateOrientationForEntity(Entity *entity);
103109

104110
void setTransformPlane(Number x, Number y, Number z, bool forceGlobal = false);
111+
void setTransformPlane(bool useX, bool useY, bool useZ);
105112
void setTransformPlaneFromView();
106113

107114
void setCenterMode(int centerMode);
115+
116+
void toggleOrientation();
108117

109118
static const int TRANSFORM_MOVE = 0;
110119
static const int TRANSFORM_SCALE = 1;
@@ -136,11 +145,16 @@ class TransformGizmo : public Entity {
136145
int transformMode;
137146
int gizmoMode;
138147
int orientation;
148+
int startingOrientation;
139149

140150
int centerMode;
141151

142152
std::vector<Entity*> selectedEntities;
143153
std::vector<Vector3> entityPositions;
154+
155+
std::vector<Vector3> oldPosition;
156+
std::vector<Vector3> oldScale;
157+
std::vector<Quaternion> oldRotation;
144158

145159
Scene *targetScene;
146160
Camera *targetCamera;
@@ -164,6 +178,8 @@ class TransformGizmo : public Entity {
164178
Vector3 gizmoPoint;
165179
Vector3 startingPoint;
166180
Number startingAngle;
181+
182+
Vector2 mouseStart2d;
167183

168184
Number scaleAmount;
169185

IDE/Contents/Source/TrackballCamera.cpp

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5653
Number 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

126126
void TrackballCamera::setOrbitingCenter(const Vector3 &newCenter) {
127127
orbitingCenter = newCenter;
128-
updateCamera();
129128
}
130129

131130
void 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

184187
void TrackballCamera::setCameraPosition(Vector3 cameraPosition) {
185188
targetCamera->setPosition(cameraPosition);
189+
targetCamera->lookAt(orbitingCenter);
186190
updateCamera();
187191
}
188192

@@ -192,60 +196,37 @@ Vector3 TrackballCamera::getOribitingCenter() {
192196

193197
void 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

220226
void 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
/*
250231
void TrackballCamera::setCameraPosition(Vector3 cameraPosition) {
251232

0 commit comments

Comments
 (0)