Sascha Willems wrote:Edit : I've removed the personal comments made by Delfi. If you have any personal problems to "discuss" with me, do it by PM or better don't tell me about it at all, I'm resistant to other peoples view and like to walk my own way. If you don't want to use the header cause you have problems with me, it's not my problem.
no problem, i'm sorry about it :/
here is my newtonworld unit with three quite useful functions
- Code: Select all
{ *********************************************************************** }
{ }
{ Newton Dynamics engine additional utility functionality unit }
{ }
{ Copyright (c) 2005 Jernej L. - Delfi (jernejcoder@gmail.com) }
{ }
{ *********************************************************************** }
unit NewtonWorld;
interface
uses newtonimport;
type
// a vector for general processing, use your own type
Vector= packed record
x, y, z: decimal;
end;
Pvector = ^Vector;
var
hitn: Vector;
intersectdistance: single;
intresectpoint: Vector;
procedure DebugShowCollision;
function WorldRay(start, stop: Vector): boolean;
function NewtonLocateBody(body: PNewtonBody): Vector;
procedure AddGlobalForce(var body: PNewtonBody; Force, Point: Vector);
implementation
// draw geometry lines
procedure DebugShowGeometryCollision( const body : PNewtonBody; vertexCount : int; const FaceArray : PFloat; faceId : int ); cdecl;
var
i: integer;
vn: integer;
procedure rendervertex(const num: integer);
var
V0: pointer;
begin
V0:= pointer(integer(FaceArray) + num * 12);
glVertex3fv(V0);
end;
begin
vn:= vertexCount-1;
for i:= 0 to vertexCount-1 do begin
rendervertex(vn);
rendervertex(i);
vn:= i;
end;
end;
// show rigid body collision geometry
procedure DebugShowBodyCollision (const body: Pnewtonbody); cdecl;
begin
NewtonBodyForEachPolygonDo(body, DebugShowGeometryCollision);
end;
// show all collision geometry in debug mode
procedure DebugShowCollision;
begin
NewtonWorldForEachBodyDo(nWorld, @DebugShowBodyCollision);
end;
// I hope this is translated correctly, i translated it from pseudocode in newton wiki
// it should be used in PhysicsApplyForceAndTorque callback
procedure AddGlobalForce(var body: PNewtonBody; Force, Point: Vector);
var
r: Vector;
BodyPos: Vector;
bodymatrix: Tmatrix4F;
torque: Vector;
begin
NewtonBodyGetMatrix(body, @bodymatrix);
Move(bodymatrix[3], bodypos, 12);
r:= subtractvectors(point, BodyPos);
Torque:= crossProduct(R, Force);
NewtonBodyAddForce(body, @Force);
NewtonBodyAddTorque(body, @Torque);
end;
// Return newton body position as vector
function NewtonLocateBody(body: PNewtonBody): Vector;
var
bodymatrix: Tmatrix4F;
begin
NewtonBodyGetMatrix(body, @bodymatrix);
Move(bodymatrix[3], result, 12);
end;
// Find first intresection point and return boolean if anything was hit,
// results are stored in global variables (so much about multithreading :P)
function WorldRay(start, stop: Vector): boolean;
var
point: Vector;
function NWRayCallback(const body : PNewtonBody; const hitNormal: PFloat; collisionID : Int; userData: Pointer; intersetParam: Float ) : Float; cdecl;
begin
anyhit:= true; // something was hit
intersectdistance:= intersetParam;
move(hitNormal, hitn, 12); // you have to copy this because callback gives you only pointer
result:= intersetParam; // tell newton to stop searching new intresecting points
end;
begin
result:= false;
anyhit:= false;
NewtonWorldRayCast(NWorld, @start, @stop, @NWRayCallback, nil);
intresectpoint.x:= start.x + intersectdistance * (stop.x - start.x);
intresectpoint.y:= start.y + intersectdistance * (stop.y - start.y);
intresectpoint.z:= start.z + intersectdistance * (stop.z - start.z);
result:= anyhit;
// try to render the ray visually
{$ifdef debugnewtonray}
glcolor3f(0, 0, 1);
glbegin(gl_lines);
glvertex3fv(@start);
glvertex3fv(@stop);
glend;
if anyhit = true then begin
glPointSize(3); // show the point bigger than just one pixel where the ray hit something
glcolor3f(1, 0, 0);
glbegin(gl_points);
glvertex3fv(@intresectpoint);
glend;
end;
{$endif}
end;
maybe other delphi (and pascal) programmers can find this useful
edit:
added improved collision geometry rendering function, this one is
much cleaner that the one in sacha's demos (if you like to copy & paste code)
it uses pointers given by newton directly, no copying of memory (so it is faster).
