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

📄 oxnewtonhelpsingle.pas

📁 Newton Game Dynamic 1.52 Delphi下基于GLScene的OpenGL游戏开发控件。功能非常强大和易于使用。 Advanced physics engine for re
💻 PAS
字号:
unit oxNewtonHelpSingle;
{******************************************************************************}
// [15-9-2007]: oxNewtonHelper last change by Dave Gravel.                   //
{******************************************************************************}
{===============================================================================

 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.

================================================================================
This helper unit is write from a delphi newton user Pierre Lemerle [blackbird_dream].
Thanks to this he to let's me use this unit helper in oxNewton, because it is very
usefull.
================================================================================

================================================================================}
interface
uses
  oxNewtonimport, Vectortypes, VectorGeometry;
{******************************************************************************}
// [15-9-2007]: Utils last change by Dave Gravel.                            //
{******************************************************************************}
procedure AddForceAtPos( Body: PNewtonBody; Force: Tvector3f; Point: Tvector3f );
procedure AddForceAtRelPos( Body: PNewtonBody; Force: Tvector3f; Point: Tvector3f );
procedure AddRelForceAtRelPos( Body: PNewtonBody; Force: Tvector3f; Point: Tvector3f );
procedure AddRelForceAtPos( Body: PNewtonBody; Force: Tvector3f; Point: Tvector3f );
function Rel2AbsPoint( Body: PNewtonBody; Pointrel: Tvector3f ): Tvector3f; overload;
function Rel2AbsVector( Body: PNewtonBody; Vectorrel: Tvector3f ): Tvector3f; overload;
procedure Rel2AbsPoint( Body: PNewtonBody; Pointrel: Tvector3f; var Pointabs: Tvector3f ); overload;
procedure Rel2AbsVector( Body: PNewtonBody; Vectorrel: Tvector3f; var Vectorabs: Tvector3f ); overload;
function AbsVelatRelPos( Body: PNewtonBody; Point: Tvector3f ): Tvector3f; overload;
procedure AbsVelatRelPos( Body: PNewtonBody; Point: Tvector3f; var AbsVel: Tvector3f ); overload;
function AbsVelatPos( Body: PNewtonBody; Point: Tvector3f ): Tvector3f; overload;
procedure AbsVelatPos( Body: PNewtonBody; Point: Tvector3f; var AbsVel: Tvector3f ); overload;
function AbsAccatRelPos( Body: PNewtonBody; Point: Tvector3f ): Tvector3f;
implementation
{******************************************************************************}
// [15-9-2007]: Utils last change by Dave Gravel.                            //
{******************************************************************************}
procedure AddForceAtPos( Body: PNewtonBody; Force: Tvector3f; Point: Tvector3f );
var
  R, Torque, com: Tvector3f;
begin
  NewtonBodygetCentreOfMass( Body, @com );
  R:= VectorSubtract( Point, Rel2Abspoint( Body, com ) );
  Torque:= VectorCrossProduct( R, Force );
  Newtonbodyaddforce( Body, @Force );
  Newtonbodyaddtorque( Body, @Torque );
end;
{******************************************************************************}
// [15-9-2007]: Utils last change by Dave Gravel.                            //
{******************************************************************************}
procedure AddForceAtRelPos( Body: PNewtonBody; Force: Tvector3f; Point: Tvector3f );
begin
  if vectorlength( Force ) <> 0 then
  AddForceAtPos( Body, Force, rel2abspoint( body, Point ) );
end;
{******************************************************************************}
// [15-9-2007]: Utils last change by Dave Gravel.                            //
{******************************************************************************}
procedure AddRelForceAtRelPos( Body: PNewtonBody; Force: Tvector3f; Point: Tvector3f );
begin
  if vectorlength( Force ) <> 0 then
    AddForceAtPos( Body, rel2absvector( body, force ), rel2abspoint( body, Point ) );
end;
{******************************************************************************}
// [15-9-2007]: Utils last change by Dave Gravel.                            //
{******************************************************************************}
procedure AddRelForceAtPos( Body: PNewtonBody; Force: Tvector3f; Point: Tvector3f );
begin
  if vectorlength( Force ) <> 0 then
    AddForceAtPos( Body, rel2absvector( body, force ), Point );
end;
{******************************************************************************}
// [15-9-2007]: Utils last change by Dave Gravel.                            //
{******************************************************************************}
procedure Rel2AbsPoint( Body: PNewtonBody; Pointrel: Tvector3f; var Pointabs: Tvector3f );
var
  M: Tmatrix4f;
  P: Tvector4f;
begin
  NewtonBodyGetMatrix( Body, @M[0,0] );
  P:= vectormake( Pointrel[0], Pointrel[1], Pointrel[2], 1 );
  Pointabs:= affinevectormake( VectorTransform( P, M ) );
end;
{******************************************************************************}
// [15-9-2007]: Utils last change by Dave Gravel.                            //
{******************************************************************************}
function Rel2AbsPoint( Body: PNewtonBody; Pointrel: Tvector3f ):Tvector3f;
var
  M: Tmatrix4f;
  P: Tvector4f;
begin
  result:= affinevectormake( 0, 0, 0 );
  NewtonBodyGetMatrix( Body, @M[0, 0] );
  P:= vectormake( Pointrel[0], Pointrel[1], Pointrel[2], 1 );
  result:= affinevectormake( VectorTransform( P, M ) );
end;
{******************************************************************************}
// [15-9-2007]: Utils last change by Dave Gravel.                            //
{******************************************************************************}
procedure Rel2AbsVector( Body: PNewtonBody; Vectorrel: Tvector3f; var Vectorabs: Tvector3f );
var
  M: Tmatrix4f;
  P: Tvector4f;
