Built-in NewtonBodyAddForceAtPosition()

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Built-in NewtonBodyAddForceAtPosition()

Postby Leadwerks » Thu Oct 30, 2008 10:34 pm

Here's the code. I think a built-in function would help a lot of people:
Code: Select all
   Method AddForceAtPosition(force:TVec3,position:TVec3)
      Local torque:TVec3
      Local r:TVec3
      r=position.minus(mat.translation())
      torque=r.cross(force)
      torque.x=Degrees(torque.x)
      torque.y=Degrees(torque.y)
      torque.z=Degrees(torque.z)
      Self.force=Self.force.plus(force)
      fx=Self.force.x
      fy=Self.force.y
      fz=Self.force.z
      Self.torque=Self.torque.plus(torque)
      tx=Self.torque.x
      ty=Self.torque.y
      tz=Self.torque.z
      Unfreeze()
   EndMethod
User avatar
Leadwerks
 
Posts: 539
Joined: Fri Oct 27, 2006 2:54 pm

Re: Built-in NewtonBodyAddForceAtPosition()

Postby JernejL » Fri Oct 31, 2008 8:59 am

Code: Select all
function GetPointGlobalVelocity(const body: PNewtonBody; Point: TVector): TVector;
function GetPointLocalVelocity(const body: PNewtonBody; Point: TVector): TVector;
procedure AddPointGlobalForce(const body: PNewtonBody; PrevBodyMatrix: pointer; Force, Point: TVector);
procedure AddPointLocalForce(const body: PNewtonBody; Force, Point: TVector);



Code: Select all

function MatrixRotateVector(vec: TVector; m: Tmatrix): TVector; // tested, works.
begin
  Result[0] := vec[0] * m[0, 0] + vec[1] * m[1, 0] + vec[2] * m[2, 0];
  Result[1] := vec[0] * m[0, 1] + vec[1] * m[1, 1] + vec[2] * m[2, 1];
  Result[2] := vec[0] * m[0, 2] + vec[1] * m[1, 2] + vec[2] * m[2, 2];
end;

function MatrixUNRotateVector(vec: TVector; m: Tmatrix): TVector; // tested, works.
begin
  Result[0] := vec[0] * m[0][0] + vec[1] * m[0][1] + vec[2] * m[0][2];
  Result[1] := vec[0] * m[1][0] + vec[1] * m[1][1] + vec[2] * m[1][2];
  Result[2] := vec[0] * m[2][0] + vec[1] * m[2][1] + vec[2] * m[2][2];
end;

function MatrixTransformVector(vec: TVector; m: Tmatrix): TVector; // tested, works.
begin

  Result[0] := vec[0] * m[0][0] + vec[1] * m[1][0] + vec[2] * m[2][0] + m[3][0];
  Result[1] := vec[0] * m[0][1] + vec[1] * m[1][1] + vec[2] * m[2][1] + m[3][1];
  Result[2] := vec[0] * m[0][2] + vec[1] * m[1][2] + vec[2] * m[2][2] + m[3][2];
end;

// retrieves body's velocity at specific point in global space
function GetPointGlobalVelocity(const body: PNewtonBody; Point: TVector): TVector;
var
  velocity, omega: TVector;
begin
  NewtonBodyGetVelocity(body, @velocity);
  NewtonBodyGetOmega(body, @omega);

  Result := VectorAdd(velocity, VectorCrossProduct(omega, point));
end;

// retrieves body's velocity at specific point in local space
function GetPointLocalVelocity(const body: PNewtonBody; Point: TVector): TVector;
var
  bodymatrix: Tmatrix;
begin
  NewtonBodyGetMatrix(body, @bodymatrix);
  Result := GetPointGlobalVelocity(body, MatrixRotateVector(Point, bodymatrix));
  Result := MatrixUNRotateVector(Result, bodymatrix);
end;

// adds force to body at a specified point in global space
procedure AddPointGlobalForce(const body: PNewtonBody; PrevBodyMatrix: pointer; Force, Point: TVector);
var
  GlobalForce: TVector;
  bodymatrix:  Tmatrix;
  Torque:      TVector;
begin

  if PrevBodyMatrix = nil then
    NewtonBodyGetMatrix(body, @bodymatrix)
  else
    move(PrevBodyMatrix^, bodymatrix, sizeof(Tmatrix));

  GlobalForce[0] := Point[0] - bodymatrix[3][0];
  GlobalForce[1] := Point[1] - bodymatrix[3][1];
  GlobalForce[2] := Point[2] - bodymatrix[3][2];

  Torque := VectorCrossProduct(GlobalForce, Force);

  NewtonBodyAddForce(body, @Force[0]);
  NewtonBodyAddTorque(body, @Torque[0]);
end;

// adds force to body at a specified point in local space
procedure AddPointLocalForce(const body: PNewtonBody; Force, Point: TVector);
var
  GlobalForce, GlobalPoint: TVector;
  bodymatrix: Tmatrix;
begin
  NewtonBodyGetMatrix(body, @bodymatrix);

  GlobalForce := MatrixRotateVector(Force, bodymatrix);
  GlobalPoint := MatrixTransformVector(Point, bodymatrix);

  AddPointGlobalForce(body, @bodymatrix, GlobalForce, GlobalPoint);
end;



Same functionality in delphi + additional functions like getlocalvelocity and some math routines.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1531
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 3 guests