/usr/src/castle-game-engine-4.1.1/x3d/x3d_pointingdevicesensor.inc is in castle-game-engine-src 4.1.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 | {
Copyright 2007-2013 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{$ifdef read_interface}
{ List of pointing device sensors. Only nodes descending from
X3DPointingDeviceSensorNode, and additionally an Anchor node. }
TPointingDeviceSensorList = class(TX3DNodeList)
public
{ Transformation (and inverse) of all the sensors on this list.
VRML/X3D specification guarantees that all sensors within a single state
have the same transform. }
Transform, InvertedTransform: TMatrix4Single;
function EnabledCount: Integer;
function Enabled(Index: Integer): boolean;
procedure Assign(Source: TPointingDeviceSensorList);
end;
{ }
TAbstractPointingDeviceSensorNode = class(TAbstractSensorNode)
public
procedure CreateNode; override;
private FFdDescription: TSFString;
public property FdDescription: TSFString read FFdDescription;
{ Event: SFBool, out } { }
private FEventIsOver: TX3DEvent;
public property EventIsOver: TX3DEvent read FEventIsOver;
{ Activate pointing device sensor. Used by the events engine
(like TCastleSceneCore) to notify this sensor.
OverPoint indicates 3D point (in global, that is TCastleSceneCore, coordinates)
pointed by the mouse.
In TAbstractPointingDeviceSensorNode class,
this simply sends isActive := TRUE event. }
procedure Activate(const Time: TX3DTime;
const ATransform, AInvertedTransform: TMatrix4Single;
const OverPoint: TVector3Single); virtual;
{ Deactivate pointing device sensor. Used by the events engine
(like TCastleSceneCore) to notify this sensor.
In TAbstractPointingDeviceSensorNode class,
this simply sends isActive := FALSE event. }
procedure Deactivate(const Time: TX3DTime); virtual;
end;
TAbstractDragSensorNode = class(TAbstractPointingDeviceSensorNode)
public
procedure CreateNode; override;
private FFdAutoOffset: TSFBool;
public property FdAutoOffset: TSFBool read FFdAutoOffset;
{ Event: SFVec3f, out } { }
private FEventTrackPoint_changed: TX3DEvent;
public property EventTrackPoint_changed: TX3DEvent read FEventTrackPoint_changed;
{ Called by events engine (like TCastleSceneCore) when you move your mouse
over the @italic(active) drag sensor. }
procedure Drag(const Time: TX3DTime; const RayOrigin, RayDirection: TVector3Single); virtual;
end;
TAbstractTouchSensorNode = class(TAbstractPointingDeviceSensorNode)
public
procedure CreateNode; override;
{ Event: SFTime, out } { }
private FEventTouchTime: TX3DEvent;
public property EventTouchTime: TX3DEvent read FEventTouchTime;
end;
TCylinderSensorNode = class(TAbstractDragSensorNode)
private
{ Are we in caps mode or cylinder mode (determined at activation) }
Caps: boolean;
{ If Caps, then this is a plane parallel to Y=0 and coincident with
original intersection, in global coordinates }
CapsPlane: TVector4Single;
{ If not Caps, then these describe (infinitely tall) cylinder
used for dragging, in global coordinates }
CylinderAxisOrigin, CylinderAxis: TVector3Single;
CylinderRadius: Single;
{ First intersection (at activation), in local coordinates,
additionally with Y component set to 0. }
OriginalIntersection: TVector3Single;
{ Transformation from global coords to the local sensor coords. }
InvertedTransform: TMatrix4Single;
{ Was a rotation_changed send during this activation of sensor. }
WasRotation: boolean;
{ Last value of rotation_changed send, meaningful only if WasRotation. }
RotationAngle: Single;
public
procedure CreateNode; override;
class function ClassNodeTypeName: string; override;
class function URNMatching(const URN: string): boolean; override;
private FFdAxisRotation: TSFRotation;
public property FdAxisRotation: TSFRotation read FFdAxisRotation;
private FFdDiskAngle: TSFFloat;
public property FdDiskAngle: TSFFloat read FFdDiskAngle;
private FFdMaxAngle: TSFFloat;
public property FdMaxAngle: TSFFloat read FFdMaxAngle;
private FFdMinAngle: TSFFloat;
public property FdMinAngle: TSFFloat read FFdMinAngle;
private FFdOffset: TSFFloat;
public property FdOffset: TSFFloat read FFdOffset;
{ Event: SFRotation, out } { }
private FEventRotation_changed: TX3DEvent;
public property EventRotation_changed: TX3DEvent read FEventRotation_changed;
procedure Activate(const Time: TX3DTime;
const ATransform, AInvertedTransform: TMatrix4Single;
const OverPoint: TVector3Single); override;
procedure Deactivate(const Time: TX3DTime); override;
procedure Drag(const Time: TX3DTime; const RayOrigin, RayDirection: TVector3Single); override;
end;
TPlaneSensorNode = class(TAbstractDragSensorNode)
private
{ Plane* stuff is in global (VRML scene) coordinates. }
{ Plane of the activated sensor. }
Plane: TVector4Single;
{ Vectors indicating X and Y axis on the plane.
Always normalized (do not really have to be strictly orthogonal,
in case plane was transformed by some shearing; this is Ok). }
PlaneX, PlaneY: TVector3Single;
{ Initial intersection point at activation. Always lies on Plane. }
PlaneOrigin: TVector3Single;
{ Transformation from global coords to the local sensor coords. }
InvertedTransform: TMatrix4Single;
{ Was a translation_changed send during this activation of sensor. }
WasTranslation: boolean;
{ Last value of translation_changed send, meaningful only if WasTranslation. }
Translation: TVector3Single;
public
procedure CreateNode; override;
class function ClassNodeTypeName: string; override;
class function URNMatching(const URN: string): boolean; override;
private FFdAxisRotation: TSFRotation;
public property FdAxisRotation: TSFRotation read FFdAxisRotation;
private FFdMaxPosition: TSFVec2f;
public property FdMaxPosition: TSFVec2f read FFdMaxPosition;
private FFdMinPosition: TSFVec2f;
public property FdMinPosition: TSFVec2f read FFdMinPosition;
private FFdOffset: TSFVec3f;
public property FdOffset: TSFVec3f read FFdOffset;
{ Event: SFVec3f, out } { }
private FEventTranslation_changed: TX3DEvent;
public property EventTranslation_changed: TX3DEvent read FEventTranslation_changed;
procedure Activate(const Time: TX3DTime;
const ATransform, AInvertedTransform: TMatrix4Single;
const OverPoint: TVector3Single); override;
procedure Deactivate(const Time: TX3DTime); override;
procedure Drag(const Time: TX3DTime; const RayOrigin, RayDirection: TVector3Single); override;
end;
TSphereSensorNode = class(TAbstractDragSensorNode)
private
{ Sphere center and radius, in world coordinates. }
SphereCenter: TVector3Single;
SphereRadius: Single;
{ First intersection (at activation), in local coordinates. }
OriginalIntersection: TVector3Single;
{ Transformation from global coords to the local sensor coords. }
InvertedTransform: TMatrix4Single;
{ Was a rotation_changed send during this activation of sensor. }
WasRotation: boolean;
{ Last value of rotation_changed send, meaningful only if WasRotation. }
Rotation: TVector4Single;
public
procedure CreateNode; override;
class function ClassNodeTypeName: string; override;
class function URNMatching(const URN: string): boolean; override;
private FFdOffset: TSFRotation;
public property FdOffset: TSFRotation read FFdOffset;
{ Event: SFRotation, out } { }
private FEventRotation_changed: TX3DEvent;
public property EventRotation_changed: TX3DEvent read FEventRotation_changed;
procedure Activate(const Time: TX3DTime;
const ATransform, AInvertedTransform: TMatrix4Single;
const OverPoint: TVector3Single); override;
procedure Deactivate(const Time: TX3DTime); override;
procedure Drag(const Time: TX3DTime; const RayOrigin, RayDirection: TVector3Single); override;
end;
TTouchSensorNode = class(TAbstractTouchSensorNode)
public
procedure CreateNode; override;
class function ClassNodeTypeName: string; override;
class function URNMatching(const URN: string): boolean; override;
{ Event: SFVec3f, out } { }
private FEventHitNormal_changed: TX3DEvent;
public property EventHitNormal_changed: TX3DEvent read FEventHitNormal_changed;
{ Event: SFVec3f, out } { }
private FEventHitPoint_changed: TX3DEvent;
public property EventHitPoint_changed: TX3DEvent read FEventHitPoint_changed;
{ Event: SFVec2f, out } { }
private FEventHitTexCoord_changed: TX3DEvent;
public property EventHitTexCoord_changed: TX3DEvent read FEventHitTexCoord_changed;
end;
{$endif read_interface}
{$ifdef read_implementation}
{ TPointingDeviceSensorList ------------------------------------------------- }
function TPointingDeviceSensorList.EnabledCount: Integer;
var
I: Integer;
begin
Result := 0;
for I := 0 to Count - 1 do
if Enabled(I) then
Inc(Result);
end;
function TPointingDeviceSensorList.Enabled(Index: Integer): boolean;
begin
Result := (not (Items[Index] is TAbstractPointingDeviceSensorNode)) or
TAbstractPointingDeviceSensorNode(Items[Index]).FdEnabled.Value;
end;
procedure TPointingDeviceSensorList.Assign(Source: TPointingDeviceSensorList);
begin
inherited Assign(Source);
Transform := Source.Transform;
InvertedTransform := Source.InvertedTransform;
end;
{ Rest of nodes -------------------------------------------------------------- }
procedure TAbstractPointingDeviceSensorNode.CreateNode;
begin
inherited;
FFdDescription := TSFString.Create(Self, 'description', '');
Fields.Add(FFdDescription);
FEventIsOver := TX3DEvent.Create(Self, 'isOver', TSFBool, false);
Events.Add(FEventIsOver);
end;
procedure TAbstractPointingDeviceSensorNode.Activate(const Time: TX3DTime;
const ATransform, AInvertedTransform: TMatrix4Single;
const OverPoint: TVector3Single);
begin
EventIsActive.Send(true, Time);
end;
procedure TAbstractPointingDeviceSensorNode.Deactivate(const Time: TX3DTime);
begin
EventIsActive.Send(false, Time);
end;
procedure TAbstractDragSensorNode.CreateNode;
begin
inherited;
FFdAutoOffset := TSFBool.Create(Self, 'autoOffset', true);
Fields.Add(FFdAutoOffset);
FEventTrackPoint_changed := TX3DEvent.Create(Self, 'trackPoint_changed', TSFVec3f, false);
Events.Add(FEventTrackPoint_changed);
Fdenabled.ChangesAlways := FdEnabled.ChangesAlways + [chDragSensorEnabled];
end;
procedure TAbstractDragSensorNode.Drag(const Time: TX3DTime; const RayOrigin, RayDirection: TVector3Single);
begin
{ Nothing to do in this class }
end;
procedure TAbstractTouchSensorNode.CreateNode;
begin
inherited;
FEventTouchTime := TX3DEvent.Create(Self, 'touchTime', TSFTime, false);
Events.Add(FEventTouchTime);
end;
procedure TCylinderSensorNode.CreateNode;
begin
inherited;
FFdAxisRotation := TSFRotation.Create(Self, 'axisRotation', Vector3Single(0, 1, 0), 0);
Fields.Add(FFdAxisRotation);
FFdDiskAngle := TSFFloat.Create(Self, 'diskAngle', Pi/12);
Fields.Add(FFdDiskAngle);
{ X3D specification comment: [0,Pi/2] }
FFdMaxAngle := TSFFloat.Create(Self, 'maxAngle', -1);
Fields.Add(FFdMaxAngle);
{ X3D specification comment: [-2Pi,2Pi] }
FFdMinAngle := TSFFloat.Create(Self, 'minAngle', 0);
Fields.Add(FFdMinAngle);
{ X3D specification comment: [-2Pi,2Pi] }
FFdOffset := TSFFloat.Create(Self, 'offset', 0);
Fields.Add(FFdOffset);
{ X3D specification comment: (-Inf,Inf) }
FEventRotation_changed := TX3DEvent.Create(Self, 'rotation_changed', TSFRotation, false);
Events.Add(FEventRotation_changed);
DefaultContainerField := 'children';
end;
class function TCylinderSensorNode.ClassNodeTypeName: string;
begin
Result := 'CylinderSensor';
end;
class function TCylinderSensorNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNVRML97Nodes + ClassNodeTypeName) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
procedure TCylinderSensorNode.Activate(const Time: TX3DTime;
const ATransform, AInvertedTransform: TMatrix4Single;
const OverPoint: TVector3Single);
var
Transform, M, IM: TMatrix4Single;
CapsPlaneDir: PVector3Single;
begin
inherited;
Transform := ATransform;
InvertedTransform := AInvertedTransform;
{ Do not apply rotation when it's 0. A small optimization for a common case. }
if FdAxisRotation.RotationRad <> 0 then
begin
RotationMatricesRad(FdAxisRotation.RotationRad, FdAxisRotation.Axis, M, IM);
Transform := MatrixMult(Transform, M);
InvertedTransform := MatrixMult(IM, InvertedTransform);
end;
OriginalIntersection := MatrixMultPoint(InvertedTransform, OverPoint);
Caps := AngleRadBetweenVectors(OriginalIntersection,
Vector3Single(0, 1, 0)) < FdDiskAngle.Value;
OriginalIntersection[1] := 0;
if Caps then
begin
{ CapsPlane is parallel to Y=0 plane
(transformed by current sensor transform and by axisRotation),
and passing though PlaneOrigin. }
CapsPlaneDir := PVector3Single(@CapsPlane);
CapsPlaneDir^ := MatrixMultDirection(Transform, UnitVector3Single[1]);
CapsPlane[3] := -VectorDotProduct(CapsPlaneDir^, OverPoint);
end else
begin
CylinderAxisOrigin := MatrixMultPoint(Transform, ZeroVector3Single);
CylinderAxis := MatrixMultDirection(Transform, UnitVector3Single[1]);
CylinderRadius := Sqrt(PointToLineDistanceSqr(
CylinderAxisOrigin, CylinderAxis, OverPoint));
end;
WasRotation := false;
end;
procedure TCylinderSensorNode.Deactivate(const Time: TX3DTime);
begin
if FdAutoOffset.Value and WasRotation then
FdOffset.Send(RotationAngle);
inherited;
end;
procedure TCylinderSensorNode.Drag(const Time: TX3DTime; const RayOrigin, RayDirection: TVector3Single);
var
Intersection, LocalIntersection: TVector3Single;
WasIntersection: boolean;
begin
inherited;
if Caps then
WasIntersection := TryPlaneRayIntersection(Intersection,
CapsPlane, RayOrigin, RayDirection) else
WasIntersection := TryCylinderRayIntersection(Intersection,
CylinderAxisOrigin, CylinderAxis, CylinderRadius, RayOrigin, RayDirection);
if WasIntersection then
begin
try
LocalIntersection := MatrixMultPoint(InvertedTransform, Intersection);
except
on ETransformedResultInvalid do
begin
if Log then
WritelnLog('Drag sensor', 'Sensor transformation matrix^-1 cannot transform points.');
Exit;
end;
end;
EventTrackPoint_changed.Send(LocalIntersection, Time);
{ both OriginalIntersection and LocalIntersection have Y component set to 0
for the sake of calculating RotationAngle. They are both assumed to lie
nicely on a CapsPlane. }
LocalIntersection[1] := 0;
RotationAngle := RotationAngleRadBetweenVectors(
OriginalIntersection, LocalIntersection, UnitVector3Single[1]);
{ add offset, and clamp by min/maxAngle }
RotationAngle += FdOffset.Value;
if FdMinAngle.Value <= FdMaxAngle.Value then
Clamp(RotationAngle, FdMinAngle.Value, FdMaxAngle.Value);
EventRotation_changed.Send(Vector4Single(0, 1, 0, RotationAngle), Time);
WasRotation := true;
end;
end;
procedure TPlaneSensorNode.CreateNode;
begin
inherited;
FFdAxisRotation := TSFRotation.Create(Self, 'axisRotation', Vector3Single(0, 0, 1), 0);
Fields.Add(FFdAxisRotation);
FFdMaxPosition := TSFVec2f.Create(Self, 'maxPosition', Vector2Single(-1, -1));
Fields.Add(FFdMaxPosition);
{ X3D specification comment: (-Inf,Inf) }
FFdMinPosition := TSFVec2f.Create(Self, 'minPosition', Vector2Single(0, 0));
Fields.Add(FFdMinPosition);
{ X3D specification comment: (-Inf,Inf) }
FFdOffset := TSFVec3f.Create(Self, 'offset', Vector3Single(0, 0, 0));
Fields.Add(FFdOffset);
{ X3D specification comment: (-Inf,Inf) }
FEventTranslation_changed := TX3DEvent.Create(Self, 'translation_changed', TSFVec3f, false);
Events.Add(FEventTranslation_changed);
DefaultContainerField := 'children';
end;
class function TPlaneSensorNode.ClassNodeTypeName: string;
begin
Result := 'PlaneSensor';
end;
class function TPlaneSensorNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNVRML97Nodes + ClassNodeTypeName) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
procedure TPlaneSensorNode.Activate(const Time: TX3DTime;
const ATransform, AInvertedTransform: TMatrix4Single;
const OverPoint: TVector3Single);
var
PlaneDir: PVector3Single;
Transform, M, IM: TMatrix4Single;
begin
inherited;
PlaneOrigin := OverPoint;
Transform := ATransform;
InvertedTransform := AInvertedTransform;
{ Do not apply rotation when it's 0. A small optimization for a common case. }
if FdAxisRotation.RotationRad <> 0 then
begin
RotationMatricesRad(FdAxisRotation.RotationRad, FdAxisRotation.Axis, M, IM);
Transform := MatrixMult(Transform, M);
InvertedTransform := MatrixMult(IM, InvertedTransform);
end;
try
{ Plane is parallel to Z=0 plane
(transformed by current sensor transform and by axisRotation),
and passing though PlaneOrigin. }
PlaneDir := PVector3Single(@Plane);
PlaneDir^ := MatrixMultDirection(Transform, UnitVector3Single[2]);
Plane[3] := -VectorDotProduct(PlaneDir^, PlaneOrigin);
{ +X, +Y vectors, transformed by current sensor transform and
by axisRotation, normalized. }
PlaneX := Normalized(MatrixMultDirection(Transform, UnitVector3Single[0]));
PlaneY := Normalized(MatrixMultDirection(Transform, UnitVector3Single[1]));
except
on ETransformedResultInvalid do
begin
{ Transform matrix doesn't manage to transform directions.
So just assume it's identity, nothing more sensible to do. }
Plane := Vector4Single(0, 0, 1, -PlaneOrigin[2]);
PlaneX := UnitVector3Single[0];
PlaneY := UnitVector3Single[1];
Transform := IdentityMatrix4Single;
InvertedTransform := IdentityMatrix4Single;
if Log then
WritelnLog('Drag sensor', 'Sensor transformation matrix cannot transform directions.');
end;
end;
WasTranslation := false;
end;
procedure TPlaneSensorNode.Deactivate(const Time: TX3DTime);
begin
if FdAutoOffset.Value and WasTranslation then
FdOffset.Send(Translation);
inherited;
end;
procedure TPlaneSensorNode.Drag(const Time: TX3DTime; const RayOrigin, RayDirection: TVector3Single);
var
Intersection, TranslationGlobal: TVector3Single;
begin
inherited;
{ If no intersection, it's Ok to do nothing? }
if TryPlaneRayIntersection(Intersection, Plane, RayOrigin, RayDirection) then
begin
try
{ trackPoint_changed should be in the local sensor coordinates
(with axisRotation), without worrying about offset
(like for translation_changed).
This is sensible:
- track point should visualize the underlying geometry of the drag sensor
(plane, in this case)
- this makes track point sensible also for Sphere/CylinderSensor
(where offset is for rotations, and cannot be applied to
trackPoint_changed).
About using local coordinates:
FreeWRL and OpenVRML seems to also do this in sources,
and Octaga and InstantReality behavior suggests they do it too.
About using local coordinates with axisRotation:
That's more difficult, see
http://castle-engine.sourceforge.net/x3d_implementation_pointingdevicesensor.php }
EventTrackPoint_changed.Send(
MatrixMultPoint(InvertedTransform, Intersection), Time);
except
on ETransformedResultInvalid do
begin
EventTrackPoint_changed.Send(Intersection, Time);
if Log then
WritelnLog('Drag sensor', 'Sensor transformation matrix^-1 cannot transform points.');
end;
end;
{ An alternative implementation would transform Intersection to
local coords (with axisRotation, and with origin at PointOrigin).
Then applying the PointOrigin, PlaneX, PlaneY would be much simpler:
PointOrigin is just zero, and PlaneX = just (1, 0, 0).
So instead of VectorDotProduct(V, PlaneX) just take V[0]. }
TranslationGlobal := Intersection - PlaneOrigin;
{ map TranslationGlobal to the plane local coord system
(with axisRotation) }
Translation[0] := VectorDotProduct(TranslationGlobal, PlaneX);
Translation[1] := VectorDotProduct(TranslationGlobal, PlaneY);
Translation[2] := 0;
Translation += FdOffset.Value;
if FdMinPosition.Value[0] <= FdMaxPosition.Value[0] then
Clamp(Translation[0], FdMinPosition.Value[0], FdMaxPosition.Value[0]);
if FdMinPosition.Value[1] <= FdMaxPosition.Value[1] then
Clamp(Translation[1], FdMinPosition.Value[1], FdMaxPosition.Value[1]);
EventTranslation_changed.Send(Translation, Time);
WasTranslation := true;
end;
end;
procedure TSphereSensorNode.CreateNode;
begin
inherited;
FFdOffset := TSFRotation.Create(Self, 'offset', Vector3Single(0, 1, 0), 0);
Fields.Add(FFdOffset);
{ X3D specification comment: [-1,1],(-Inf,Inf) }
FEventRotation_changed := TX3DEvent.Create(Self, 'rotation_changed', TSFRotation, false);
Events.Add(FEventRotation_changed);
DefaultContainerField := 'children';
end;
class function TSphereSensorNode.ClassNodeTypeName: string;
begin
Result := 'SphereSensor';
end;
class function TSphereSensorNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNVRML97Nodes + ClassNodeTypeName) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
procedure TSphereSensorNode.Activate(const Time: TX3DTime;
const ATransform, AInvertedTransform: TMatrix4Single;
const OverPoint: TVector3Single);
begin
inherited;
InvertedTransform := AInvertedTransform;
OriginalIntersection := MatrixMultPoint(AInvertedTransform, OverPoint);
SphereCenter := MatrixMultPoint(ATransform, ZeroVector3Single);
SphereRadius := PointsDistance(OverPoint, SphereCenter);
WasRotation := false;
end;
procedure TSphereSensorNode.Deactivate(const Time: TX3DTime);
begin
if FdAutoOffset.Value and WasRotation then
FdOffset.Send(Rotation);
inherited;
end;
procedure TSphereSensorNode.Drag(const Time: TX3DTime; const RayOrigin, RayDirection: TVector3Single);
var
Intersection, LocalIntersection, RotationAxis: TVector3Single;
RotationAngle: Single;
RotationQ: TQuaternion;
begin
inherited;
{ If no intersection, it's Ok to do nothing? }
if TrySphereRayIntersection(Intersection, SphereCenter, SphereRadius,
RayOrigin, RayDirection) then
begin
try
LocalIntersection := MatrixMultPoint(InvertedTransform, Intersection);
except
on ETransformedResultInvalid do
begin
if Log then
WritelnLog('Drag sensor', 'Sensor transformation matrix^-1 cannot transform points.');
Exit;
end;
end;
EventTrackPoint_changed.Send(LocalIntersection, Time);
{ Rotation always contains offset }
RotationQ := QuatFromAxisAngle(FdOffset.Axis, FdOffset.RotationRad);
{ Add to RotationQ rotation from OriginalIntersection to current. }
RotationAxis := OriginalIntersection >< LocalIntersection;
if not ZeroVector(RotationAxis) then
begin
RotationAngle := RotationAngleRadBetweenVectors(
OriginalIntersection, LocalIntersection);
RotationQ := QuatFromAxisAngle(
Normalized(RotationAxis), RotationAngle) * RotationQ;
end;
Rotation := RotationQ.ToAxisAngle;
EventRotation_changed.Send(Rotation, Time);
WasRotation := true;
end;
end;
procedure TTouchSensorNode.CreateNode;
begin
inherited;
FEventHitNormal_changed := TX3DEvent.Create(Self, 'hitNormal_changed', TSFVec3f, false);
Events.Add(FEventHitNormal_changed);
FEventHitPoint_changed := TX3DEvent.Create(Self, 'hitPoint_changed', TSFVec3f, false);
Events.Add(FEventHitPoint_changed);
FEventHitTexCoord_changed := TX3DEvent.Create(Self, 'hitTexCoord_changed', TSFVec2f, false);
Events.Add(FEventHitTexCoord_changed);
DefaultContainerField := 'children';
end;
class function TTouchSensorNode.ClassNodeTypeName: string;
begin
Result := 'TouchSensor';
end;
class function TTouchSensorNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNVRML97Nodes + ClassNodeTypeName) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
procedure RegisterPointingDeviceSensorNodes;
begin
NodesManager.RegisterNodeClasses([
TCylinderSensorNode,
TPlaneSensorNode,
TSphereSensorNode,
TTouchSensorNode
]);
end;
{$endif read_implementation}
|