| 1 |
{%MainUnit x3dnodes.pas} |
| 2 |
{ |
| 3 |
Copyright 2007-2023 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
{ List of pointing device sensors. Only nodes descending from |
| 19 |
X3DPointingDeviceSensorNode, and additionally an Anchor node. } |
| 20 |
TPointingDeviceSensorList = class(TX3DNodeList) |
| 21 |
public |
| 22 |
{ Transformation (and inverse) of all the sensors on this list. |
| 23 |
X3D specification guarantees that all sensors within a single state |
| 24 |
have the same transform. } |
| 25 |
Transformation: TTransformation; |
| 26 |
|
| 27 |
function EnabledCount: Integer; |
| 28 |
function Enabled(Index: Integer): boolean; |
| 29 |
|
| 30 |
procedure Assign(Source: TPointingDeviceSensorList); |
| 31 |
end; |
| 32 |
|
| 33 |
{ Base type for all pointing device sensors. } |
| 34 |
TAbstractPointingDeviceSensorNode = class(TAbstractSensorNode) |
| 35 |
protected |
| 36 |
procedure GroupBeforeTraverse(const State: TX3DGraphTraverseState; var WasPointingDeviceSensor: Boolean); override; |
| 37 |
public |
| 38 |
{ Activate pointing device sensor. Used by the events engine |
| 39 |
(like TCastleSceneCore) to notify this sensor. |
| 40 |
|
| 41 |
OverPoint indicates 3D point (in global, that is TCastleSceneCore, coordinates) |
| 42 |
pointed by the mouse. |
| 43 |
|
| 44 |
In TAbstractPointingDeviceSensorNode class, |
| 45 |
this simply sends isActive := TRUE event. } |
| 46 |
procedure Activate(const Time: TX3DTime; |
| 47 |
const ATransform, AInvertedTransform: TMatrix4; |
| 48 |
const OverPoint: TVector3); virtual; |
| 49 |
|
| 50 |
{ Deactivate pointing device sensor. Used by the events engine |
| 51 |
(like TCastleSceneCore) to notify this sensor. |
| 52 |
|
| 53 |
In TAbstractPointingDeviceSensorNode class, |
| 54 |
this simply sends isActive := FALSE event. } |
| 55 |
procedure Deactivate(const Time: TX3DTime); virtual; |
| 56 |
|
| 57 |
{$I auto_generated_node_helpers/x3dnodes_x3dpointingdevicesensornode.inc} |
| 58 |
end; |
| 59 |
|
| 60 |
{ Base type for all drag-style pointing device sensors. } |
| 61 |
TAbstractDragSensorNode = class(TAbstractPointingDeviceSensorNode) |
| 62 |
public |
| 63 |
constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override; |
| 64 |
|
| 65 |
{ Called by events engine (like TCastleSceneCore) when you move your mouse |
| 66 |
over the @italic(active) drag sensor. } |
| 67 |
procedure Drag(const Time: TX3DTime; const RayOrigin, RayDirection: TVector3); virtual; |
| 68 |
|
| 69 |
{$I auto_generated_node_helpers/x3dnodes_x3ddragsensornode.inc} |
| 70 |
end; |
| 71 |
|
| 72 |
{ Base type for all touch-style pointing device sensors. } |
| 73 |
TAbstractTouchSensorNode = class(TAbstractPointingDeviceSensorNode) |
| 74 |
strict private |
| 75 |
FOnClick: TNotifyEvent; |
| 76 |
procedure EventTouchTimeReceive(const Event: TX3DEvent; const AValue: TX3DField; |
| 77 |
const Time: TX3DTime); |
| 78 |
public |
| 79 |
constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override; |
| 80 |
|
| 81 |
{ Event generated when we register a "click" on the shapes within |
| 82 |
the same group as this TouchSensor node. |
| 83 |
The "click" definition corresponds to the X3D touchTime event generation. |
| 84 |
|
| 85 |
Note that, in order to register clicks, TCastleScene must |
| 86 |
be configured to process events (@link(TCastleSceneCore.ProcessEvents)) |
| 87 |
and be able to detect clicks on particular shapes |
| 88 |
(@link(TCastleSceneCore.PreciseCollisions)). |
| 89 |
Usually you will initialize scene like this: |
| 90 |
|
| 91 |
@longCode(# |
| 92 |
MyScene := TCastleScene.Create(...); |
| 93 |
MyScene.Load(...); // load X3D graph with TouchSensor node |
| 94 |
MyScene.PreciseCollisions := true; |
| 95 |
MyScene.ProcessEvents := true; |
| 96 |
#) |
| 97 |
} |
| 98 |
property OnClick: TNotifyEvent read FOnClick write FOnClick; |
| 99 |
|
| 100 |
{$I auto_generated_node_helpers/x3dnodes_x3dtouchsensornode.inc} |
| 101 |
end; |
| 102 |
|
| 103 |
{ Pointing device sensor to rotate objects around a constrained axis. } |
| 104 |
TCylinderSensorNode = class(TAbstractDragSensorNode) |
| 105 |
strict private |
| 106 |
{ Are we in caps mode or cylinder mode (determined at activation) } |
| 107 |
Caps: boolean; |
| 108 |
{ If Caps, then this is a plane parallel to Y=0 and coincident with |
| 109 |
original intersection, in global coordinates } |
| 110 |
CapsPlane: TVector4; |
| 111 |
|
| 112 |
{ If not Caps, then these describe (infinitely tall) cylinder |
| 113 |
used for dragging, in global coordinates } |
| 114 |
CylinderAxisOrigin, CylinderAxis: TVector3; |
| 115 |
CylinderRadius: Single; |
| 116 |
|
| 117 |
{ First intersection (at activation), in local coordinates, |
| 118 |
additionally with Y component set to 0. } |
| 119 |
OriginalIntersection: TVector3; |
| 120 |
|
| 121 |
{ Transformation from global coords to the local sensor coords. } |
| 122 |
InvertedTransform: TMatrix4; |
| 123 |
|
| 124 |
{ Was a rotation_changed send during this activation of sensor. } |
| 125 |
WasRotation: boolean; |
| 126 |
{ Last value of rotation_changed send, meaningful only if WasRotation. } |
| 127 |
RotationAngle: Single; |
| 128 |
public |
| 129 |
procedure Activate(const Time: TX3DTime; |
| 130 |
const ATransform, AInvertedTransform: TMatrix4; |
| 131 |
const OverPoint: TVector3); override; |
| 132 |
procedure Deactivate(const Time: TX3DTime); override; |
| 133 |
procedure Drag(const Time: TX3DTime; const RayOrigin, RayDirection: TVector3); override; |
| 134 |
|
| 135 |
{$I auto_generated_node_helpers/x3dnodes_cylindersensor.inc} |
| 136 |
end; |
| 137 |
|
| 138 |
{ Pointing device sensor to move objects on a plane. } |
| 139 |
TPlaneSensorNode = class(TAbstractDragSensorNode) |
| 140 |
strict private |
| 141 |
{ Plane* stuff is in global (VRML scene) coordinates. } |
| 142 |
{ Plane of the activated sensor. } |
| 143 |
Plane: TVector4; |
| 144 |
{ Vectors indicating X and Y axis on the plane. |
| 145 |
Always normalized (do not really have to be strictly orthogonal, |
| 146 |
in case plane was transformed by some shearing; this is Ok). } |
| 147 |
PlaneX, PlaneY: TVector3; |
| 148 |
{ Initial intersection point at activation. Always lies on Plane. } |
| 149 |
PlaneOrigin: TVector3; |
| 150 |
|
| 151 |
{ Transformation from global coords to the local sensor coords. } |
| 152 |
InvertedTransform: TMatrix4; |
| 153 |
|
| 154 |
{ Was a translation_changed send during this activation of sensor. } |
| 155 |
WasTranslation: boolean; |
| 156 |
{ Last value of translation_changed send, meaningful only if WasTranslation. } |
| 157 |
Translation: TVector3; |
| 158 |
public |
| 159 |
procedure Activate(const Time: TX3DTime; |
| 160 |
const ATransform, AInvertedTransform: TMatrix4; |
| 161 |
const OverPoint: TVector3); override; |
| 162 |
procedure Deactivate(const Time: TX3DTime); override; |
| 163 |
procedure Drag(const Time: TX3DTime; const RayOrigin, RayDirection: TVector3); override; |
| 164 |
|
| 165 |
{$I auto_generated_node_helpers/x3dnodes_planesensor.inc} |
| 166 |
end; |
| 167 |
|
| 168 |
{ Pointing device sensor to rotate objects. } |
| 169 |
TSphereSensorNode = class(TAbstractDragSensorNode) |
| 170 |
strict private |
| 171 |
{ Sphere center and radius, in world coordinates. } |
| 172 |
SphereCenter: TVector3; |
| 173 |
SphereRadius: Single; |
| 174 |
{ First intersection (at activation), in local coordinates. } |
| 175 |
OriginalIntersection: TVector3; |
| 176 |
|
| 177 |
{ Transformation from global coords to the local sensor coords. } |
| 178 |
InvertedTransform: TMatrix4; |
| 179 |
|
| 180 |
{ Was a rotation_changed send during this activation of sensor. } |
| 181 |
WasRotation: boolean; |
| 182 |
{ Last value of rotation_changed send, meaningful only if WasRotation. } |
| 183 |
Rotation: TVector4; |
| 184 |
public |
| 185 |
procedure Activate(const Time: TX3DTime; |
| 186 |
const ATransform, AInvertedTransform: TMatrix4; |
| 187 |
const OverPoint: TVector3); override; |
| 188 |
procedure Deactivate(const Time: TX3DTime); override; |
| 189 |
procedure Drag(const Time: TX3DTime; const RayOrigin, RayDirection: TVector3); override; |
| 190 |
|
| 191 |
{$I auto_generated_node_helpers/x3dnodes_spheresensor.inc} |
| 192 |
end; |
| 193 |
|
| 194 |
{ Sensor to catch clicks on 3D objects. } |
| 195 |
TTouchSensorNode = class(TAbstractTouchSensorNode) |
| 196 |
{$I auto_generated_node_helpers/x3dnodes_touchsensor.inc} |
| 197 |
end; |
| 198 |
|
| 199 |
{$endif read_interface} |
| 200 |
|
| 201 |
{$ifdef read_implementation} |
| 202 |
|
| 203 |
{ TPointingDeviceSensorList ------------------------------------------------- } |
| 204 |
|
| 205 |
function TPointingDeviceSensorList.EnabledCount: Integer; |
| 206 |
var |
| 207 |
I: Integer; |
| 208 |
begin |
| 209 |
Result := 0; |
| 210 |
for I := 0 to Count - 1 do |
| 211 |
if Enabled(I) then |
| 212 |
Inc(Result); |
| 213 |
end; |
| 214 |
|
| 215 |
function TPointingDeviceSensorList.Enabled(Index: Integer): boolean; |
| 216 |
begin |
| 217 |
Result := (not (Items[Index] is TAbstractPointingDeviceSensorNode)) or |
| 218 |
TAbstractPointingDeviceSensorNode(Items[Index]).FdEnabled.Value; |
| 219 |
end; |
| 220 |
|
| 221 |
procedure TPointingDeviceSensorList.Assign(Source: TPointingDeviceSensorList); |
| 222 |
begin |
| 223 |
inherited Assign(Source); |
| 224 |
Transformation := Source.Transformation; |
| 225 |
end; |
| 226 |
|
| 227 |
{ TAbstractPointingDeviceSensorNode -------------------------------------------------------------- } |
| 228 |
|
| 229 |
procedure TAbstractPointingDeviceSensorNode.GroupBeforeTraverse( |
| 230 |
const State: TX3DGraphTraverseState; var WasPointingDeviceSensor: Boolean); |
| 231 |
begin |
| 232 |
inherited; |
| 233 |
GroupBeforeTraverse_AddPointingDeviceSensor(Self, State, WasPointingDeviceSensor); |
| 234 |
end; |
| 235 |
|
| 236 |
procedure TAbstractPointingDeviceSensorNode.Activate(const Time: TX3DTime; |
| 237 |
const ATransform, AInvertedTransform: TMatrix4; |
| 238 |
const OverPoint: TVector3); |
| 239 |
begin |
| 240 |
EventIsActive.Send(true, Time); |
| 241 |
end; |
| 242 |
|
| 243 |
procedure TAbstractPointingDeviceSensorNode.Deactivate(const Time: TX3DTime); |
| 244 |
begin |
| 245 |
EventIsActive.Send(false, Time); |
| 246 |
end; |
| 247 |
|
| 248 |
constructor TAbstractDragSensorNode.Create(const AX3DName, ABaseUrl: String); |
| 249 |
begin |
| 250 |
inherited; |
| 251 |
FdEnabled.ChangeAlways := chDragSensorEnabled; |
| 252 |
end; |
| 253 |
|
| 254 |
procedure TAbstractDragSensorNode.Drag(const Time: TX3DTime; const RayOrigin, RayDirection: TVector3); |
| 255 |
begin |
| 256 |
{ Nothing to do in this class } |
| 257 |
end; |
| 258 |
|
| 259 |
constructor TAbstractTouchSensorNode.Create(const AX3DName, ABaseUrl: String); |
| 260 |
begin |
| 261 |
inherited; |
| 262 |
EventTouchTime.AddNotification({$ifdef FPC}@{$endif} EventTouchTimeReceive); |
| 263 |
end; |
| 264 |
|
| 265 |
procedure TAbstractTouchSensorNode.EventTouchTimeReceive( |
| 266 |
const Event: TX3DEvent; const AValue: TX3DField; const Time: TX3DTime); |
| 267 |
begin |
| 268 |
if Assigned(FOnClick) then |
| 269 |
FOnClick(Self); |
| 270 |
end; |
| 271 |
|
| 272 |
procedure TCylinderSensorNode.Activate(const Time: TX3DTime; |
| 273 |
const ATransform, AInvertedTransform: TMatrix4; |
| 274 |
const OverPoint: TVector3); |
| 275 |
var |
| 276 |
Transform, M, IM: TMatrix4; |
| 277 |
CapsPlaneDir: PVector3; |
| 278 |
begin |
| 279 |
inherited; |
| 280 |
|
| 281 |
Transform := ATransform; |
| 282 |
InvertedTransform := AInvertedTransform; |
| 283 |
|
| 284 |
{ Do not apply rotation when it's 0. A small optimization for a common case. } |
| 285 |
if FdAxisRotation.RotationRad <> 0 then |
| 286 |
begin |
| 287 |
RotationMatricesRad(FdAxisRotation.RotationRad, FdAxisRotation.Axis, M, IM); |
| 288 |
Transform := Transform * M; |
| 289 |
InvertedTransform := IM * InvertedTransform; |
| 290 |
end; |
| 291 |
|
| 292 |
OriginalIntersection := InvertedTransform.MultPoint(OverPoint); |
| 293 |
Caps := AngleRadBetweenVectors(OriginalIntersection, |
| 294 |
Vector3(0, 1, 0)) < FdDiskAngle.Value; |
| 295 |
|
| 296 |
OriginalIntersection.Y := 0; |
| 297 |
|
| 298 |
if Caps then |
| 299 |
begin |
| 300 |
{ CapsPlane is parallel to Y=0 plane |
| 301 |
(transformed by current sensor transform and by axisRotation), |
| 302 |
and passing though PlaneOrigin. } |
| 303 |
CapsPlaneDir := PVector3(@CapsPlane); |
| 304 |
CapsPlaneDir^ := Transform.MultDirection(TVector3.One[1]); |
| 305 |
CapsPlane.W := -TVector3.DotProduct(CapsPlaneDir^, OverPoint); |
| 306 |
end else |
| 307 |
begin |
| 308 |
CylinderAxisOrigin := Transform.MultPoint(TVector3.Zero); |
| 309 |
CylinderAxis := Transform.MultDirection(TVector3.One[1]); |
| 310 |
CylinderRadius := Sqrt(PointToLineDistanceSqr( |
| 311 |
CylinderAxisOrigin, CylinderAxis, OverPoint)); |
| 312 |
end; |
| 313 |
|
| 314 |
WasRotation := false; |
| 315 |
end; |
| 316 |
|
| 317 |
procedure TCylinderSensorNode.Deactivate(const Time: TX3DTime); |
| 318 |
begin |
| 319 |
if FdAutoOffset.Value and WasRotation then |
| 320 |
FdOffset.Send(RotationAngle); |
| 321 |
|
| 322 |
inherited; |
| 323 |
end; |
| 324 |
|
| 325 |
procedure TCylinderSensorNode.Drag(const Time: TX3DTime; const RayOrigin, RayDirection: TVector3); |
| 326 |
var |
| 327 |
Intersection, LocalIntersection: TVector3; |
| 328 |
WasIntersection: boolean; |
| 329 |
begin |
| 330 |
inherited; |
| 331 |
|
| 332 |
if Caps then |
| 333 |
WasIntersection := TryPlaneRayIntersection(Intersection, |
| 334 |
CapsPlane, RayOrigin, RayDirection) else |
| 335 |
WasIntersection := TryCylinderRayIntersection(Intersection, |
| 336 |
CylinderAxisOrigin, CylinderAxis, CylinderRadius, RayOrigin, RayDirection); |
| 337 |
|
| 338 |
if WasIntersection then |
| 339 |
begin |
| 340 |
try |
| 341 |
LocalIntersection := InvertedTransform.MultPoint(Intersection); |
| 342 |
except |
| 343 |
on ETransformedResultInvalid do |
| 344 |
begin |
| 345 |
WritelnLog('Drag sensor', 'Sensor transformation matrix^-1 cannot transform points.'); |
| 346 |
Exit; |
| 347 |
end; |
| 348 |
end; |
| 349 |
EventTrackPoint_changed.Send(LocalIntersection, Time); |
| 350 |
|
| 351 |
{ both OriginalIntersection and LocalIntersection have Y component set to 0 |
| 352 |
for the sake of calculating RotationAngle. They are both assumed to lie |
| 353 |
nicely on a CapsPlane. } |
| 354 |
LocalIntersection.Y := 0; |
| 355 |
|
| 356 |
RotationAngle := RotationAngleRadBetweenVectors( |
| 357 |
OriginalIntersection, LocalIntersection, TVector3.One[1]); |
| 358 |
|
| 359 |
{ add offset, and clamp by min/maxAngle } |
| 360 |
RotationAngle := RotationAngle + FdOffset.Value; |
| 361 |
if FdMinAngle.Value <= FdMaxAngle.Value then |
| 362 |
ClampVar(RotationAngle, FdMinAngle.Value, FdMaxAngle.Value); |
| 363 |
|
| 364 |
EventRotation_changed.Send(Vector4(0, 1, 0, RotationAngle), Time); |
| 365 |
WasRotation := true; |
| 366 |
end; |
| 367 |
end; |
| 368 |
|
| 369 |
procedure TPlaneSensorNode.Activate(const Time: TX3DTime; |
| 370 |
const ATransform, AInvertedTransform: TMatrix4; |
| 371 |
const OverPoint: TVector3); |
| 372 |
var |
| 373 |
PlaneDir: PVector3; |
| 374 |
Transform, M, IM: TMatrix4; |
| 375 |
begin |
| 376 |
inherited; |
| 377 |
|
| 378 |
PlaneOrigin := OverPoint; |
| 379 |
|
| 380 |
Transform := ATransform; |
| 381 |
InvertedTransform := AInvertedTransform; |
| 382 |
|
| 383 |
{ Do not apply rotation when it's 0. A small optimization for a common case. } |
| 384 |
if FdAxisRotation.RotationRad <> 0 then |
| 385 |
begin |
| 386 |
RotationMatricesRad(FdAxisRotation.RotationRad, FdAxisRotation.Axis, M, IM); |
| 387 |
Transform := Transform * M; |
| 388 |
InvertedTransform := IM * InvertedTransform; |
| 389 |
end; |
| 390 |
|
| 391 |
try |
| 392 |
{ Plane is parallel to Z=0 plane |
| 393 |
(transformed by current sensor transform and by axisRotation), |
| 394 |
and passing though PlaneOrigin. } |
| 395 |
PlaneDir := PVector3(@Plane); |
| 396 |
PlaneDir^ := Transform.MultDirection(TVector3.One[2]); |
| 397 |
Plane.W := -TVector3.DotProduct(PlaneDir^, PlaneOrigin); |
| 398 |
|
| 399 |
{ +X, +Y vectors, transformed by current sensor transform and |
| 400 |
by axisRotation, normalized. } |
| 401 |
PlaneX := Transform.MultDirection(TVector3.One[0]).Normalize; |
| 402 |
PlaneY := Transform.MultDirection(TVector3.One[1]).Normalize; |
| 403 |
except |
| 404 |
on ETransformedResultInvalid do |
| 405 |
begin |
| 406 |
{ Transform matrix doesn't manage to transform directions. |
| 407 |
So just assume it's identity, nothing more sensible to do. } |
| 408 |
Plane := Vector4(0, 0, 1, -PlaneOrigin.Z); |
| 409 |
PlaneX := TVector3.One[0]; |
| 410 |
PlaneY := TVector3.One[1]; |
| 411 |
Transform := TMatrix4.Identity; |
| 412 |
InvertedTransform := TMatrix4.Identity; |
| 413 |
WritelnLog('Drag sensor', 'Sensor transformation matrix cannot transform directions.'); |
| 414 |
end; |
| 415 |
end; |
| 416 |
|
| 417 |
WasTranslation := false; |
| 418 |
end; |
| 419 |
|
| 420 |
procedure TPlaneSensorNode.Deactivate(const Time: TX3DTime); |
| 421 |
begin |
| 422 |
if FdAutoOffset.Value and WasTranslation then |
| 423 |
{ Hm, we don't pass Time below, instead Scene.NextEventTime will be used. |
| 424 |
But that's even better, since we guarantee no loops in Deactivate() |
| 425 |
calls? } |
| 426 |
FdOffset.Send(Translation); |
| 427 |
|
| 428 |
inherited; |
| 429 |
end; |
| 430 |
|
| 431 |
procedure TPlaneSensorNode.Drag(const Time: TX3DTime; const RayOrigin, RayDirection: TVector3); |
| 432 |
var |
| 433 |
Intersection, TranslationGlobal: TVector3; |
| 434 |
begin |
| 435 |
inherited; |
| 436 |
|
| 437 |
{ If no intersection, it's Ok to do nothing? } |
| 438 |
|
| 439 |
if TryPlaneRayIntersection(Intersection, Plane, RayOrigin, RayDirection) then |
| 440 |
begin |
| 441 |
try |
| 442 |
{ trackPoint_changed should be in the local sensor coordinates |
| 443 |
(with axisRotation), without worrying about offset |
| 444 |
(like for translation_changed). |
| 445 |
|
| 446 |
This is sensible: |
| 447 |
- track point should visualize the underlying geometry of the drag sensor |
| 448 |
(plane, in this case) |
| 449 |
- this makes track point sensible also for Sphere/CylinderSensor |
| 450 |
(where offset is for rotations, and cannot be applied to |
| 451 |
trackPoint_changed). |
| 452 |
|
| 453 |
About using local coordinates: |
| 454 |
FreeWRL and OpenVRML seems to also do this in sources, |
| 455 |
and Octaga and InstantReality behavior suggests they do it too. |
| 456 |
|
| 457 |
About using local coordinates with axisRotation: |
| 458 |
That's more difficult, see |
| 459 |
https://castle-engine.io/x3d_implementation_pointingdevicesensor.php } |
| 460 |
EventTrackPoint_changed.Send( |
| 461 |
InvertedTransform.MultPoint(Intersection), Time); |
| 462 |
except |
| 463 |
on ETransformedResultInvalid do |
| 464 |
begin |
| 465 |
EventTrackPoint_changed.Send(Intersection, Time); |
| 466 |
WritelnLog('Drag sensor', 'Sensor transformation matrix^-1 cannot transform points.'); |
| 467 |
end; |
| 468 |
end; |
| 469 |
|
| 470 |
{ An alternative implementation would transform Intersection to |
| 471 |
local coords (with axisRotation, and with origin at PointOrigin). |
| 472 |
|
| 473 |
Then applying the PointOrigin, PlaneX, PlaneY would be much simpler: |
| 474 |
PointOrigin is just zero, and PlaneX = just (1, 0, 0). |
| 475 |
So instead of TVector3.DotProduct(V, PlaneX) just take V.X. } |
| 476 |
|
| 477 |
TranslationGlobal := Intersection - PlaneOrigin; |
| 478 |
|
| 479 |
{ map TranslationGlobal to the plane local coord system |
| 480 |
(with axisRotation) } |
| 481 |
Translation.X := TVector3.DotProduct(TranslationGlobal, PlaneX); |
| 482 |
Translation.Y := TVector3.DotProduct(TranslationGlobal, PlaneY); |
| 483 |
Translation.Z := 0; |
| 484 |
|
| 485 |
Translation := Translation + FdOffset.Value; |
| 486 |
|
| 487 |
if FdMinPosition.Value.X <= FdMaxPosition.Value.X then |
| 488 |
ClampVar(Translation.X, FdMinPosition.Value.X, FdMaxPosition.Value.X); |
| 489 |
if FdMinPosition.Value.Y <= FdMaxPosition.Value.Y then |
| 490 |
ClampVar(Translation.Y, FdMinPosition.Value.Y, FdMaxPosition.Value.Y); |
| 491 |
|
| 492 |
EventTranslation_changed.Send(Translation, Time); |
| 493 |
WasTranslation := true; |
| 494 |
end; |
| 495 |
end; |
| 496 |
|
| 497 |
procedure TSphereSensorNode.Activate(const Time: TX3DTime; |
| 498 |
const ATransform, AInvertedTransform: TMatrix4; |
| 499 |
const OverPoint: TVector3); |
| 500 |
begin |
| 501 |
inherited; |
| 502 |
|
| 503 |
InvertedTransform := AInvertedTransform; |
| 504 |
|
| 505 |
OriginalIntersection := AInvertedTransform.MultPoint(OverPoint); |
| 506 |
|
| 507 |
SphereCenter := ATransform.MultPoint(TVector3.Zero); |
| 508 |
SphereRadius := PointsDistance(OverPoint, SphereCenter); |
| 509 |
|
| 510 |
WasRotation := false; |
| 511 |
end; |
| 512 |
|
| 513 |
procedure TSphereSensorNode.Deactivate(const Time: TX3DTime); |
| 514 |
begin |
| 515 |
if FdAutoOffset.Value and WasRotation then |
| 516 |
FdOffset.Send(Rotation); |
| 517 |
|
| 518 |
inherited; |
| 519 |
end; |
| 520 |
|
| 521 |
procedure TSphereSensorNode.Drag(const Time: TX3DTime; const RayOrigin, RayDirection: TVector3); |
| 522 |
var |
| 523 |
Intersection, LocalIntersection, RotationAxis: TVector3; |
| 524 |
RotationAngle: Single; |
| 525 |
RotationQ: TQuaternion; |
| 526 |
begin |
| 527 |
inherited; |
| 528 |
|
| 529 |
{ If no intersection, it's Ok to do nothing? } |
| 530 |
|
| 531 |
if TrySphereRayIntersection(Intersection, SphereCenter, SphereRadius, |
| 532 |
RayOrigin, RayDirection) then |
| 533 |
begin |
| 534 |
try |
| 535 |
LocalIntersection := InvertedTransform.MultPoint(Intersection); |
| 536 |
except |
| 537 |
on ETransformedResultInvalid do |
| 538 |
begin |
| 539 |
WritelnLog('Drag sensor', 'Sensor transformation matrix^-1 cannot transform points.'); |
| 540 |
Exit; |
| 541 |
end; |
| 542 |
end; |
| 543 |
EventTrackPoint_changed.Send(LocalIntersection, Time); |
| 544 |
|
| 545 |
{ Rotation always contains offset } |
| 546 |
RotationQ := QuatFromAxisAngle(FdOffset.Axis, FdOffset.RotationRad); |
| 547 |
|
| 548 |
{ Add to RotationQ rotation from OriginalIntersection to current. } |
| 549 |
RotationAxis := TVector3.CrossProduct(OriginalIntersection, LocalIntersection); |
| 550 |
if not RotationAxis.IsZero then |
| 551 |
begin |
| 552 |
RotationAngle := RotationAngleRadBetweenVectors( |
| 553 |
OriginalIntersection, LocalIntersection); |
| 554 |
RotationQ := QuatFromAxisAngle( |
| 555 |
RotationAxis.Normalize, RotationAngle) * RotationQ; |
| 556 |
end; |
| 557 |
|
| 558 |
Rotation := RotationQ.ToAxisAngle; |
| 559 |
|
| 560 |
EventRotation_changed.Send(Rotation, Time); |
| 561 |
WasRotation := true; |
| 562 |
end; |
| 563 |
end; |
| 564 |
|
| 565 |
procedure RegisterPointingDeviceSensorNodes; |
| 566 |
begin |
| 567 |
NodesManager.RegisterNodeClasses([ |
| 568 |
TCylinderSensorNode, |
| 569 |
TPlaneSensorNode, |
| 570 |
TSphereSensorNode, |
| 571 |
TTouchSensorNode |
| 572 |
]); |
| 573 |
end; |
| 574 |
|
| 575 |
{$endif read_implementation} |