begin
  NewtonBodyGetMatrix( Body, @M[0, 0] );
  P:= vectormake( Vectorrel[0], Vectorrel[1], Vectorrel[2], 0 );
  Vectorabs:= affinevectormake( VectorTransform( P, M ) );
end;
{******************************************************************************}
// [15-9-2007]: Utils last change by Dave Gravel.                            //
{******************************************************************************}
function Rel2AbsVector( Body: PNewtonBody; Vectorrel: Tvector3f ):Tvector3f;
var
  M: Tmatrix4f;
  P: Tvector4f;
begin
  result:= affinevectormake( 0, 0, 0 );
  NewtonBodyGetMatrix( Body, @M[0, 0] );
  P:= vectormake( Vectorrel[0], Vectorrel[1], Vectorrel[2], 0 );
  result:= affinevectormake( VectorTransform( P, M ) );
end;
{******************************************************************************}
// [15-9-2007]: Utils last change by Dave Gravel.                            //
{******************************************************************************}
function AbsVelatRelPos( Body: PNewtonBody; Point: Tvector3f ): Tvector3f;
var
  omega, CenterVel, CoM: Tvector3f;
begin
  NewtonBodygetCentreOfMass( Body, @com );
  result:= affinevectormake( 0, 0, 0 );
  NewtonBodyGetVelocity( Body, @CenterVel );
  NewtonBodyGetOmega( Body, @omega );
  result:= vectoradd( CenterVel, vectorcrossproduct( omega, vectorsubtract( Rel2AbsPoint( Body, point ), Rel2Abspoint( Body, com ) ) ) );
end;
{******************************************************************************}
// [15-9-2007]: Utils last change by Dave Gravel.                            //
{******************************************************************************}
procedure AbsVelatRelPos( Body: PNewtonBody; Point: Tvector3f; var AbsVel: Tvector3f );
var
  omega, CenterVel, CoM: Tvector3f;
begin
  NewtonBodygetCentreOfMass( Body, @com );
  NewtonBodyGetVelocity( Body, @CenterVel );
  NewtonBodyGetOmega( Body, @omega );
  AbsVel:= vectoradd( CenterVel, vectorcrossproduct( omega, vectorsubtract( Rel2AbsPoint( Body, point ), Rel2Abspoint( Body, com ) ) ) );
end;
{******************************************************************************}
// [15-9-2007]: Utils last change by Dave Gravel.                            //
{******************************************************************************}
function AbsVelatPos( Body: PNewtonBody; Point: Tvector3f ): Tvector3f;
var
  omega, CenterVel, CoM: Tvector3f;
begin
  NewtonBodygetCentreOfMass( Body, @com);
  result:= affinevectormake( 0, 0, 0 );
  NewtonBodyGetVelocity( Body, @CenterVel );
  NewtonBodyGetOmega( Body, @omega );
  result:= vectoradd( CenterVel, vectorcrossproduct( omega, vectorsubtract( point, Rel2Abspoint( Body, com ) ) ) );
end;
{******************************************************************************}
// [15-9-2007]: Utils last change by Dave Gravel.                            //
{******************************************************************************}
procedure AbsVelatPos( Body: PNewtonBody; Point: Tvector3f; var AbsVel: Tvector3f );
var
  omega, CenterVel, CoM: Tvector3f;
begin
  NewtonBodygetCentreOfMass( Body, @com );
  NewtonBodyGetVelocity( Body, @CenterVel );
  NewtonBodyGetOmega( Body, @omega );
  AbsVel:= vectoradd( CenterVel, vectorcrossproduct( omega, vectorsubtract( point, Rel2Abspoint( Body, com ) ) ) );
end;
{******************************************************************************}
// [15-9-2007]: Utils last change by Dave Gravel.                            //
{******************************************************************************}
function AbsAccatRelPos( Body: PNewtonBody; Point: Tvector3f ): Tvector3f;
var
  M, M0: Tmatrix4f;
  P: Tvector4f;
  omega, CoM, Force, Torque, Alpha: Tvector3f;
  invMass, invIxx, invIyy, invIzz: single;
begin
  result:= affinevectormake( 0, 0, 0 );
  NewtonBodyGetMatrix( Body, @M[0, 0] );
  NewtonBodyGetInvMass(Body, @invMass, @invIxx, @invIyy, @invIzz );
  M0:= M;
  Invertmatrix( M );
  Newtonbodygetforce( Body, @Force ) ;
  Newtonbodygettorque( Body, @Torque ) ;
  P:= vectormake( Torque[0], Torque[1], Torque[2], 0 );
  Alpha:= affinevectormake( VectorTransform( P, M ) );
  scalevector( Alpha, affinevectormake( invIxx, invIyy, invIzz ) );
  P:= vectormake( Alpha[0], Alpha[1], Alpha[2], 0 );
  Alpha:= affinevectormake( VectorTransform( P, M0 ) );
  Force:= VectorScale( Force, invMass );
  NewtonBodyGetOmega( Body, @omega );
  NewtonBodygetCentreOfMass( Body, @com );
  result:= vectoradd( vectoradd( Force, vectorcrossproduct( Alpha,
  vectorsubtract( Rel2AbsPoint( Body, point ), Rel2Abspoint( Body, com ) ) ) ),
  vectorcrossproduct( omega, vectorcrossproduct( omega,
  vectorsubtract( Rel2AbsPoint( Body, point ), Rel2Abspoint( Body, com ) ) ) ) );
end;
{******************************************************************************}
{$D '[15-9-2007]: TOXNewtonManager v1.55 by Dave Gravel under MPL-1.1 license.'}
{******************************************************************************}
end.

⌨️ 快捷键说明

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