⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oxcamerafollow.pas

📁 Newton Game Dynamic 1.52 Delphi下基于GLScene的OpenGL游戏开发控件。功能非常强大和易于使用。 Advanced physics engine for re
💻 PAS
字号:
// I have try this following solution and with vehicle looking to work good.
// It is not so easy to setup it correctly sorry.
// I try to make it better and more simple later.
// I don't have get times on v1.55 to improve this unit.
// I think to try to write a better method on next version because it is not easy
// to setup direction and distance.
unit oxCameraFollow;
{===============================================================================

 Version: MPL 1.1

 The contents of this file are subject to the Mozilla Public License Version
 1.1 (the "License"); you may not use this file except in compliance with
 the License. You may obtain a copy of the License at
 http://www.mozilla.org/MPL/


 Software distributed under the License is distributed on an "AS IS" basis,
 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 for the specific language governing rights and limitations under the
 License.

 The Original Code is "oxNewton opengl-Dynamics-delphi-component".

 The Initial Developer of the Original Code is
 Dave Gravel, OrionX3D Opengl & Delphi Programming, dave.gravel@cgocable.ca.
                       http://www.Dave.ServeUsers.com
                       http://www.k00m.sexidude.com

 Portions created by Dave Gravel are Copyright (C) 2004 - 2006.
 Dave Gravel. All Rights Reserved.

 Contributor(s): GLScene (http://www.glscene.org) -
 Julio Jerez and Alain Suero (http://www.newtondynamics.com) -
 Sascha Willems (www.delphigl.de)

================================================================================
 oxNewton v1.55 by Dave Gravel.

PS: I request only one thing from the users of my oxNewton Component,
it is to put on your about or your help a comment saying you using the
oxNewton Component with my name Dave Gravel and my
e-mail: dave.gravel@cgocable.ca
Don't modify or remove any comment or information on my file
if you do some modification on my code please contact me.
Read the Newton license too, it is realy important.

================================================================================}
interface
  uses forms, classes, Dialogs, windows, GLScene, GLObjects, VectorGeometry,
       GLTexture, GLMisc,
  // oxNewton
  oxNewtonVehicle4Wheels, oxNewtonManager, oxNewtonUtils, oxNewtonImport;
{******************************************************************************}
// [15-9-2007]: oxCameraFollow last change by Dave Gravel.                   //
{******************************************************************************}
type  
  TOXCameraVehicleFollow = class( TComponent )
    private
      FOrionX3D: byte;
      FDistanceTo: Float;
      FMSDelay: Float;
      FMajorSpeed: Float;
      FMinorSpeed: Float;
      FActived: boolean;
      FManager: TOXNewtonManager;
      FDummy: TGLDummyCube;
      FCamera: TGLCamera;
      FVehicle: TOXNewtonVehicle;
      FPointDir: TGLCoordinates;
      FPosition: TGLCoordinates;
      procedure SetManager( val: TOXNewtonManager );
      procedure SetCamera( val: TGLCamera );
      procedure SetVehicle( val: TOXNewtonVehicle );
      procedure SetDistanceTo( val: Float );
      procedure SetDelay( val: Float );
      procedure SetMajorSpeed( val: Float );
      procedure SetMinorSpeed( val: Float );
    public
      procedure InitNewton; virtual;
      procedure Follow;
      procedure Notification( AComponent: TComponent; Operation: TOperation ); override;
      constructor Create( aOwner: TComponent ); override;
      destructor Destroy; override;
    protected
      procedure PointVehicle;
    published
      property PointDir: TGLCoordinates read FPointDir write FPointDir;
      property Position: TGLCoordinates read FPosition write FPosition;
      property DistanceTo: Float read FDistanceTo write SetDistanceTo;
      property Delay: Float read FMSDelay write SetDelay;
      property MajorSpeed: Float read FMajorSpeed write SetMajorSpeed;
      property MinorSpeed: Float read FMinorSpeed write SetMinorSpeed;
      property Manager: TOXNewtonManager read FManager write SetManager;
      property Camera: TGLCamera read FCamera write SetCamera;
      property Vehicle: TOXNewtonVehicle read FVehicle write SetVehicle;
  end;
implementation
{******************************************************************************}
// [15-9-2007]: oxCameraFollow last change by Dave Gravel.                   //
{******************************************************************************}
constructor TOXCameraVehicleFollow.Create( aOwner: TComponent );
begin
  inherited Create( aOwner );
  FOrionX3D:= 100;  
  FPointDir:= TGLCoordinates.CreateInitialized( Self, VectorMake( 0, 0, 1 ), csVector );
  FPosition:= TGLCoordinates.CreateInitialized( Self, VectorMake( 0, 0, 0 ), csVector );
  FActived:= false;
  FDistanceTo:= 10;
  FMSDelay:= 1000;
  FMajorSpeed:= 0.0;
  FMinorSpeed:= 0.1;
end;
{******************************************************************************}
// [15-9-2007]: oxCameraFollow last change by Dave Gravel.                   //
{******************************************************************************}
procedure TOXCameraVehicleFollow.Notification( AComponent: TComponent; Operation: TOperation );
begin
  if ( Operation = opRemove ) then begin
    if ( AComponent = Manager ) then begin
      Manager:= nil;
      FActived:= False;
    end;
    if ( AComponent = Camera ) then begin
      if ( csDesigning in ComponentState ) then
        FCamera.Parent:= FManager.Objects;
      Camera:= nil;
      FActived:= False;
    end;
    if ( AComponent = Vehicle ) then begin
      Vehicle:= nil;
      FActived:= False;
    end;
  end;
  inherited;
end;
{******************************************************************************}
// [15-9-2007]: oxCameraFollow last change by Dave Gravel.                   //
{******************************************************************************}
destructor TOXCameraVehicleFollow.Destroy;
begin
  FActived:= False;
  if not ( csDesigning in ComponentState ) then
    FCamera.Parent:= FManager.Objects;
  FPointDir.Free;
  FPosition.Free;
  if Assigned( FDummy ) then FDummy.Free;
  inherited Destroy;
end;
{******************************************************************************}
// [15-9-2007]: oxCameraFollow last change by Dave Gravel.                   //
{******************************************************************************}
procedure TOXCameraVehicleFollow.InitNewton;
begin
  if not assigned( FManager ) and not assigned( FVehicle ) and not assigned( FCamera ) then exit;
  FDummy:= TGLDummyCube( FManager.Objects.AddNewChild( TGLDummyCube ) );
  FDummy.Position:= FPosition;
  FCamera.TargetObject:= nil;
  FCamera.Parent:= FDummy;
  FActived:= true;
  PointVehicle;
end;
{******************************************************************************}
// [15-9-2007]: oxCameraFollow last change by Dave Gravel.                   //
{******************************************************************************}
procedure TOXCameraVehicleFollow.PointVehicle;
begin
  if not FActived then exit;
  FDummy.PointTo( ( FVehicle ), FPointDir.AsVector );
end;
{******************************************************************************}
// [15-9-2007]: oxCameraFollow last change by Dave Gravel.                   //
{******************************************************************************}
procedure TOXCameraVehicleFollow.Follow;
var
  speed: Float;
begin
  if not FActived then exit;
    speed:= FVehicle.GetSpeed;
    if speed > FMajorSpeed then FDummy.PointTo( ( FVehicle ), FPointDir.AsVector );
    while ( speed >= FMinorSpeed ) do begin speed:= speed - FMinorSpeed;
      if FDummy.DistanceTo( FVehicle ) > FDistanceTo then FDummy.Move( speed / FMSDelay );
    end;
end;
{******************************************************************************}
// [15-9-2007]: oxCameraFollow last change by Dave Gravel.                   //
{******************************************************************************}
procedure TOXCameraVehicleFollow.SetDistanceTo( val: Float );
begin
  if ( val <> FDistanceTo ) then FDistanceTo:= val;
end;
{******************************************************************************}
// [15-9-2007]: oxCameraFollow last change by Dave Gravel.                   //
{******************************************************************************}
procedure TOXCameraVehicleFollow.SetDelay( val: Float );
begin
  if ( val <> FMSDelay ) then FMSDelay:= val;
end;
{******************************************************************************}
// [15-9-2007]: oxCameraFollow last change by Dave Gravel.                   //
{******************************************************************************}
procedure TOXCameraVehicleFollow.SetMajorSpeed( val: Float );
begin
  if ( val <> FMajorSpeed ) then FMajorSpeed:= val;
end;
{******************************************************************************}
// [15-9-2007]: oxCameraFollow last change by Dave Gravel.                   //
{******************************************************************************}
procedure TOXCameraVehicleFollow.SetMinorSpeed( val: Float );
begin
  if ( val <> FMinorSpeed ) then FMinorSpeed:= val;
end;
{******************************************************************************}
// [15-9-2007]: oxCameraFollow last change by Dave Gravel.                   //
{******************************************************************************}
procedure TOXCameraVehicleFollow.SetVehicle( val: TOXNewtonVehicle );
begin
  if ( FVehicle <> val ) then FVehicle:= val;
end;
{******************************************************************************}
// [15-9-2007]: oxCameraFollow last change by Dave Gravel.                   //
{******************************************************************************}
procedure TOXCameraVehicleFollow.SetManager( val: TOXNewtonManager );
begin
  if ( FManager <> val ) then FManager:= val;
end;
{******************************************************************************}
// [15-9-2007]: oxCameraFollow last change by Dave Gravel.                   //
{******************************************************************************}
procedure TOXCameraVehicleFollow.SetCamera( val: TGLCamera );
begin
  if ( FCamera <> val ) then FCamera:= val;
end;
{******************************************************************************}
{$D '[15-9-2007]: TOXNewtonManager v1.55 by Dave Gravel under MPL-1.1 license.'}
{******************************************************************************}
initialization
  RegisterClasses( [ TOXCameraVehicleFollow ] );

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -