PDA

View Full Version : Translating heavy math code from c/c++ to pascal [ Award!! ]



JernejL
12-04-2007, 09:34 AM
This is source code for a c++ implementation of a raycast car for newton dynamics:

http://www.gtatools.com/TDC/Development/rcv_pascal_test.rar

- Archive contains working delphi 7 demo that is using a bit older version of C++ DLL
- archive contains a test program made with c++ using c++ dll, there is also source code for the whole dll and initialization part of test program, sources compile with MSVC 6.x with no problem.
- there is also a non-working translation of code from c to pascal (sources in TDC_RCV.pas, rcv_records.pas contains the records), it all compiles but the car doesn't work as it should (note that there may be parts of code translated i commented out or added while testing it all).

There's also a small hack for glscene to make the 3d model i used to load, the whole thing compiles with delphi 7.

To whoever who can fix / translate the c++ code properly into pascal code (so that it compiles and works properly in test apps), i'm giving a copy of, err, nostalgic delphi 1 in excellent condition (not a scratch! ), which would look good in anyone's collection! :D I think it is client/server.. i know that delphi 2 to 7 also came with delphi 1 for free, but this is a original delphi 1 release with cd key and all (but no dead tree manuals)

http://www.gtatools.com/TDC/Development/delphi1.jpg

It's not much, but it's the best i can offer atm, but everybody would benefit from this: whole source code of this project will be availible to public, and it implements a stable raycast car dynamics for newton physics engine, with gearbox and a differential, which is quite a awesome thing in itself.

The translated source code is free for use in any project.

Sly
12-04-2007, 11:07 AM
Where's the rest of the C source? It's missing files like the MSVC project file, resman.h, parse.h, game1.h and many others.

JernejL
12-04-2007, 12:33 PM
There is no source code for the c++ test app, since that's not what is to translate, the goal is, to translate the DLL library source code in directory "RCV pascal test.rar\RCV pascal test\original C rcv project\rcv project\source\tdcvehicle\", the game1.cpp is just there to show how the demo program initializes the variables which are used in the gamelab3.exe which works with the dll which should be translated (except that pascal translation isn't a DLL but a normal pascal unit).

JernejL
16-04-2007, 11:04 AM
Ok, it's been 4 days, topic got 80 views, is anybody even remotely interested in helping? the whole problem with the code is probably just a simple wrongly translated vector/matrix routine :/

Traveler
16-04-2007, 11:54 AM
I do not think its a matter of interest, but rather of knowledge and time.

JSoftware
16-04-2007, 02:23 PM
Your matrix multiplication code is wrong
Here's the right version

function MultiplyMatrix(m, w: Tmatrix4FC): Tmatrix4FC;
var
x, y, i: integer;
begin
for x := 0 to 3 do
for y := 0 to 3 do
begin
result.arr[x,y] := 0;
for i := 0 to 3 do
result.arr[x, y] := result.arr[x, y]+ m.arr[x, i] * w.arr[i, y];
end;
end;

I couldn't figure the orientation out. You might need to transpose the result

Clootie
18-04-2007, 12:04 PM
Small addition: doing matrix multiplication with embedded cycle is really bad for CPUs. For performance reason you need to unroll your loop.

fire.tiger
19-04-2007, 07:14 AM
TDC_RCV.pas line 925 - procedure DriveTrainUpdate is strange.
in C code is :


