BeRo1985 / kraft

Kraft Physics Engine is an open source Object Pascal physics engine library that can be used in 3D games.
113 stars 22 forks source link

Fix SphereCast() with mesh shapes. #29

Closed and3md closed 1 year ago

and3md commented 1 year ago

Hello,

I tried to use SphereCast() but it sometimes not works for me. I found the problem is in function SphereCastTriangle(). We have here code:

   if t<0.0 then begin
    result:=false;
    exit;
   end else begin
    Time:=-t;
    aU:=U;
    aV:=V;
    result:=true;
    exit;
   end;

But in function that call it (TKraftShapeMesh.SphereCast()) there is a condition (Time>=0.0):

       if SphereCastTriangle(Origin,
                             Radius,
                             Direction,
                             fMesh.fVertices[Triangle^.Vertices[DoubleSidedTriangleVertexOrderIndices[SidePass,0]]],
                             fMesh.fVertices[Triangle^.Vertices[DoubleSidedTriangleVertexOrderIndices[SidePass,1]]],
                             fMesh.fVertices[Triangle^.Vertices[DoubleSidedTriangleVertexOrderIndices[SidePass,2]]],
                             Time,
                             u,
                             v) then begin
        p:=Vector3Add(Origin,Vector3ScalarMul(Direction,Time));
        if ((Time>=0.0) and (Time<=SphereCastData.MaxTime)) and (First or (Time<Nearest)) then
        begin

So when t is greater than 0 it changes it sign and always is negative. My fix is to change that line to:

Time:=t;

After that SphereCast() works perfectly. My test case project is CGE simple game located in examples/physics/physics_3d_shooter in physics_walk_navigation branch commit 6fea532ae3550102bf3b6916c8a489a0022fe2ba.

BeRo1985 commented 1 year ago

It should also be fixed with https://github.com/BeRo1985/kraft/commit/8e4c714960e6c79e8d5bb1267b42aab4b0719d66 now. :-)

and3md commented 1 year ago

Thanks :)