Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
0.8.3:interaction [2020/11/17 18:53] – external edit 127.0.0.10.8.3:interaction [2022/04/20 15:45] (current) – [Classic Camera Mouse Movement] admin
Line 85: Line 85:
  
 Another solution is to include rotation behaviour in each object related mouse-move event, or to use TGorillaInputController with global mouse hooks ([[inputpolling|Input Polling]]). Another solution is to include rotation behaviour in each object related mouse-move event, or to use TGorillaInputController with global mouse hooks ([[inputpolling|Input Polling]]).
 +
 +==== Classic Camera Mouse Movement  ====
 +
 +Very often users want to implement  simple camera movement controlled by mouse.
 +Here is a simple example how to move your camera in your scene.
 +
 +It expects a simple component setup before it works.
 +  - Put A TDummy onto your viewport
 +  - Put A TCamera / TGorillaCamera inside this TDummy component
 +  - Position the TCamera/TGorillaCamera component at a position like (0.0, -2.5, -10)
 +  - Set TCamera/TGorillaCamera "Target" to the Dummy
 +  - Do not forget to set the "Camera" as active in your viewport
 +  - Do not forget to disable "UseDesignCamera"
 +
 +Then you can create 4 events by the event editor in your IDE object inspector with the following code:
 +
 +<file pascal>
 +var FMove : Boolean;
 +    FLastPos : TPointF;
 +    FSpeed : Single = 0.1;
 +
 +procedure TGameWin.GorillaViewport1MouseDown(Sender: TObject;
 +  Button: TMouseButton; Shift: TShiftState; X, Y: Single);
 +begin
 +  /// allow camera movement
 +  FMove := true;
 +  /// store the current mouse position to detect the pixel movement
 +  FLastPos := PointF(X, Y);
 +end;
 +
 +procedure TGameWin.GorillaViewport1MouseMove(Sender: TObject;
 +  Shift: TShiftState; X, Y: Single);
 +var LDiff : TPointF;
 +    LDir  : TPoint3D;
 +begin
 +  /// if mouse was not down - skip camera movement here
 +  if not FMove then
 +  begin
 +    FLastPos := PointF(X, Y);
 +    Exit;
 +  end;
 +
 +  /// compute the offset to the last pixel position
 +  LDiff := PointF(X, Y) - FLastPos;
 +
 +  if ssRight in Shift then
 +  begin
 +    /// rotate camera on right mouse button down
 +    Dummy1.RotationAngle.Y := Dummy1.RotationAngle.Y + LDiff.X;
 +  end
 +  else if ssLeft in Shift then
 +  begin
 +    /// move camera in view direction if left mouse button is down
 +    LDir := TPoint3D.Zero;
 +
 +    /// at first we apply the left-side direction 
 +    LDir := LDir + TPoint3D(GorillaCamera1.AbsoluteLeft) * (LDiff.X * FSpeed);
 +    /// then we apply the forward direction
 +    LDir := LDir + TPoint3D(GorillaCamera1.AbsoluteDirection) * (LDiff.Y * FSpeed);
 +
 +    /// we move the parent dummy, not the camera itself!
 +    /// simply by adding our direction vector to the current dummy position
 +    Dummy1.Position.Point := Dummy1.Position.Point + LDir;
 +  end;
 +
 +  FLastPos := PointF(X, Y);
 +end;
 +
 +procedure TGameWin.GorillaViewport1MouseUp(Sender: TObject;
 +  Button: TMouseButton; Shift: TShiftState; X, Y: Single);
 +begin
 +  FLastPos := PointF(X, Y);
 +  FMove := false;
 +end;
 +
 +procedure TGameWin.GorillaViewport1MouseWheel(Sender: TObject;
 +  Shift: TShiftState; WheelDelta: Integer; var Handled: Boolean);
 +begin
 +  if WheelDelta < 0 then
 +    GorillaCamera1.Position.Z := GorillaCamera1.Position.Z - 0.5
 +  else
 +    GorillaCamera1.Position.Z := GorillaCamera1.Position.Z + 0.5;
 +
 +  Handled := true;
 +end;
 +</file>
 ==== On components ==== ==== On components ====