void DriveTrainUpdate(TDriveTrain* pDTrain, float gas, int gear)
{
float fActiveRPM;
static int counter = 0;
counter += 1;
float brakeCoef;
// telemetry
RCVehicle* pv = (RCVehicle*)pDTrain->pRcv;
dFloat* ts = pv->fTimeSlice;
int ofs = 45;

PrintDebug("-DriveTrainUpdate [%d]-\n", counter);

in Pascal is :

procedure DriveTrainUpdate(var pDTrain: TDriveTrain; gas: single; gear: integer);
var
fActiveRPM: single;
counter: integer;
begin
counter := 0;
counter := counter + 1;

// PrintDebug("-----DriveTrainUpdate [%d] -------\n", counter);


I think there should be :

procedure DriveTrainUpdate(var pDTrain: TDriveTrain; gas: single; gear: integer);
var
fActiveRPM: Single;
counter: Integer;
brakeCoef: Single;
pv: RCVehicle;
ts: TFriction;
ofs: Integer;
begin
counter := 0;
Inc(counter);
pv := pDTrain.pRcv;
ts := pv.fTimeSlice;
ofs := 45;


Also from line 1019 it is strange...


there should be in pascal this ( I think you forgot brakeCoef variable in there)

This is just my short look.Maybe I'm wrong.
I'll take deeper look inside...this is just first thing that I've seen...

JernejL
19-04-2007, 08:17 AM
Most of these counter and ofs variables were part of histogram in original code which i haven't translated since they are just for debugging variables. i admit my translation of the code looks very rough and bad, so it may need another re-translation to get it right :S

I was suspecting force and torque newton callback to have an error and the code i uploaded isn't proper since i was doing some experiments there, this should be proper callback routine (still doesn't fix the issue):


procedure BaseBodyForceAndTorqueCallback(const body: PNewtonBody); cdecl;
var
veh: PRCVehicle;

Force, torque, v: Vector;
i: integer;
begin

veh := NewtonBodyGetUserData(body);

force:= veh.vTotal;
torque:= makevector(0,0,0);

for i:= 0 to 3 do
begin
force:= AddVectors(force, veh.sRay[i].vTotal);
v:= RotateVector(veh.sRay[i].vlCol, veh.mBody);
torque:= AddVectors(torque, MulVectors(v, veh.sRay[i].vTotal));
end;

torque:= AddVectors(torque, veh.vTorque);

NewtonBodyAddForce(veh.pBody, @force);
NewtonBodyAddTorque(veh.pBody, @torque);

NewtonBodyGetForce(veh.pBody, @force);
NewtonBodyGetTorque(veh.pBody, @torque);

end;

JernejL
04-05-2007, 08:20 PM
anybody else checked out anything? i'm still running in circles :(

JernejL
06-07-2007, 10:51 AM
100Ǩ to the first person that makes it work!

jdarling
06-07-2007, 01:21 PM
Delfi, what your asking isn't any trivial thing, especially given what you have provided. Some questions follow:

Can you name the exact C/C++ files that you specifically need converted?
Are you sure you have the exports proper for the methods you are calling?
Can you give a SIMPLE sample that doesn't require hacking another project?
* 2D top down a rectangle that "drives" around and shows what your looking for. As simple as you can make it.

Update your RAR to include the latest source you have. No sense in us having to re-work your work by having to read/patch every reply.

If that seems like too much to ask, then put your request on any of the bounty sites and see what happens. You might also try some of the Borland (sorry, Code Gear) News Groups.

Legolas
06-07-2007, 01:41 PM
Is there a particular reason about converting all dll source code and not using the dll itself, by translating header files only?

JernejL
06-07-2007, 03:25 PM
Legolas: Perhaps, because i want to compile code on multiple platforms rather than just one i need to translate the code, rather than mess with c compiler and linking process on each platform?

I'll put up the whole thing by the end of day in some more comfortable codebase to whoever wants to mess with the code.

