Kraft - a 3D physics engine in Object Pascal

Post Reply
Posts: 8
Joined: Fri Apr 17, 2015 5:50 am
Location: Moenchengladbach, Germany

Kraft - a 3D physics engine in Object Pascal

Post by BeRo »

Hello everyone,

as already indicated in another thread, I published my physics engine on GitHub now. It is licensed under the zlib license. It is implemented in Object Pascal.

GitHub repo:

Compatible with: Delphi 7-XE7 (but not with the Android and iOS targets), FreePascal >= 2.6.2 (with almost all FPC-supported targets including Android and iOS)

The Kraft Physics Engine has the following features:
  • Rigid body dynamics
  • Discrete collision detection
  • Optional additional continuous collision detection, either with Bilateral Advancement or Conservative Advancement
  • Full one-shot contact manifold collision shapes (spheres, capsules, convex hulls, boxes, and for static geometries also triangle meshs)
  • Multiple collision shapes per rigid body without the need for a compound shape
  • Broadphase collision detection with a dynamic AABB tree
  • Fast mid phase for static triangle mesh geometries
  • Narrowphase collision detection
  • Collision response and friction (Sequential impulses with post projection)
  • Joint constraints (Grab, Distance, Rope, Ball Socket, Hinge, Slider, Fixed)
  • Collision filtering with groups
  • Ray casting
  • Sleeping of inactive rigid bodies
  • Island-based multithreading
  • SIMD optimizations for x86-32 (later also for x86-64)
Future possibly possible features:
  • Sphere casting / Linear casting
  • Raycast vehicles
  • More joint constraint types (for example cone twist, universal and/or general 6DOF)
  • Cloths / Soft body
Feedbacks and comments are welcome.
Posts: 2
Joined: Tue Oct 24, 2017 7:11 am

Re: Kraft - a 3D physics engine in Object Pascal

Post by wchris »


Better late than never... I'm stuck so I'll still awake this thread and hope you're still there.

First, I'm only an average skilled programmer, please speak to me like to a beginner.

The demo of kraft "Sandbox" is impressive and works well

I use Delphi and tryed to make Kraft and Firemonkey work together.

The idea was basic and simple.

I had to to sync a fmx Tcube Matrix with a Kraft TRigibody shape matrix.

but I failed partially and don't understand why (searching a solution for 2 or 3 weeks now)

while the cubes do stack well and even bounce a little like expected, they do not rotate

Instead of rotating they slide ! (Guru meditation)

Would you mind helping me out with this ? Could I PM you my source code ?

Thank you in advance
PS: I know your kraft project is still active, so I have great hopes

Posts: 2
Joined: Tue Oct 24, 2017 7:11 am

Re: Kraft - a 3D physics engine in Object Pascal

Post by wchris »

Hello there, or not there :D

Finally I got it working. It took me one more week with my "try and fail + intuition" technique, but I got the expected result.

My initial intuition about assigning kraft matrix to firemonkey tcube matrix was good. Only I have to mutilply by -1 m12, m22, m32 and m42 or everything is inverted.

My alternate try with the TransformationFromKraft function extracted from Caste Game engine Kraft implementation with vector and angle computations
Q := QuaternionFromMatrix4x4(Shape.InterpolatedWorldTransform);
QuaternionToAxisAngle(Q, Axis, Angle);
Cube.RotationAngle.Vector := Vector3D(Axis.X, Axis.Y*-1, Axis.Z, angle);
was a complete failure with plenty of gimbal lock issues with cubes spinning crazy. I must have missed something because there is no such issue in the engine.

The problem witn matrix assignation is that I had to subclass the Firemonkey TControl3D object to get access to the private read only matrix. And of course, after assigning the matrix rendering is correct but all internal variables position, rotationangle, scale are wrong. They just don't expect anyone having to assign the matrix and don't handle it at all, ... my disappointment.

But this is not a kraft issue, so I just digress for the story.

Another issue is that on my android device (an old lg g2 mini) performance falls fast when adding cubes. I'll have to find ways to optimize. I guess the inverted Y axys in the matrix is not helping.

Well that's all folks, for now.
At least you know someone enjoyed your work
Kraft is amazing
Thank you
Screenshot_2017-10-29-14-35-03.png (61.68 KiB) Viewed 6837 times
PS: Kraft is compatible with Delphi tokyo but I had to mod it a little for android target
Post Reply