really cool stuff with material contact joints

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

really cool stuff with material contact joints

Postby JernejL » Thu Oct 07, 2010 7:14 pm

I wrote this really nice code to detect if a character controller or a box is on a level ground or a hill based on body contacts,
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;
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: really cool stuff with material contact joints

Postby ledahut » Fri Oct 08, 2010 3:05 am

Good, I didn't think we could use these function outside material callback.
I was looking for a long time how to display contact [position and normal] inside an Opengl render event. Thanks to you.
ledahut
 
Posts: 98
Joined: Mon Jun 21, 2010 8:03 am
Location: France

Re: really cool stuff with material contact joints

Postby Julio Jerez » Fri Oct 08, 2010 8:35 am

yes iterators are one of the first feature of 2.0 tah mak e coding game logic eassier than 1.0 because they can be use anywhere.
Julio Jerez
Moderator
Moderator
 
Posts: 12452
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 228 guests