the translated header files btw work with the C dll (i don't think that includes fixed pascal dll header).

JernejL
06-07-2007, 08:14 PM
This is working older version which uses older dll version (it shows nice dynamics with proper physics constants tho)
http://www.gtatools.com/TDC/Development/rcvcode/NEW%20STUFF/working%20pascal%20demo%20using%20older%20c%20dll. rar

This is delphi version, which uses C dll (works kinda, except the gearbox gear is always stuck in reverse)
http://www.gtatools.com/TDC/Development/rcvcode/NEW%20STUFF/delphi%20demo%20using%20dll.rar

This is version using my (broken) translation of the dll code, this is what i'm trying to make work properly:
http://www.gtatools.com/TDC/Development/rcvcode/NEW%20STUFF/broken_translation.rar

Small hack to make glscene work with the models in demo apps:
http://www.gtatools.com/TDC/Development/rcvcode/NEW%20STUFF/glscene%20fix.rar

This is the dll source code (what i'm trying to translate), (requires parts of newton SDK examples code, translated c dll is there AS "TDC_RCV.pas", i think some records might be in rcv_records.pas):
http://www.gtatools.com/TDC/Development/rcvcode/NEW%20STUFF/c%20dll%20code.rar

any other questions? post here, or ask on msn (msn only: stdcall@volja.net)

JernejL
08-07-2007, 03:11 PM
So who's up the challenge?

savage
09-07-2007, 10:18 AM
I'd give it a go, but I need to find the time. too many things ont he go at the moment. Btw, not for the prize, as I have Delphi 1 somewhere.

Setharian
09-07-2007, 01:49 PM
save the money ;) I looked at your pascal C header conversion and everything looks fine...possible problems are...bool<->boolean...in C++ sizeof(bool) is compiler-specific (doesn't have to be 1 byte), sizeof(enum_type) is also compiler-specific and can be minimum size required to express the enumeration or fixed 32/64 bits....basicly the problem should be in field size/alignment....also in the original C source there is no packing directive so it defaults to the default of the compiler used which can also cause problems...

JernejL
09-07-2007, 04:32 PM
save the money ;) I looked at your pascal C header conversion and everything looks fine...possible problems are...bool<->boolean...in C++ sizeof(bool) is compiler-specific (doesn't have to be 1 byte), sizeof(enum_type) is also compiler-specific and can be minimum size required to express the enumeration or fixed 32/64 bits....basicly the problem should be in field size/alignment....also in the original C source there is no packing directive so it defaults to the default of the compiler used which can also cause problems...

err, what do you mean?? there is a problem in matrix math, the goal is to get the code translated into pascal and make it work, There is a bug in some math routines or some other code that i must have translated errorenous, i couldn't locate the error after 2 rewrites. I could use the c dll if i wanted to, and i can use it from delphi (see one of example apps) so that header has no byte alingment problem, but the one used in delphi translated project doesn't matter what alingment it has, since it is all native to delphi / fpc, isn't it?

savage: there's now an additional 100Ǩ award, not just copy of delphi 1.

paul_nicholls
10-07-2007, 01:09 AM
TDC_RCV.pas line 925 - procedure DriveTrainUpdate is strange.
in C code is :


void DriveTrainUpdate&#40;TDriveTrain* pDTrain, float gas, int gear&#41;
&#123;
float fActiveRPM;
static int counter = 0;
counter += 1;
float brakeCoef;
// telemetry
RCVehicle* pv = &#40;RCVehicle*&#41;pDTrain->pRcv;
dFloat* ts = pv->fTimeSlice;
int ofs = 45;

PrintDebug&#40;"-DriveTrainUpdate &#91;%d&#93;-\n", counter&#41;;

in Pascal is :

procedure DriveTrainUpdate(var pDTrain: TDriveTrain; gas: single; gear: integer);
var
fActiveRPM: single;
counter: integer;
begin
counter := 0;
counter := counter + 1;

// PrintDebug("-----DriveTrainUpdate [%d] -------\n", counter);


I think there should be :

procedure DriveTrainUpdate(var pDTrain: TDriveTrain; gas: single; gear: integer);
var
fActiveRPM: Single;
counter: Integer;
brakeCoef: Single;
pv: RCVehicle;
ts: TFriction;
ofs: Integer;
begin
counter := 0;
Inc(counter);
pv := pDTrain.pRcv;
ts := pv.fTimeSlice;
ofs := 45;


Also from line 1019 it is strange...


there should be in pascal this ( I think you forgot brakeCoef variable in there)

This is just my short look.Maybe I'm wrong.
I'll take deeper look inside...this is just first thing that I've seen...

Unless I am mistaken, this code:

void DriveTrainUpdate&#40;TDriveTrain* pDTrain, float gas, int gear&#41;
&#123;
float fActiveRPM;
static int counter = 0;
counter += 1;
float brakeCoef;
// telemetry
RCVehicle* pv = &#40;RCVehicle*&#41;pDTrain->pRcv;
dFloat* ts = pv->fTimeSlice;
int ofs = 45;

PrintDebug&#40;"-DriveTrainUpdate &#91;%d&#93;-\n", counter&#41;;

initialize counter with 0 the very first time it runs (as it is static), and from then on each time it is called counter will be incremented by 1.

With the pascal version below:

procedure DriveTrainUpdate(var pDTrain: TDriveTrain; gas: single; gear: integer);
var
fActiveRPM: Single;
counter: Integer;
brakeCoef: Single;
pv: RCVehicle;
ts: TFriction;
ofs: Integer;
begin
counter := 0;
Inc(counter);
pv := pDTrain.pRcv;
ts := pv.fTimeSlice;
ofs := 45;

This will always make counter become equal to +1 each run...

this doesn't sound like the equivalent behavior at all...

perhaps you could try defining counter as (local to the procedure)

Const
counter: Integer = 0;


or

Var
counter: Integer = 0;

counter may have to be a global constant/variable instead of local for it to work...you need to try both methods (local to the procedure first, then global) to see if they work for you.

Hope this helps,
cheers,
Paul.

Setharian
10-07-2007, 08:50 AM
err, what do you mean?? there is a problem in matrix math, the goal is to get the code translated into pascal and make it work, There is a bug in some math routines or some other code that i must have translated errorenous, i couldn't locate the error after 2 rewrites. I could use the c dll if i wanted to, and i can use it from delphi (see one of example apps) so that header has no byte alingment problem, but the one used in delphi translated project doesn't matter what alingment it has, since it is all native to delphi / fpc, isn't it?

savage: there's now an additional 100Ǩ award, not just copy of delphi 1.
alignment still matters, but I clearly see no reason why you just cannot use the c dll after fixing alignment issues or use C++ builder to compile it into .obj files and link them into your Delphi code (FPC supports this?)...translating that code into delphi means translating part of newton into delphi which is kinda awkward (eg. dVector, dMatrix, dQuaternion)...

JernejL
10-07-2007, 10:31 AM
[quote="Setharian"]alignment still matters, but I clearly see no reason why you just cannot use the c dll after fixing alignment issues or use C++ builder to compile it into .obj files and ]

exactly because of that precise reasons, i am going to make changes to the car dynamics code, which i'd like to have in pascal, there is NO UNIFORM linking for at least 3 platforms on which i'd like to use this on (meaning i'd have to compile and link separately on each platform) also there are certain security issues if u use an externally linked library - people might just replace it with a hacked version and cheat.. delphi can link bcb obj files, but fpc DOES NOT link obj files made with delphi or bcb (wrong format). those dvector, dmatrix etc.. files are not part of newton, but of the sdk examples, i translated them already (but the translation might be broken)

JernejL
29-07-2007, 04:42 PM
Ok guys, stop being lazy, vacations are over, help me would you? :/

paul_nicholls
30-07-2007, 12:22 AM
Ok guys, stop being lazy, vacations are over, help me would you? :/

Did my previous post help you at all (Re: static variables)?
cheers,
Paul

vgo
30-07-2007, 05:06 PM
I had a quick look on the code and I haven't even been able to compile it yet... Why are you mixing VectorGeometry unit used by GLScene with your own stuff??? It's a total mess and one can't easily see what's going on...

I'm working on vehicle physics myself and the original C project may be useful for me as well... :)

JernejL
30-07-2007, 07:20 PM
I had a quick look on the code and I haven't even been able to compile it yet... Why are you mixing VectorGeometry unit used by GLScene with your own stuff??? It's a total mess and one can't easily see what's going on...


because i'm not using glscene where the code will be used, and i didn't write the original c++ code, i only translated it (twice, but both translations didn't work and i can't locate the flaw)



I'm working on vehicle physics myself and the original C project may be useful for me as well... :)

well, if you get this working, you can use the code for whatever you want.

JernejL
27-10-2007, 02:42 PM
I'm cancelling this.. it's not as if anyone shown much interest into solving this aniway.

paul_nicholls
27-10-2007, 11:05 PM
I'm cancelling this.. it's not as if anyone shown much interest into solving this aniway.

Hi Delfi,
you never said if what I posted helped you at all...

cheers,
Paul