What is REALLY nice is, that NewtonBodyGetFirstContactJoint and the rest of contact joint functions also work pretty much everywhere outside material callback (really cool), so you can do this during the forcetorque callback and detect if a player was on stable or unstable surface (in previous physics simulation step as the current contacts will be only calculated after forcetorque callback ends), here's the code i use:
- Code: Select all
function CheckIFNewtonContactIsGround(joint: PNewtonJoint): boolean;
var
ThisContact: PNewtonJoint;
material: Pnewtonmaterial;
pos, nor: Vector;
begin
ThisContact := NewtonContactJointGetFirstContact(joint);
Result := False;
while ThisContact <> nil do
begin
material := NewtonContactGetMaterial(ThisContact);
NewtonMaterialGetContactPositionAndNormal(material, @pos, @nor);
nwi_upv := makevector(0, 0, 1);
nwi_nordot := DotProduct(nor, nwi_upv);
if nwi_nordot >= 0.8 then // deviation from the ideal gravity up vector.
begin
Result := True;
break;
end;
ThisContact := NewtonContactJointGetNextContact(joint, ThisContact);
end;
end;
function NewtonObjectOnGround(const body: PNewtonBody): boolean;
var
cnt: pnewtonjoint;
begin
Result := False;
cnt := NewtonBodyGetFirstContactJoint(body);
while cnt <> nil do
begin
if CheckIFNewtonContactIsGround(cnt) = True then
begin
Result := True;
break;
end;
cnt := NewtonBodyGetNextContactJoint(body, cnt);
end;
end;

